csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Suggestion: Avoid breaking Attributes with only one parameter

Open timlindeberg opened this issue 1 year ago • 2 comments

Input:

[Obsolete("Will be removed in a future version. Please replace any use of this class with some other class.")]

Output:

[Obsolete(
    "Will be removed in a future version. Please replace any use of this class with some other class."
)]

Expected behavior:

[Obsolete("Will be removed in a future version. Please replace any use of this class with some other class.")]

For cases like this breaking the statement up into multiple lines is not really helpful even if it surpasses the maximum line length. It's not possible to break up the string expression so you only end up indenting it slightly less and using three lines instead of one without really reducing the length of the line.

Could there maybe be an exception for attributes or methods with a single argument where the argument itself surpasses the line length? It could be limited to attributes but I've also seen method calls like this where it might make more sense to keep everything on one line:

Debug.Log(
     "A veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery long string"
);

Thanks for the great work on CSharpier! :)

timlindeberg avatar Oct 23 '24 13:10 timlindeberg

Great suggestion, I've gotten a bit annoyed with attributes breaking this way.

Treating method calls the same way seems reasonable. I feel like I may have implemented a similar rule for an if condition when it only contains a single operator.

The csproj/xml formatting I have in progress has similar rules about not breaking an empty element with a single attribute, and an element with no attributes and just a single line of text within it.

belav avatar Oct 23 '24 14:10 belav

I would agree that multiline attributes is kinda annoying. My personal opinion is usually keep them one line.

However, for csharpier, would the rule be that IF the attribute line is longer than max length AND after breaking onto multiple lines there is an overflowing line (e.g. the string), then just keep it all on one line?

Hona avatar Oct 23 '24 22:10 Hona