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

Ignore and decorators #848

Closed
Billuc opened this issue Mar 8, 2023 · 1 comment · Fixed by #855
Closed

Ignore and decorators #848

Billuc opened this issue Mar 8, 2023 · 1 comment · Fixed by #855
Milestone

Comments

@Billuc
Copy link

Billuc commented Mar 8, 2023

Hi (again),
In a Xunit test class, I wanted to ignore formatting on decorators because it made the class unnecessarily ugly.
However, // csharpier-ignore seem to have no effect and ranged ignore seem to disable formatting for the rest of the document.
I guess ignore on decorators isn't implemented yet, is it ?

Here is some sample code I played with in the playground :

Without ignore, makes the result really long

public class ClassName {
    public string SomeProperty
    { get;
    set; }

    [Theory]
    [InlineData(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0 })]
    public void TestSomething(double[] values) {
        this.LongUglyMethod("1234567890", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }
}

With ranged ignore, doesn't format the method

public class ClassName {
    public string SomeProperty
    { get;
    set; }

    // csharpier-ignore-start
    [Theory]
    [InlineData(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0 })]
    // csharpier-ignore-end
    public void TestSomething(double[] values) {
        // this line should be formatted
        this.LongUglyMethod("1234567890", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }
}

With single-line ignore, doesn't have any effect

public class ClassName {
    public string SomeProperty
    { get;
    set; }

    [Theory]
    // csharpier-ignore
    [InlineData(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0 })]
    public void TestSomething(double[] values) {
        this.LongUglyMethod("1234567890", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    }
}
@belav
Copy link
Owner

belav commented Mar 8, 2023

CSharpier-ignore (and ranges) only works in some contexts because there was no generic way to implement it. I started with the basic cases and planned on expanding support as people ran into other areas that it made sense so be able to ignore.

The ignore-start in the first example is probably a leading trivia on the method itself, so the current code is expecting the end to appear somewhere else within the members of the class. But the nodes for the attribute are children of the method declaration. I may just leave that as is if it isn't an easy fix. The range ignore code is a bit messy if I remember correctly.

In the second example csharpier doesn't look for ignores on attribute nodes, so the only place it ends up working is before the first attribute because that is considered leading trivia of the method declaration. That then results in no formatting on the attributes nor on the method. I'll work on getting it to behave like this

// csharpier-ignore - only the first attribute
[Attribute          ]
[Attribute]
public void MethodThatShouldFormat() { }

[Attribute]
// csharpier-ignore - only the second attribute
[Attribute         ]
public void MethodThatShouldFormat() { }

[Attribute]
[Attribute]
// csharpier-ignore - just the method
public void MethodThatShouldFormat(           ) { }

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

Successfully merging a pull request may close this issue.

2 participants