21

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>
Methodician
  • 2,396
  • 5
  • 30
  • 49
  • Have you looked in `Resharper > Options > Code Editing > HTML|Razor|etc > Formatting Style`? I've found that the default settings never seem to play nicely with VS auto-formatting and needs some configuring. You may have lost some settings when you reinstalled Resharper perhaps. – learningcs Oct 29 '15 at 03:27
  • You have the `{` on one line. This isn't going to work because you have C# and Html mixed on one line. Put the `` on the line below the opening handlebar/squiggle/brace/bracket/thingamabob, then try CTRL+K, CTRL+F (or CTRL+K, CTRL+D...whatever your auto format hotkeys are) – Bardicer Oct 29 '15 at 13:32
  • @rshepp: That's it! It had a "do not indent children of" clause with a bunch of common tags... I guess not everyone would agree that the above is "bad indentation" but would elect to keep it like that. I love me some good indentation though. Thanks! Post it as an answer and I'll mark it as such. – Methodician Oct 31 '15 at 01:17
  • @Nick Hmm... not sure how it ended up like that in my pasted code but it's on its own line in my actual code. I'll update the post to match... – Methodician Oct 31 '15 at 01:17

3 Answers3

18

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.

learningcs
  • 1,856
  • 16
  • 25
3

Since 3 weeks (since Update to Visual Studio 2015 SP 2 after VS 2013) I had the problem, that copy/paste and comment in cshtml files destroy my format of my file. Interestingly the tab indent was at column 7. I don't understand why.

After Update to VS 2015 SP 3, the problem was not banned.

Then I found out, that in my cshtml files a "@" sign destroyed my copy/paste/comments:

@model MAWGridModel<AktionGridRowModel>

@if (Model != null)
{
    @Html.DevExpress().GridView(settings =>
    {
        settings.Name = "MAWAktionenErgebnisGrid";
    ...
    }).Bind(Model).GetHtml();
}

The "@"before "Html.DevEpress()..." destroyed all. Here the code snippet which works for me. (I hope it will really do.)

@model MAWGridModel<AktionGridRowModel>

@if (Model != null)
{
    Html.DevExpress().GridView(settings =>
    {
        settings.Name = "MAWAktionenErgebnisGrid";
        // ...
    }).Bind(Model).Render();
}

Hopefully it helps you.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
Annette V
  • 51
  • 3
  • 1
    I was having a similar issue, but in my case, it was a `@* ... *@` comment block inside a already @-started code block. I replaced the `@* ...` comment notation with traditional `//` and everything would align normal again. – Reuel Ribeiro Jul 08 '16 at 19:34
0

I work with VS2017 (asp.net core 2.2), use Devextreme controls and have exactly the same problems.
I have deactivated the automatic auto format after copy paste since a longer time now (as it don't work).
As I have large views with > 100 DE controls (each with about 5 properties in average), I have to format the view (with "Format document" whereby "Format selection" don't work in my installation, b.t.w.) from time to time, to make the file structure "readable".

If I do so, I have exactly the same problem (the lines are intended to the right until they are not visible as "out of monitor" and I have a large monitor...).
To make the file "readable" again, I have to edit every single line manually (remove wrong space).

I have wasted hours now with try-and error. The only (very ugly and bad) "workaround" I found is, to write the whole code on ONE line.
Example: @(Html.DevExtreme().TextBox().ID("AngebotFirma").Value(@Model.PAB_Firma).MaxLength(45).Placeholder("Firma...").InputAttr("autocomplete", "company") )

This way, the whole document (HTML, JQuery and also Devextreme (the one line) is formatted (intended) correct.

So.. this is not a solution, but may be a workaround to save time. I change my code to "one line code" at least until I have finished and tested the views. After that, I - maybe - will change the code back to "multiline"...

FredyWenger
  • 2,236
  • 2
  • 32
  • 36