12
Vote

Razor code formatting is broken when using tabs in VS2012 MVC 4

description

When using "Keep tabs" instead of the default "Insert spaces", and running "Fomat document" (CTRL-E, CTRL-D) on in a .cshtml file with razor code any ifs inside other markup gets "mangled". I've attached a screenshot with a comparison of space and tab formatting.

Repro:
In Visual Studio 2012 (I'm using premium), create a new ASP.NET MVC 4 project (using the default web site template).
Open "Tools"->"Options"->"Text Editor"->"All Languages"->"Tabs" and set "Keep tabs" instead of "Insert spaces".
Open any view (.cshtml) and insert:
<div> @if(true) { } </div> Run format document (CTRL-E, CTRL-D).

file attachments

comments

aeinbu wrote Sep 11, 2012 at 8:11 PM

This is also the case for MVC3, and in VS2010

kkbruce wrote Sep 12, 2012 at 2:38 AM

Visual Studio 2012 + MVC 4 + VB.NET for Razor is also the case.

petropavlovsky wrote Nov 13, 2012 at 4:42 AM

I've the same issue in vs2012. It's even more strange, because it's being reproduced with space's indents.

Schotime wrote Nov 21, 2012 at 3:35 AM

I get this issue with spaces as well on vs2012 - MVC3/MVC4

joshmouch wrote Jan 17 at 4:30 PM

Any idea if/when this will be fixed? I hate using space indentation.

joshmouch wrote Jan 17 at 5:36 PM

One workaround I found that still lets you use tab indentation is to use Razor code blocks.

So for example, replace the following:
<div> <div> @if (true) { <b>Hi</b> } </div> </div> with:
<div> <div> @{ if (true) { <b>Hi</b> } } </div> </div>

stijnherreman wrote Feb 6 at 10:07 AM

This issue has existed for more than two years now. How hard can it be to fix this really? Apparently very hard. One of the team members, on StackOverflow:
It does not work correctly in all cases because it's a difficult problem to solve.
<div>
    @if (true)
 {
    <div>
        <div>
            @if (true)
   {
   }
        </div>
    </div>
 }
</div>
Now look closely.
The first @if is indented with one tab. With how many spaces is the { indented? One space.
The second @if is indented with three tabs. With how many spaces is the { indented? Three spaces.

I'm beginning to think you guys secretly hate tabs, and decided you wanted to end the "tabs vs spaces" holy war.