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

csharpier does not respect editorconfig specific c# rules #1214

Closed
ghost opened this issue Mar 22, 2024 · 2 comments · Fixed by #1221
Closed

csharpier does not respect editorconfig specific c# rules #1214

ghost opened this issue Mar 22, 2024 · 2 comments · Fixed by #1221
Milestone

Comments

@ghost
Copy link

ghost commented Mar 22, 2024

Issue

Csharpier fails to adhere to the specified guidelines for .cs files.

.editorconfig

root = true

[*]
charset = utf-8
tab_width = 2
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 200

[*.{cs}]
indent_size = 4
tab_width = 4

Bonus: We do use [*.{cs,csx}] and [*.{cs}] sometimes. It would be nice if the "more specific" rule takes precedence. eg [.{cs}] is used ultimately for formatting.
But probably an override mechanism is necessary: base editor config rules + override with [*.{cs,csx}] + override with [*.{cs}], which results in formatting with indent_size = 4

Example:

[*]
charset = utf-8
tab_width = 2
indent_size = 2

[*.{cs,csx}]
indent_size = 3
tab_width = 3

[*.{cs}]
indent_size = 4
tab_width = 4

This example results in formatting with indent_size = 4

Input:

namespace X;

public class Resolver
{
    private const int SinglePhase = 1;

    public int Resolve(object source)
    {
        if (source is not string)
        {
            return default;
        }

        return 0;
    }
}

Output:

namespace X;

public class Resolver
{
  private const int SinglePhase = 1;

  public int Resolve(object source)
  {
    if (source is not string)
    {
      return default;
    }

    return 0;
  }
}

Expected behavior:

Csharpier does not change Input. Formatting Input results in Input

@belav belav added this to the 0.28.0 milestone Mar 22, 2024
@belav
Copy link
Owner

belav commented Mar 22, 2024

I've narrowed this down to CSharpier not working with braces in the glob, which means a pattern like this will not work [*.{cs}], but [*.cs] does.

For this example

[*]
charset = utf-8
tab_width = 2
indent_size = 2

[*.{cs,csx}]
indent_size = 3
tab_width = 3

[*.{cs}]
indent_size = 4
tab_width = 4

According to the editorconfig spec, the most recent rule is what should be used. Which would result in an indent size of 4.

Which means for this example, indent size of 3 should be used for *.cs files even though the last rule isn't as specific. I imagine trying to code for most specific rule would be problematic.

[*]
charset = utf-8
tab_width = 2
indent_size = 2

[*.{cs}]
indent_size = 4
tab_width = 4

[*.{cs,csx}]
indent_size = 3
tab_width = 3

belav added a commit that referenced this issue Mar 22, 2024
…orconfig needs. Customized GlobMatcher to allow single brace sets like [*.{cs}] and treat them like [*.cs]

closes #1214
@ghost
Copy link
Author

ghost commented Mar 25, 2024

I agree sticking to the editorconfig specification is the way to go

belav added a commit that referenced this issue Apr 7, 2024
* Pull in new way to parse globs, dotnet.glob did not support what editorconfig needs. Customized GlobMatcher to allow single brace sets like [*.{cs}] and treat them like [*.cs]

closes #1214

* Make these internal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant