views:

89

answers:

3

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?

A: 

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>
Dänu
Thanks, but this answer simply restates my question. I know that the problem is with the <% %> tag, and I know how to configure them to be be on 3 lines. The question is if it is possible to get it on 1 line because that is much easier to read when the code block is short and trivial (as is often the case in ASP.NET MVC).The answer to my question may in fact be "it's not possible".
Erv Walter
Well as I said, in my opinion it isn't possible, since <% %> is not treated as an HTML "tag" and neither as a C# syntax element. The two code examples above show, according to Microsofts examples, the way the developer is intended to use the <% %> "tags".
Dänu
+1  A: 

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".

schellack
Setting None for the "Client tag does not support contents" is too wide reaching for what I want (I want those as before and after), but +1 for the hint that helped me find a better solution.
Erv Walter
+2  A: 

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.

Erv Walter
Just wondering, what version of VS do you use?
Dänu
Visual Studio 2010
Erv Walter
Note that ReSharper in this case is not very helpful, as we want automatic formatting on ";" and "}" in regular source code, but not in our html markup.
Thomas Eyde
Wow. This is awesome!
Jordan