dotnet / dotnet/roslyn

Let me set the C# formatter to use correct indentation (or at least not remove it) on blank lines

Open
#83,443 3 comments 4 reactions 0 assignees View on GitHub
Area-IDE Feature Request
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Brief description:**

I want the C# formatter (in VSCode) to ***stop removing indentation from blank lines***.

Not all IDEs support virtual whitespace, and even if so, it shouldn't force users to have it enabled. I want to keep blank lines in code indented to whatever the level is there. I hate that it keeps removing my indentation in blank lines whenever I save, because I left the indentation there on purpose - I want to just click and type already at the correct indentation I had before whenever I add something at that level. I don't want to press an unnecessary tab that I didn't need before saving.

More importantly, in my style it's ***logically wrong*** for the file to have incorrect lack of indentation at any level, regardless of if the line is blank. It's not like I'm de-indenting the code just because it's a blank line. It breaks the code's flow!

If there's already an .editorconfig setting (or repo issue) for this, please point me to it, because I couldn't find one.

I'd expect something like this in .editorconfig:
```editorconfig
csharp_indent_blank_lines = none|ignore|indent
```

**Languages applicable:**

I care only about C#; I don't know any VB or if this problem applies to VB.

**Code example that the analyzer should report:**

Pretend the `// blank line ...` comments don't exist (they're just here to show where the indentation is on blank lines). These lines have only spaces or tabs, up to right before the comment.
```cs
// csharp_indent_blank_lines = indent
// this is the setting I want
namespace N {
class A {
void F() {}
// blank line here; if I save the file, indentation is fixed to be here
void G() {}
}
}

// csharp_indent_blank_lines = none
// that's the current behavior that I hate
namespace N {
class A {
void F() {}
// blank line here; if I save the file, whitespace is removed to use less bytes or whatever
void G() {}
}
}

// csharp_indent_blank_lines = ignore
// not super required to implement, but it's probably a good idea to allow this too
namespace N {
class A {
void F() {}
// blank line is not touched, whitespace stays wherever I left it
void G() {}
}
}
```

**Additional information:**

In VSCode, I set `"editor.trimAutoWhitespace": false,` precisely so I don't have this annoyance, which works in other languages, but the C# formatter ruins my preference.

But note that I'm NOT asking for the formatter to stop removing trailing whitespace on non-blank lines! My point is that indentation whitespace on blank lines is not "trailing whitespace", it's leading/indentation whitespace which I want to keep/fix. Any whitespace AFTER that is "trailing whitespace" that's extra and should be removed.

**Documentation requirements:**

When this analyzer is implemented, it must be documented by following the steps at [Documentation for IDE CodeStyle analyzers](https://github.com/dotnet/roslyn/blob/main/docs/contributing/Documentation%20for%20IDE%20CodeStyle%20analyzers.md).

Contributor guide

Open the contributing guide

Research direction

The issue names no repository files or tests. Start by tracing the C# formatter's handling of whitespace and existing .editorconfig code-style options, using the supplied C# examples as cases; done means blank-line indentation can be configured as indent, none, or ignore, without changing trailing whitespace behavior, and the option is documented according to the linked analyzer documentation guidance.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.