2

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.

Community
  • 1
  • 1
Gauthier
  • 40,309
  • 11
  • 63
  • 97
  • 2
    I've become a fan of [uncrustify](https://github.com/uncrustify/uncrustify) after finding clang-format wanting in so many ways. It's not perfect, but at least it's constantly progressing. With no input uncrustify will not change your layout; however, there are [9 minor defaults](https://github.com/uncrustify/uncrustify/blob/master/documentation/htdocs/configuration.txt) (line 25). To make all enums look how you want you can use `nl_enum_own_lines = force`. I'm still learning, so I'm not sure about arranging the array def how you prefer yet though. But again it will leave it alone by default. – Novice C Sep 08 '16 at 16:22
  • @NoviceC: more than a year later, and I stubbled on uncrustify after rage-quitting on GNU indent and clang-format. I love it, it's so much better! I wish I tried it when you suggested it a year ago. You're maybe much better at it now, but if not let me suggest [universalindentgui](http://universalindent.sourceforge.net/). It shows all options, and their effect in real time. – Gauthier Nov 14 '17 at 08:03

2 Answers2

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.

Community
  • 1
  • 1
Gauthier
  • 40,309
  • 11
  • 63
  • 97
0

Comments on every line also works.

enum Mode {
  NONE,         // Comments
  SPLIT_FILES,  // On
  SINGLE_FILE,  // Every
  INVALID       // Line
};
13rac1
  • 1,057
  • 11
  • 14