Search code examples
clang-format

How to Change clang-format Rule for a Specific Code Section?


Is it possible to change a clang-format rule for a code section? I know that you can add a comment to disable formatting completely for a section via // clang-format off, but is it also possible to change a specific rule?

enum class Foo {
    // clang-format AlignConsecutiveAssignments: Consecutive
    ABC = 0,
    X   = 1
};

My overall goal is to align the equal signs in an enum, but not in the rest of the code. With AlignConsecutiveAssignments: Consecutive, clang-format aligns variable assignments as well.


Solution

  • Unfortunately, overriding styles for a section of code isn't possible. Source

    You could try organizing your project to use different .clang-format's, as described here.