Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Visual Studio 2010, under Tools -> Options -> Text Editor -> HTML -> Formatting -> Tag Specific Options, there are options for configuring how the editor auto formats different HTML and ASP.NET tags. This includes things like if it should automatically put a newline before and after the tag, etc.

Is there a place to configure the formatting rules for <% %> <%= %> and <%: %> blocks in a similar fashion?

In particular, I would like to not force a newline before <%= and <%: blocks.

For example, I have already configured the options for the h1 tag to not add newlines around its contents and that works great with static content, but it doesn't work when there is a <%: or <%= block in the h1 tag. I currently get this:

<h1>
    <%: Model.Name %></h1>

but I would like this:

<h1><%: Model.Name %></h1>

In a perfect world, I would also like to auto format the contents of <% %> blocks to make sure there is always a space between the <% and its contents.

For example, good:

<% if (something) { %>

bad:

<%if (something) {%>

So, are there any settings buried somewhere to control either of these formatting behaviors?

like image 464
Erv Walter Avatar asked Jul 01 '10 16:07

Erv Walter


People also ask

How do I format a block of code in Visual Studio?

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.

How do I add formatting in Visual Studio?

To access this options page, choose Tools > Options from the menu bar. In the Options dialog box, choose Text Editor > C# > Code Style > Formatting.

How do I add formatting to Visual Studio code?

Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.

How do I fix format in Visual Studio?

As long as your code is syntactically correct (i.e. no stray brackets or End Ifs without matching Ifs), Visual Studio will reformat your whole file with one key chord: Hold down the Control key and then press K, followed by D. Boom!


1 Answers

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.

like image 152
Erv Walter Avatar answered Oct 07 '22 11:10

Erv Walter