Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get server blocks <% %> to format well in Visual Studio?

Tags:

I'm using ASP.NET MVC a lot recently which means using server blocks a bunch. Visual Studio does this strange thing when I type:

<% } %>

and hit enter, I get this:

<%
     }
     %>

Which is unsightly and generally the most horrible thing I've ever seen. I've customized the Ctrl+K+D behavior pretty heavily and when I do this I get everything to look the way I want except for this one case. Anyone have any suggestions on how to customize this?

like image 549
JC Grubbs Avatar asked Apr 06 '09 01:04

JC Grubbs


People also ask

How do I fix code formatting in Visual Studio?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

How do I beautify HTML code in Visual Studio?

To improve the formatting of your HTML source code, you can use the Format Document command Ctrl+Shift+I to format the entire file or Format Selection Ctrl+K Ctrl+F to just format the selected text. The HTML formatter is based on js-beautify.

How do I beautify JSON in Visual Studio Code?

Formatting# You can format your JSON document using Ctrl+Shift+I or Format Document from the context menu.


1 Answers

Looking at the behavior. I don't know that you can override it just for the HTML editor. It is exhibiting the exact behavior that is defined in the C# editor preferences.

I suppose you could modify the behavior for a closing brace in the C# editor preferences, but it would do fugly things to your .cs files.

Edit:

I got tired of trying to find other ways to do it. So I wrote this macro. Tested in VS 2k8. Not 2k5. You can see what it does. Also, sorry about the VB, but it was the default for the macro editor.

Sub FixFormatCurrentFile()

    Dim selection As TextSelection = DTE.ActiveDocument.Selection
    Dim fixed As String = "<% } %>"
    Dim regex As String = "\<\%:Wh*\}:Wh*\%\>"

    While selection.FindPattern(regex, vsFindOptions.vsFindOptionsRegularExpression)
        selection.ReplacePattern(regex, fixed, vsFindOptions.vsFindOptionsRegularExpression)
    End While
End Sub
like image 120
Chad Ruppert Avatar answered Oct 04 '22 20:10

Chad Ruppert