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?
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.
To access this options page, choose Tools > Options from the menu bar. In the Options dialog box, choose Text Editor > C# > Code Style > Formatting.
Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.
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!
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):
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With