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


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.

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

Closed Aug 3, 2013 at 4:01 AM by kamehrot


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, 2013 at 4:30 PM

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

joshmouch wrote Jan 17, 2013 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, 2013 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.
    @if (true)
            @if (true)
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.

johnmblack wrote May 23, 2013 at 4:34 PM

Wondering if anyone has heard of any progress on this.

yishaigalatzer wrote May 23, 2013 at 8:39 PM

@johnmblack - As you can tell from the bug status it is planned to be addressed in our next release.

We already know how to fix the formatting when you format using Ctrl-K-D, and we are looking at the auto formatting issue as well.

johnmblack wrote May 23, 2013 at 9:08 PM

Thank you! What is confusing for me (newbie, I admit) is how the release numbers for this particular component relate to the MVC release numbers overall -- Does "5.0 RTM" mean that this is for MVC5 ? Or is this component on its own release numbering?


yishaigalatzer wrote May 24, 2013 at 1:31 AM

The bug tracking numbering is based on the MVC releases but the individual components will have the following numbering:

Razor/Web Pages V3
WebApi V2

yishaigalatzer wrote Jul 16, 2013 at 4:34 AM

Fixed the issue for Razor V3.

Please verify using the latest Visual studio 2013 (preview bits still have some critical bugs in this area), work with Anand who verified the private bits.

stijnherreman wrote Jul 16, 2013 at 6:17 AM

Thank you! Will play around with this sometime soon.

thelehehmanlip wrote Oct 9 at 8:43 PM

Still getting these issues in 2013 Update 3. Will this ever truly be fixed.

yishaigalatzer wrote Oct 9 at 9:13 PM

@thelehehmanlip can you please let us know what version of Razor you are using in your project?