belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

Inconsistent formatting #1247

Closed Lowfeye closed 6 months ago

Lowfeye commented 6 months ago

Input:

_gradient.SetKeys(
    new GradientColorKey[] { new(Color.blue, 0.0f), new(Color.green, 0.5f), new(Color.red, 1.0f) },
    new GradientAlphaKey[] { new(1.0f, 0.0f), new(1.0f, 0.5f), new(1.0f, 1.0f) });

Output:

_gradient.SetKeys(
    new GradientColorKey[]
    {
        new(Color.blue, 0.0f),
        new(Color.green, 0.5f),
        new(Color.red, 1.0f)
    },
    new GradientAlphaKey[] { new(1.0f, 0.0f), new(1.0f, 0.5f), new(1.0f, 1.0f) }
);

Expected behavior:

_gradient.SetKeys(
    new GradientColorKey[]
    {
        new(Color.blue, 0.0f),
        new(Color.green, 0.5f),
        new(Color.red, 1.0f)
    },
    new GradientAlphaKey[] 
    { 
        new(1.0f, 0.0f),
        new(1.0f, 0.5f),
        new(1.0f, 1.0f)
    }
);
belav commented 6 months ago

The first parameter is long enough that it breaks, the second parameter fits within the width and nothing is forcing it to break.

I could see having a rule for "collection initializers that call constructors of objects should automatically break if there are 3 or more objects in the initializer". Which would result in

// input 
var array = new SomeObject[] { new(1, 0), new(1, 0), new(1, 0) };

// output
var array = new SomeObject[] 
{ 
    new(1, 0),
    new(1, 0),
    new(1, 0)
};

Object initializers have a similar rule and auto break with 3+ properties.


var x = new X
{
    x = 1,
    y = 2,
    z = 3
};
Lowfeye commented 6 months ago

I thought it was two consecutive arrays that caused no line breaks. Now this is OK, thank you.