9

Sometimes clang-format does this :

SomeType VariableName[] = {Thing1,
                           Thing2,
                           Thing3}

and sometimes clang-format does this :

SomeType VariableName[] = {
   Thing1,
   Thing2,
   Thing3}

and a single character change can make it switch between.

Is there any way to control which it does?

I'm building from the latest git source, so the latest options are available.

Andy Jewell
  • 413
  • 3
  • 12
  • Could you give more information about what kind of single character change could make it switch between? Such information will be helpful to me to find out the answer. Thanks. – Kun Ling May 19 '15 at 21:46
  • I tried to create a little tiny case for this question, but it works fine without problem. – Kun Ling May 19 '15 at 21:51
  • The single character change was changing PointerAlignment from Right to Middle. – Andy Jewell May 20 '15 at 12:18
  • I don't understand "works fine without problem". I wasn't claiming that there was a bug. It's just that I would prefer to always get the second style. – Andy Jewell May 20 '15 at 12:20

2 Answers2

1

According to this answer, clang-format in some step puts as much as possible on a single line, and applies the ColumnLimit on that.

That would explain the switching between behaviors.

One way to prevent this could be to set ColumnLimit to 0, with the cost of removing all automatic wrapping. I think it's worth the cost, I'm sure other don't agree.

Community
  • 1
  • 1
Gauthier
  • 40,309
  • 11
  • 63
  • 97
-3

clang-format provide a way for user to specify a single property when format the code, such as whether tab is allowed, what is the tabwidth.

You could use the following way to tell clang-format to use a customized property.

  $clang-format -style="{BaseonStype: llvm, IndentWidth: 8}"
  $clang-format -style=HAND_WRITTEN_FORMAT_FILENAME
  $clang-format -style=llvm  #builtin styles.

You could get an idea about what property you could customize in the file from line 171 to line 266.

Kun Ling
  • 2,211
  • 14
  • 22
  • I know how to change the properties. I've studied http://clang.llvm.org/docs/ClangFormatStyleOptions.html carefully, and it seems to be up to date with respect to the source. No property seems to control this behavior, although a smaller ColumnLimit will often change it from the first style to the second style. – Andy Jewell May 20 '15 at 12:26