Configuring the formatting of <% %> blocks in Visual Studio editor

后端 未结 4 468
-上瘾入骨i
-上瘾入骨i 2021-02-05 20:28

In Visual Studio 2010, under Tools -> Options -> Text Editor -> HTML -> Formatting -> Tag Specific Options, there are options for configuring how the editor auto formats differe

相关标签:
4条回答
  • 2021-02-05 21:03

    Well, there's Edit -> Advanced -> Format Document, which I guess messes your code up as well. I searched loads of documentation trying to find something more, but as far as I can say, there isn't anything.

    Edit: The Problem isn't in the HTML Formatting options but with the <% %> "tag". For example: <h2><span></span></h2> works quite well. As I said, I don't think this can be done.

    For the moment I recommend using:

    <h2>
        <%="Hello World" %>
    </h2>
    

    or

    <h2>
        <%
            if(true)
                Response.Write("Hello World");
        %>
    </h2>
    
    0 讨论(0)
  • You were looking in the right area:

    Tools -> Options -> Text Editor -> HTML -> Formatting -> Tag Specific Options.

    However, you need to set the option in "Client tag supports contents", under Default Settings, for Line breaks to "None". Visual Studio is looking at this setting rather than the setting for the <h1 /> tag.

    I don't believe this will give you the spacing inside the <% %> tag that you want, but it will fix those pernicious extra line breaks.

    [EDIT] I had initially said to set the option for "Server tag supports contents", but I think it's actually "Client tag supports contents" (I changed this above). You can also set the "Line breaks" setting to "Before and after" instead of to "None" if that better gives you what you are looking for. You may also need to set Line breaks for "Client tag does not support contents" to "None".

    0 讨论(0)
  • 2021-02-05 21:11

    Indeed there is no any significant difference between the two folders (Client and ASP.NET) from the VS's point of view. They exist just for convenience. The fact is that the tag is recognized only by its name and (luckily) VS ignores it's not a true tag. Therefore you can put subject settings in either of the folders. Even more one can create (what personally I did) another folder (called, for example, Expressions) and store settings there.

    Concerning changing "Default Settings". If one wants to change settings only for a few tags then IMHO it should be better to create/change the rules for these tags themselves rather than changing the defaults (appears that h1-h6 rules are missing withing the default set of rules).

    0 讨论(0)
  • 2021-02-05 21:15

    Thanks to @schellack for the knudging me in the right direction. Here are the settings I needed to get the behavior I wanted (all within the tag specific options dialog box):

    • Default Settings -> Client tag supports contents
      • Line breaks: Before and after
      • (This makes h1, p, and similar tags behave the way I wanted. Others may want None as a choice. Personal preference I suppose.)
    • Add a new tag under Client HTML Tags.
      • Tag Name: %
      • Closing tag: No closing tag
      • Line breaks: Before and after
      • (This catches actual code blocks and keeps them separated from HTML markup with line breaks before and after the code blocks.)
    • Add another new tag under Client HTML Tags
      • Tag Name: %:
      • Closing tag: No closing tag
      • Line breaks: None
      • (This catches <%: %> blocks and keeps them inline with HTML markup without any line breaks.)
    • Add another new tag under Client HTML Tags
      • Tag Name: %=
      • Closing tag: No closing tag
      • Line breaks: None
      • (Similar to the previous one. This catches <%= %> blocks and keeps them inline with HTML markup without any line breaks.)

    The trick is that the editor seems to recognize <% %> blocks as a client tag named '%' that has no closing tag. Same deal for <%: %> and <%= %>.

    With these settings (combined with the rest of the defaults in Visual Studio) I get formatted markup that looks like the following (which is the compact form I was looking for):

        <h1><%: Model.Name %></h1>
        <ul>
            <% foreach (var item in Model.Items) { %>
            <li><%: item %></li>
            <% } %>
        </ul>
    

    As yet, it doesn't appear that the second part of my question is possible.

    0 讨论(0)
提交回复
热议问题