haxe-formatter icon indicating copy to clipboard operation
haxe-formatter copied to clipboard

Options for trailing commas

Open andyli opened this issue 4 years ago • 5 comments

I would like to specify whether a trailing comma should be added for the last array element.

e.g.

// no trailing comma
[
    1,
    2,
    3
];

// with trailing comma
[
    1,
    2,
    3,
];

Not sure if it's already possible?

andyli avatar Jan 07 '20 11:01 andyli

The formatter does currently not insert or remove tokens.

Gama11 avatar Jan 07 '20 11:01 Gama11

Do you mean it's not possible to implement (without major changes)?

andyli avatar Jan 07 '20 11:01 andyli

Not sure about "major" changes, @AlexHaxe probably has a better idea on that, but definitely not as straightforward as it might seem (ranges of subsequent tokens would have to be updated at the very least).

Gama11 avatar Jan 07 '20 12:01 Gama11

Currently formatter only deals with removal or addition of whitespace. It doesn't rearrange, add or delete tokens from your sources. Introducing a new token into or making edits to a parsed tokentree involves at least a) finding the correct spot and b) correcting token list and index values. Otherwise successive formatter stages will run into issues.

I would say it's not impossible to implement, but it might take some time to get it right.

Also I feel like that feature is more checkstyle territory. It would be a separate check which produces a diagnostic in VSCode, and a code action in checkstyle extension could offer a fix. Of course that works only on a file by file basis, where you have to open each file and apply fixes manually. I guess such a check makes sense independant of whether formatter learns that trick or not.

AlexHaxe avatar Jan 07 '20 16:01 AlexHaxe

For what it's worth I agree that this is more of a checkstyle thing. I would keep haxe-formatter a whitespace rearrangement tool for the sake of the author's sanity.

Simn avatar Feb 17 '20 12:02 Simn