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.07k stars 4.04k forks source link

Formatting: if a member has multiple attributes, the attributes on the same line get formatted with a line break #21789

Open jceipek opened 7 years ago

jceipek commented 7 years ago

This is probably related to #11685 and is the one thing keeping me from switching to formatting on save.

Version Used: Visual Studio Code 1.15.1 with the extension "C# for Visual Studio Code (powered by OmniSharp) v 1.21.1"

I could be mistaken, but I think roslyn provides the underlying formatting engine for the extension; please let me know if that isn't the case.

To Reproduce: Format a file that includes a member with multiple attributes, such as this:

    [Tooltip("Should probably start at 0,1 and end at 1,0\n to make attraction near the suctionOrigin highest")]
    [SerializeField] [CurveRange] AnimationCurve _suctionForceDistanceCurve = null;

Expected Formatting Result:

    [Tooltip("Should probably start at 0,1 and end at 1,0\n to make attraction near the suctionOrigin highest")]
    [SerializeField] [CurveRange] AnimationCurve _suctionForceDistanceCurve = null;

Actual Formatting Result:

    [Tooltip("Should probably start at 0,1 and end at 1,0\n to make attraction near the suctionOrigin highest")]
    [SerializeField]
    [CurveRange]
    AnimationCurve _suctionForceDistanceCurve = null;
dpoeschl commented 7 years ago

Hi @jceipek. Yes, this repros with Roslyn outside Visual Studio Code.

Is it the splitting of attributes onto individual lines that's bothering you, or the moving of the variable declaration below the attributes (not in-line with [CurveRange])? If it's the former, you can use the [SerializeField, CurveRange] syntax instead and the formatter will leave them together.

jceipek commented 7 years ago

Thanks for checking @dpoeschl!

It's the moving of the variable declaration away from the line with attributes that bothers me. In Unity projects using C#, there are often many variables with multiple attributes in the same file, and I'd like to be able to read them all without scrolling vertically or horizontally while keeping variables visually associated with the attributes that affect their behavior.

If I use the comma syntax it is a bit better, but it's still visually harder to make this association than if the formatting were maintained.

Since #11685 was intended to "Keep attribute on the same line as a member if they started on a single line," (which is what I want) the current behavior seems like a bug to me.

dpoeschl commented 7 years ago

Yes, it does seem like a regression from the issue/PR you point out.

thorn0 commented 6 years ago

This issue has been fixed by #22192, hasn't it? Why is it still open?