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
}
}
I agree. It would be useful.
It's been a year already.. pls implement this feature :(
@thexdd Would you be interested in contributing here? I can give you some pointers on how you could start!
I'm adding this and #12305 to an upcoming design review to determine if this is a feature we want to start maintaining in the core formatting code.
@CyrusNajmabadi I would love to participate in that, actually.
@sharwell Sounds great. Looking forward to hearing more information regarding this issue!
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 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:
we would not want to introduce a configuration item related to this outside of empirical evidence showing significant use of an alternative formatting.
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?
@jbienzms I would be interested in the number of github projects, for example, that use this style for C# (i.e. the majority of their pp-directives are indented, and it's not just a one-off). I think there are some projects that have analyzed github and placed the results in queryable DBs. But i'm not personally familiar with them or how you'd go about efficiently querying all this data.
I would be interested in the number of github projects
@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++).
Other languages, even in VS, also provide options for these types of settings (such as C/C++).
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 :)).
@CyrusNajmabadi, one of the problems with this is the same as with the "switch case" indentation issue (which was eventually fixed).
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.
However, as @jbienzms indicates, there are a number of requests on various external sites and other editors or extensions do allow this.
Agreed. This may be enough information to warrant this. While i'm not necessarily of the belief that we should just do what another editor does, i do think there is significant enough feedback through regular channels to warrant the question being asked and decided on again.
As before, my personal opinion would be to not have it. But it's certainly up to the team, and they may feel differently.
Reopening for reconsideration.
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:
Can visual studio automatically indent / format preprocessing directives?
C# #if and #endif indentation is reset after formatting messing with folding of outer code
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:
Thanks for gathering those links @jbienzms, it's very helpful. 馃槃
We reviewed this proposal in light of the new options given above, and came to a conclusion that the proposal described in https://github.com/dotnet/roslyn/issues/28729#issuecomment-426247800 would apply to this feature request as well. I'll create a Story issue to design the new extension point and link it to the two open issues it covers.
Marking as Blocked on #31691
Most helpful comment
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