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

2

21

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?

Hardboiled answered 6/4, 2009 at 1:48 Comment(5)
OMG I want to know to! thanks for asking!Insubstantial
Good question, this has been bugging me since I started using MVC regularly.Banjermasin
Y'all should submit this to the MS Connect feedback site and vote it up like crazy and they might fix it.Groundhog
You should subnmit it to connect.microsoft.com/visualstudio then post the bug/suggestion URL here, then whoever cares should vote once each. Screw with voting and they'll just ignore the entire suggestion.Gregg
If someone does post it to MS Connect, link back here so we can upvote itStocking
J
3

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
Janetjaneta answered 6/4, 2009 at 15:23 Comment(2)
I'm not familiar with using macros in Visual studio...can I essentially attach this to Ctrl+K+D?Hardboiled
Not to ctrl-k-d, its going to have be an extra keystroke, I believe. You could however, likely call the 'autoformat' within the macro first.Janetjaneta
T
0

i think you format it by Ctrl + K, Ctrl + D

Tallou answered 7/4, 2009 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.