roslyn: An option controlling the indentation of code #if blocks should be provided.
Currently #if
blocks are always formatted to start at the beginning of the line:
public class ExampleClass
{
public void ExampleMethod()
{
#if DEBUG
#endif
}
}
An option should be provided so that the #if
block can be indented to the desired level (probably the same options as label
has, which are left-most
, indent one less
, and indent normally
):
public class ExampleClass
{
public void ExampleMethod()
{
#if DEBUG
// Some code
#endif
}
}
public class ExampleClass
{
public void ExampleMethod()
{
#if DEBUG
// Some code
#endif
}
}
public class ExampleClass
{
public void ExampleMethod()
{
#if DEBUG
// Some code
#endif
}
}
It would also be useful, in the last example, to be able to indent the contents one more level:
public class ExampleClass
{
public void ExampleMethod()
{
#if DEBUG
// Some code
#endif
}
}
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 27
- Comments: 28 (16 by maintainers)
Since the comment I linked to above has been hidden as off-topic, I just wanted to paste in my supporting evidence for this issue here.
Thanks
Examples of users requesting this feature:
How to force indentation of C# conditional directives?
Can visual studio automatically indent / format preprocessing directives?
How to indent #if directives in code?
C# #if and #endif indentation is reset after formatting messing with folding of outer code
Formatting Pre-Processor Directives
C# editor removes indentation of preprocessor directives when autoformatting code is inserted immediately after them
Feature request - Do not format indentation of preprocessor directives
How to indent C# conditional directives using ReSharper StyleCop?
UserVoice Request for this feature:
Plugins for Visual Studio that add this feature:
ReSharper - Indention Style for Preprocessor Directives
CSharpFixFormat for VS Code - Indent Preprocessor Ignored
AStyle --indent-preproc-cond
At the very minimum, there needs to be an option to prevent our own carefully crafted indentation being wrecked.
The code exists to move the preprocessor directive to the first column so you’d think it would be easy to NOT move it. I can’t believe that 6 1/2 years later and it still hasn’t been fixed.
L.S.
I would very much like to be able to specify the indentation level of the preprocessor directives as well. I am of the opinion that always moving the preprocessor directives to the left-most column does not much help with the readability of one’s code. I think that indenting the preprocessor directives one level back from the code would be the most useful, but one should be able to specify this.
Regards, Jeroen Posch.
@CyrusNajmabadi, one of the problems with this is the same as with the “switch case” indentation issue (which was eventually fixed). That is, the VS IDE, under its default settings, fights against you from formatting your code this way (and in many cases, there isn’t an option to prevent the IDE from reformatting your code).
However, as @jbienzms indicates, there are a number of requests on various external sites and other editors or extensions do allow this. Other languages, even in VS, also provide options for these types of settings (such as C/C++).
I would like to vote for this issue to be re-opened. Instead of copying and pasting all of my reasons why inline, I’ll refer to the existing reasons I already gave here:
https://github.com/dotnet/roslyn/issues/2487#issuecomment-418837810
@sharwell you wrote above:
As I mentioned in my linked post above, there are many requests for this feature on Stack Overflow and this feature has 29 upvotes in Visual Studio UserVoice. This feature also exists in ReSharper, so there is at least some evidence that there is demand.
Any chance you could elaborate on the specific numbers or statistics that the team would accept as empirical evidence to necessitate this feature?
There does not need to be an option specifically for this. Features within the IDE today are generally expected to not reformat code which is outside the scope of the action being applied. There are a few cases where we know of problems occurring (e.g. #2487). Many other cases would be considered edge case bugs.
Note that in most cases, code does not need to include
#if
/#endif
because other options are available (e.g. polyfills orConditionalAttribute
). The remaining cases should be sufficiently infrequent that the default indentation is irrelevant because users almost never see it anyway.@MV10 can you file a separate issue on that?
We’d happily accept a PR to fix that up if you’re interested. Thanks! 😃
Other languages do not though 😃 VB allows no formatting options for example (and there have definitely been requests for it 😃). C/C++ has never been a guiding language to help motivate IDE scenarios. (And i believe i can say that that has been true since forever 😃).
True. 😃
Part of the reason there was because there was a regression involved. Our older formatter would not indent blocks in a case, but then we broke that (probably in the roslyn rewrite). We were then in the unpleasant position of potentially having two sets of users, those who had code with the original style, and code that was now being formatted into the new style. Fixing the regression would break thsi new group. So the undesirable position was taken to introduce the option to allow people to say which behavior they wanted.
The indentation behavior of
#if
has remained the same for many Visual Studio releases. During today’s design review, the team generally felt that uniformity provided by this uniformity is an asset to the ecosystem, and we would not want to introduce a configuration item related to this outside of empirical evidence showing significant use of an alternative formatting.@thexdd I know this isn’t the news you wanted to hear. However, we would love to have you contribute to the project. We have a rather large selection of issues in our Small Fixes project, or you can look for issues labeled help wanted to see if something there catches your interest.
I agree. It would be useful.