开发者

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

开发者 https://www.devze.com 2023-01-05 15:13 出处:网络
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

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?


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.


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号