clang-format removes new lines in array definition with designators

我只是一个虾纸丫 提交于 2020-01-11 10:19:31

问题


I like to define my array values with a designator, when possible:

enum Mode {
    NONE,
    SPLIT_FILES,
    SINGLE_FILE,
    INVALID
};

const std::string ModeName[] = {
    [NONE] = "NONE",
    [SPLIT_FILES] = "SPLIT_FILES",
    [SINGLE_FILE] = "SINGLE_FILE",
    [INVALID] = "INVALID"
};

Running this through clang-format (3.5) mangles the new lines and makes it less readable:

enum RecorderMode { REC_NONE, REC_SPLIT_FILES, REC_SINGLE_FILE, REC_INVALID };

const std::string RecorderModeName[]
    = {[REC_NONE] = "NONE", [REC_SPLIT_FILES] = "SPLIT_FILES", [REC_SINGLE_FILE] = "SINGLE_FILE",
       [REC_INVALID] = "INVALID" };

The array definition has several problems: = { is moved to the next line. If I add a comma after the last array entry, the rows are indented twice.

Is there a way to keep the new lines and indentation, short of using the clang-format turn off comment?

This shows a work-around for the enum (add a comma after the last constant, or add a trailing comment after a comma), but it doesn't seem to apply to the array.


回答1:


This answer gave me an acceptable work-around:

Set ColumnLimit to 0. The trade-off is that no line is automatically wrapped, but this is worth it. All programmers at work tend to not write past column 120 anyway.




回答2:


Comments on every line also works.

enum Mode {
  NONE,         // Comments
  SPLIT_FILES,  // On
  SINGLE_FILE,  // Every
  INVALID       // Line
};


来源:https://stackoverflow.com/questions/39144255/clang-format-removes-new-lines-in-array-definition-with-designators

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!