Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell clang-format to indent visibility modifiers?

I want my visibility modifiers (public, protected and private) to be indented by clang-format who currently leaves them at the same level as the class declaration. I've looked for indent and visibility on a dump of the default format options but couldn't find anything.

like image 611
ruipacheco Avatar asked Mar 22 '15 19:03

ruipacheco


2 Answers

From the Clang-Format Style Options documentation:

AccessModifierOffset (int) The extra indent or outdent of access modifiers, e.g. public:.

So, add the appropriate entry to your .clang-format. For example,

AccessModifierOffset: 2
like image 152
Pradhan Avatar answered Oct 18 '22 22:10

Pradhan


If you want to give the access modifiers themselves their own level of indentation, you can use IndentAccessModifiers: true. This will give you code that looks like the following.

class my_class {
  public:
    my_class() = default;
};

With IndentAccessModifiers: false, by default you'll get the access modifiers not indented at all, and class members will be indented only one level beyond the surrounding scope.

class my_class {
public:
  my_class() = default;
};

You can then use AccessModifierOffset to adjust the alignment of the access modifiers only, without affecting the alignment of the class members. With IndentAccessModifiers: false and AccessModifierOffset: 1, you'd get this.

class my_class {
 public:
  my_class() = default;
};

With IndentAccessModifiers: true, AccessModifierOffset is ignored.

I'm sure all reasonable programmers would agree that only one of these options is even remotely acceptable. Though they probably wouldn't agree on which one it is.

like image 39
Sam Marinelli Avatar answered Oct 18 '22 22:10

Sam Marinelli