Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML formatting in Visual Studio 2010

Whenever I reformat html source code in Visual Studio with Ctrl-K, Ctrl-D it formats my source code like this:

<p>
    text</p>
<p>
    more text</p>

How can I make it use the following format instead?

<p>
    text
</p>
<p>
    more text
</p>

I know that there are settings at Options-> Text Editor -> Html -> Formatting, but I could not find suitable there.

Thanks,

Adrian

Edit: I've checked the tag-specific settings, and page break for p tags is set to "Before opening, within, and after closing". Also, the little preview shows exactly the format I want to have. But Visual Studio still does it wrong. Could this have anything to do with Resharper being installed on my system?

like image 348
Adrian Grigore Avatar asked Jan 09 '11 14:01

Adrian Grigore


2 Answers

The problem has nothing to do with ReSharper. This is a feature by design of the Visual Studio Source Formatter where it will attempt not to change the semantics of an element due to formatting options that you specify.

So, you specified that you want the p tags to have breaks within the content, but a break after a p tag would change the semantics of the content within the tag, thus the formatter ends up putting the closing p tag right after the content. To have the closing tag on a separate line you will need to explicitly add a space just before the end of the content and the closing tag.

Thus:

<p>content</p>

will produce:

<p>
   content</p>

While (note the explicit inclusion of a space between the content and the closing p tag):

<p>content </p>

will produce:

<p>
   content
</p>

This is discussed in a blog post by Scott Guthrie in the 3rd paragraph from the bottom. Start counting from the paragraph right above the additional links section.

like image 131
Waleed Al-Balooshi Avatar answered Oct 12 '22 21:10

Waleed Al-Balooshi


Click Tools, Options, Text Editor, HTML, Formatting, Tag Specific Options.
Add a new Client-side tag for p (if it's not there already) and select Separate Closing tag and Before, within, and after closing.

like image 24
SLaks Avatar answered Oct 12 '22 19:10

SLaks