aspnet / Tooling

Issue tracker and info on Visual Studio tooling for ASP.NET
Other
256 stars 124 forks source link

Automatic Formatting Breaks Code in CSHTML #909

Open RandyBuchholz opened 7 years ago

RandyBuchholz commented 7 years ago

Background

This issue has been discussed before but considered "as designed". (Including in issue #8224, which I can't find the link too now). This is a case that wasn't raised before that I believe makes it worth looking at again.

The root concern is that in many cases you can't really turn auto-formatting off. The result of this is that the editor is injecting things like tabs and spaces into your code. In many languages that is not an issue because spaces do not impact the output. But in others (like CSHTML) tabs and spaces do. The result is that the editor breaks your code or prevents you from doing some basic things.

Example

VS2015 Update 3, Editing a View (.cshtml)

Create css just to make things visible.

.layout { display:inline-block; border: 1px solid #000; height: 20px; width: 20px; }

Set Indenting to None. Set Tabs to Keep Tabs

Type:

<div><div class="layout"></div><div class="layout"></div></div>

Run the page and two boxes will show side by side with no space between them.

Place your cursor after the first </div> and hit return. The text becomes formatted/indented even though it is set to none.

<div>
    <div class="layout"></div>
    <div class="layout"></div>
</div>

Run the code again and a space separates the boxes.

Issue

An editor should never force its own code into your output when it impacts behavior. I don't really have a problem with auto-formatting staying on (except that it should be greyed out in options if you can't use it so you don't wonder what's happening (or not)).

Suggestion

I can see two possible approaches to this. 1) The first would be to use some type of char that HTML ignores to represent a "auto-item". This approach basically distinguishes (in the background) between formatting that was done by the editor from that done by the user. Visually there would be no difference. 2) The second addresses the narrower case in my example. In Editor Options Advanced there could be a switch to "Ignore Only Tabs Between Tags". When the document is being parsed, it would just ignore them and they would not show up in the output. If the only thing found between two tags is tabs it would exclude them and place the elements together.

mlorbetske commented 7 years ago

TFS Bug#362033