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

后端 未结 4 524
-上瘾入骨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: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):

        

    <%: Model.Name %>

      <% foreach (var item in Model.Items) { %>
    • <%: item %>
    • <% } %>

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

提交回复
热议问题