Skip to content

Preprocessors are not put on their own line during code formatting #76566

Closed as not planned
@SteveDunn

Description

Hi - there's been a change in Roslyn between Visual Studio 17.13.0 Preview 1 and Preview 2.1.

Previously (rightly or wrongly), I could emit code such as:

    public static global::System.Boolean TryParse( #if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] 
#endif
    global::System.String? s, global::System.IFormatProvider? provider, #if NETCOREAPP3_0_OR_GREATER
 [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] 
#endif
    out AssemblyName result)

Note the preprocessor are on the same line.

Prior to 17.13.0 Preview 2.1 (I tried with 17.13.0 Preview 1), the formatter would place those preprocessors on different lines, e.g.:

    public static global::System.Boolean TryParse( #if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] 
#endif
    global::System.String? s, global::System.IFormatProvider? provider, #if NETCOREAPP3_0_OR_GREATER
 [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] 
#endif

The formatting code that I use is:

    public static SourceText FormatSource(string source)
    {
        SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source);
        SyntaxNode root = syntaxTree.GetRoot();
        SyntaxNode formatted = root.NormalizeWhitespace();

        return SourceText.From(formatted.ToFullString(), Encoding.UTF8);
    }

This was reported in my source generor, Vogen: SteveDunn/Vogen#725

The issue is obviously that preprocessors need to be on their own lines, but I thought I'd let you know as I would guess there are other source generators that miss this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions