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.03k stars 4.03k forks source link

Bug: Field comments don't work with tabs. #72966

Open hamarb123 opened 6 months ago

hamarb123 commented 6 months ago

Version Used: VS 17.9.5, roslyn 4.9.0-3.24121.1 (a98c90d5); AND VS 17.10.0 Preview 3.0, roslyn 4.10.0-3.24202.15 (cbca41ca).

Steps to Reproduce:

  1. Make a c# class library project.
  2. Put this in the .editorconfig for the solution:
    [*.cs]
    dotnet_diagnostic.IDE0055.severity = error
    indent_style = tab
  3. Put this in a c# file:
    namespace ClassLibrary1
    {
    public enum Class1
    {
        a, //a
        //b,
        c,
    }
    }

    (make sure it uses tabs, and not spaces)

Diagnostic Id:

IDE0055 fix formatting. (move //b, to align with //a)

Expected Behavior:

No diagnostic.

Actual Behavior:

Error.

Additional information:

See https://github.com/dotnet/roslyn/issues/29482 for the feature request

Also, if you replace the tabs with spaces, and update the .editorconfig appropriately, you get no error (as per the original feature request) - it only occurs with tabs.

This is a blocker for me with enabling proper formatting rules in my projects.

/cc @CyrusNajmabadi who was discussing this with me on discord

sharwell commented 6 months ago

@CyrusNajmabadi I'm not able to find a reference to the pull request(s) where #29482 or #65247 were fixed. Can you help with that?

sharwell commented 1 month ago

Additional notes

Here's the line of code that skips comment formatting if the current indentation is considered "correct": https://github.com/dotnet/roslyn/blob/21499087d8dbb2cacd603c98531d37a298131ae1/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Engine/Trivia/TriviaDataFactory.CodeShapeAnalyzer.cs#L197

Here's the line of code preventing the previous line of code from working when UseTabs is true: https://github.com/dotnet/roslyn/blob/21499087d8dbb2cacd603c98531d37a298131ae1/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Engine/Trivia/TriviaDataFactory.ComplexTrivia.cs#L78-L80

New questions

The above code is 10 years old, so it doesn't explain why #29482 was filed as a bug, or what ended up fixing that.