Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve NormalizeWhitespace for nested fixed/using statements without blocks #61518

Closed
elinor-fung opened this issue May 25, 2022 · 1 comment · Fixed by #61533
Closed

Improve NormalizeWhitespace for nested fixed/using statements without blocks #61518

elinor-fung opened this issue May 25, 2022 · 1 comment · Fixed by #61533
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@elinor-fung
Copy link
Member

Version Used:
Microsoft.CodeAnalysis.CSharp version 4.2.0

Steps to Reproduce:

Call NormalizeWhitespace() on code with nested fixed or using statements without a { } block between.

string code = @"
public unsafe void M1(string s1, string s2, string s3)
{
    fixed (char* ptr1 = s1)
    fixed (char* ptr2 = s2)
    fixed (char* ptr3 = s3)
    {
    }
}

public void M2(string path)
{
    using (FileStream fs = File.Create(path))
    using (StreamWriter sw = new StreamWriter(fs))
    {
    }
}";
Console.WriteLine(ParseSyntaxTree(code).GetRoot().NormalizeWhitespace().ToFullString());

Expected Behavior:

Output:

public unsafe void M1(string s1, string s2, string s3)
{
    fixed (char* ptr1 = s1)
    fixed (char* ptr2 = s2)
    fixed (char* ptr3 = s3)
    {
    }
}

public void M2(string path)
{
    using (FileStream fs = File.Create(path))
    using (StreamWriter sw = new StreamWriter(fs))
    {
    }
}

Actual Behavior:

Output:

public unsafe void M1(string s1, string s2, string s3)
{
    fixed (char* ptr1 = s1)
        fixed (char* ptr2 = s2)
            fixed (char* ptr3 = s3)
            {
            }
}

public void M2(string path)
{
    using (FileStream fs = File.Create(path))
        using (StreamWriter sw = new StreamWriter(fs))
        {
        }
}

Additional Notes:

With the usage of NormalizeWhitespace for source generators, this results in some awkwardly formatted generated code.

Auto-formatting code like this in Visual Studio stacks the statements without indentation like in the expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
2 participants