Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indentation is broken in Visual Studio .cshtml files

It's the most infuriating thing and after 45 minutes of Googling and testing I caved to the forum gods... I simply cannot live without automatic indentation, even if it's just on .cshtml view files

I'm using VS2015 Community edition. My cshtml files have played nicely until now...

For some odd reason, indentation broke.

I have already tried the obvious:

  • Selecting the markup and hitting Ctrl+K and Ctrl+F
  • Removing and re-adding the closing tag (usually resets indents)
  • Going into Tools>Options>Text Editor>[insert language-or-all]>Tabs and setting them to smart.

I had just installed ReSharper before noticing the problem. Not sure if that caused it or not, but the problem is indeed new and I don't remember seeing it before that.

Also odd, some things do indent while others do not

For those that need a visual aid, this indentation frustrates me:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
<h1>Posts Index</h1>
@if (true)
{
 <text>
     <p>True is true!</p>
 </text>
}
</body>
</html>
like image 468
Methodician Avatar asked Oct 29 '15 03:10

Methodician


People also ask

How do I fix indentation in Visual Studio?

Go to Home and select Line and Paragraph Spacing > Line Spacing Options at the bottom of the menu. The Paragraph dialog box opens. On the Indents and Spacing tab, select the options you want, and click OK. The Paragraph dialog box options are described in Adjust indents and spacing.

How do you indent multiple lines in Visual Studio?

If you prefer using [spacebar] to indent your code rather than using [tab], you can select multiple lines by holding the [alt] key and clicking on the beginning of each line you want to indent. Then, you can press [spacebar] and all the selected lines will be affected.


1 Answers

You mentioned that you just installed Resharper before this problem started occurring, so your problem is most likely being caused by Resharper.

By default Resharper does many extra formatting fixes as you type, but you'll likely find a bunch of these default fixes conflict with your coding style.

You can configure Resharper's code formatting options via Resharper > Options > Code Editing > HTML|Razor|etc > Formatting Style

In this case, your problem was the setting Do not indent children of contained tags that you didn't want by default.

like image 98
learningcs Avatar answered Oct 11 '22 06:10

learningcs