dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.1k stars 4.04k forks source link

Formatter doesn't correctly format usage of fluent APIs #67931

Open Youssef1313 opened 1 year ago

Youssef1313 commented 1 year ago
using System.Text;

var builder = new StringBuilder();
builder.AppendLine(
builder.AppendLine(
    builder.AppendLine().ToString()).ToString())
            .ToString();

The formatter is happy with the above code.

It shouldn't be happy and should format it to:

using System.Text;

var builder = new StringBuilder();
builder.AppendLine(
    builder.AppendLine(
        builder.AppendLine().ToString()
    ).ToString()
).ToString();
sharwell commented 1 year ago

Note that if any change is made, the expected behavior would be the following (only indentation changed):

using System.Text;

var builder = new StringBuilder();
builder.AppendLine(
    builder.AppendLine(
        builder.AppendLine().ToString()).ToString())
                .ToString();

Overall, I would not expect any change to be made here. There are multiple potentially-valid indentation locations for the second line, and the formatter doesn't have a way to allow several options but only enforce one.

CyrusNajmabadi commented 1 year ago

Overall, I would not expect any change to be made here

Me neither. It would also be highly problematic as suddenly lots of code would start getting formatting warnings.