Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto indent a C++ class with 4 spaces using clang-format?

I got the next .clang-format file in my project's root directory:

--- AlignTrailingComments: true AllowShortFunctionsOnASingleLine: false AllowShortIfStatementsOnASingleLine: true AllowShortLoopsOnASingleLine: true BreakBeforeBinaryOperators: false IndentWidth: 4 SortIncludes: false NamespaceIndentation: All ... 

Problem comes when I run clang-format on my c++ headers, the classes become autoindented like this:

enter image description here

As you can see, labels public & private are indented only with 2 spaces. But what I'm trying to achieve is the below output (indentation was manually tweaked):

enter image description here

That way code-collapsing becomes something really pleasant to do.

How could I tweak my .clang-format to achieve this effect? If not possible, how would you patch clang-format source code to achieve this desired behaviour?

EDIT:

I've tried using unsuccessfully AccessModifierOffset, I've used values {-2,0,2,4} example below:

enter image description here

As you can see the statement inside the public block won't be indented properly.

EDIT2:

I've tried the @Henrique Jung solution and that's definitely not what I'm asking for, if using that combination the result would be something like this one:

enter image description here

And as you can see, the content inside the functions are indented 8 spaces instead 4, which is not good.

EDIT3:

I gave a bounty few months ago so I'm going to try again as this one is definitely interesting. If I got enough knowledge about clang-format source code I'd give it a shot, unfortunately I don't.

like image 689
BPL Avatar asked Mar 15 '17 00:03

BPL


People also ask

Does clang-format work for C?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.

How do you customize your clang-format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.


2 Answers

As near as I can tell, clang-format offers no option for indenting function contents differently from non-access-modifier class contents. That is, consider the following code:

class A {   public:     void foo() {} }  void bar() {     int a; } 

In this code, the line "void foo() {}" will always be indented the same amount as "int a;" by clang-format.

The closest thing to the style you seem to want that is available would come from not indenting the access modifiers, e.g.:

class A { public:     void foo() {} }  void bar() {     int a; } 

This is done, for example, by the WebKit, Mozilla, and LLVM styles. It's achieved by setting:

IndentWidth: 4 AccessModifierOffset: -4 
like image 133
Kimby Avatar answered Oct 08 '22 14:10

Kimby


I managed to achieve the effect you want by changing both AccessModifierOffset with IndentWidth. Basically, the first is used as an offset of the second, so if you create your .clang-format like this you get what you want:

AccessModifierOffset: -4 IndentWidth:     8 

If AccessModifierOffset is 0, the public keyword would be at the same level of indentation as the members. However, changing IndentWidth will indent all code by 8 spaces, even those outside the class declaration. This is a sample code:

class Foo {     public:         Foo();         virtual ~Foo(); };  int main(int argc, char *argv[]) {         std::cout << "Hello world" << std::endl;         return 0; } 
like image 22
Henrique Jung Avatar answered Oct 08 '22 14:10

Henrique Jung