markw65 / prettier-extension-monkeyc

A VSCode extension for Garmin MonkeyC
MIT License
11 stars 1 forks source link

How to disable adding "trailing" commas while formatting Dictionaries and Arrays? #1

Closed karol-brejna-i closed 2 years ago

karol-brejna-i commented 2 years ago

When formating monkey C array, the following:

    static var bitmaps = [
        [
            WatchUi.loadResource($.Rez.Drawables.ArrowL1),
            WatchUi.loadResource($.Rez.Drawables.ArrowL2)
        ],
        [
            WatchUi.loadResource($.Rez.Drawables.ArrowR1),
            WatchUi.loadResource($.Rez.Drawables.ArrowR2)
        ]
    ];

becomes:

    static var bitmaps = [
        [
            WatchUi.loadResource($.Rez.Drawables.ArrowL1),
            WatchUi.loadResource($.Rez.Drawables.ArrowL2),     <-- here
        ],
        [
            WatchUi.loadResource($.Rez.Drawables.ArrowR1),
            WatchUi.loadResource($.Rez.Drawables.ArrowR2),    <-- here
        ],    <-- here
    ];

Similarly, for dict:

    public function initialize(
        params as {
            :dir as Integer,
            :locX as Number,
            :locY as Number,
            :speed as Number
        }
    )

becomes:

    public function initialize(
        params as {
            :dir as Integer,
            :locX as Number,
            :locY as Number,
            :speed as Number,       <-- here
        }
    )

Is there a way to disable this feature?

markw65 commented 2 years ago

Yes, that's one of the few configuration options prettier provides. See https://prettier.io/docs/en/options.html

If you're only using prettier inside of vscode, you can just go to settings, type "Trailing Comma" into the search box, and the option should appear. Set it to "none".

But note that generally, the preferred way to set prettier's options is via a config file (see https://prettier.io/docs/en/configuration.html), so that prettier behaves the same whether you run it from inside vscode, or from the command line.

By the way, the main reason for adding the trailing commas is source control: if you add more items at the end of the list, and you always have trailing commas, then the only affected lines are the new ones. If you don't add the trailing comma, then the last existing line is also modified, making it look like more lines were changed than was really the case. Similarly for deleting lines.

This makes me realize there is a potential problem though... setting trailing-comma to "all" would add trailing commas at the end of function call parameter lists - which wouldn't compile. And worse, my parser wouldn't accept it either, so it wouldn't be able to undo the change, even after switching the option back. There's a similar problem with the "semicolon" option. I'll fix those...

markw65 commented 2 years ago

I pushed version 1.0.8 which doesn't allow options.trailingComma=all or options.semi=false, and which can fix the effects of using options.trailingComma=all with an earlier version of the plugin.