neovim.github.io icon indicating copy to clipboard operation
neovim.github.io copied to clipboard

style guide: Require trailing comma

Open ZyX-I opened this issue 9 years ago • 7 comments

Current style guide does not talk about trailing commas in array and struct braced initializer list format. The example shows that it should be omitted in struct initialization, but I think this is bad idea in struct initializers (except for one-line ones) and it is especially bad idea in array initializers. Reasoning is simple and known: should you append element to the list without the trailing comma you are forced make not only one addition (+n lines line after last array element), but also a change to the last line of the last array element.

Thus I would suggest the following addition to the style guide: require trailing comma always when closing brace is placed on the separate line and require absense of trailing comma when closing brace is placed on the same line as the last element in the initializer list. This change does not even go against the example in http://neovim.io/develop/style-guide.xml?showone=Braced_Initializer_Lists#Braced_Initializer_Lists.

ZyX-I avatar May 02 '15 01:05 ZyX-I

:+1:

Shougo avatar May 02 '15 02:05 Shougo

Sounds reasonable to me.

ghost avatar May 02 '15 02:05 ghost

This change does not even go against the example in http://neovim.io/develop/style-guide.xml?showone=Braced_Initializer_Lists#Braced_Initializer_List

@ZyX-I Would this be a correct example?

struct my_struct m = {  // Here, you could also break before {.
    superlongvariablename1,
    superlongvariablename2,
    { short, interior, list },
    { interiorwrappinglist,
      interiorwrappinglist2 }, 
  };

justinmk avatar May 03 '15 00:05 justinmk

@justinmk Yes, though I would prefer to keep the current one and add additional (unless you want to also require }; on the separate line as long as opening { is on the separate line from the first element):

struct my_super_struct m = {
    superlongvariablename1,
    { short, interior, list },
};

struct my_list_struct m[] = {
    { varname, { short, interior, list } },
    { superlongvariablename,
      { short, interior, list2 } },
};

(note: added example of the array).

ZyX-I avatar May 03 '15 09:05 ZyX-I

The existing example shows that

… = {
    … };

is allowed, changing it hangs this information (you don’t know whether it is allowed or not).

ZyX-I avatar May 03 '15 09:05 ZyX-I

Needlessly complicated, but sure. @justinmk: should we move this issue to the neovim repo if it's still relevant?

dundargoc avatar Sep 28 '21 16:09 dundargoc

should be a quick addition to :help dev-style, let's just close this when that is done

justinmk avatar Oct 03 '21 01:10 justinmk

This is enforced by our auto-formatter now:

https://github.com/neovim/neovim/blob/4b5ef0ff0aacff2d763f13fe50f9c2e97ffafd17/src/uncrustify.cfg#L3200

justinmk avatar Oct 02 '22 22:10 justinmk