csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Improve formatting of Field with Array Initializer

Open belav opened this issue 4 years ago • 5 comments

// currently formats like
        private static readonly string[] _defaultSources = new string[]
        {
            "https://api.nuget.org/v3/index.json"
        };
// but if it can fit onto two lines like this, that would be preferred
        private static readonly string[] _defaultSources =
            new string[] { "https://api.nuget.org/v3/index.json" };

belav avatar Jun 12 '21 16:06 belav

The code in RightHandSide is probably what needs to change, but there are a ton of edge cases that get ruined every time I attemp to figure out how to get this working.

belav avatar Aug 04 '21 23:08 belav

There is also the question of if

this should break, because it has multiple items
    private string[] SomeArray = new[]
    {
        "SomeValue_____________________",
        "SomeValue_____________________",
    };
// or if it should stick to one line, because it is short enough
    private string[] SomeArray =
        new[] { "SomeValue_____________________", "SomeValue_____________________", };

belav avatar Aug 04 '21 23:08 belav

Also note that prettier doesn't do this for objects or arrays

var someSemiLongName_____________ = {
  one: "one_____________________________________",
};
var someSemiLongName_____________ = [
  "one_____________________________________",
];

belav avatar Aug 04 '21 23:08 belav

As of 8/7 with the changes for #345, this can be done by printing ArrayCreationExpressionSyntax with Layout.BreakAfterOperator in RightHandSide

But that means we get this, which we don't want

    int[] array =
        new int[]
        {
            "someLongValue_____________________________________",
            "someLongValue_____________________________________"
        };

We could possibly try to determine the length of the right side and decide that way. Prettier does something like that in assignment.js.

belav avatar Aug 07 '21 22:08 belav

#269 is also related to this

belav avatar Aug 07 '21 22:08 belav