0

I'm trying format one c header file. While struct idents is equals to class variables.

test.h:

class MyClass{
    public:
        int var;
        int var2;
}

struct myst {
    int foo;
    int bar;
};

Style Config:

TabWidth: 4
IndentWidth: 4
UseTab: Always
IndentAccessModifiers: true

Run:

clang-format -style="{TabWidth: 4, IndentWidth: 4, UseTab: Always, IndentAccessModifiers: true}" test.h

The following output is bad. (in struct is must be one tab not more!)

class MyClass {
    public:
        int var;
        int var2;
}

struct myst {
        int foo;
        int bar;
};

Also like this output:

class MyClass {
    public:
        int var;
        int var2;
}

struct myst {
    int foo;
    int bar;
};

1 Answers1

-1

Follwing Sam Marinelli's answer

According to the documentation using IndentAccessModifiers will result in record members always being indented two levels. It's currently not possible to indent in two different use-cases (with and without access modifiers)

What you can do is change the indentation of the access modifiers themselves.

Looking at your target output we have the following:

  • Your base offset is 6. This will result in IndentWidth: 6
  • Your modifiers are offset as 4, so we will use a negative offset to unindent them, resulting in AccessModifierOffset: -2
  • AccessModifierOffset is ignored when IndentAccessModifiers is used. So we need to remove that line or set it to false.

All together this will result in the following:

Style Config:

AccessModifierOffset: -2
TabWidth: 4
IndentWidth: 6
UseTab: Always
IndentAccessModifiers: false

Output:

class MyClass {
    public:
      int var;
      int var2;
}

struct myst {
      int foo;
      int bar;
};

As an alternative you can also use IndentWidth: 4 to get this result:

class MyClass {
  public:
    int var;
    int var2;
}

struct myst {
    int foo;
    int bar;
};
Petzep
  • 1
  • 3