Can clang-format align variable or macro assignments in columns?

syvex picture syvex · Nov 24, 2014 · Viewed 7.9k times · Source

Is it possible to have clang-format align variable assignments in columns? For example:

int someInteger             = 42;
std::string someString      = "string";
const unsigned someUnsigned = 42;

#define SOME_INTEGER        42
#define SOME_STRING_LITERAL "string"
#define SOME_CONSTANT       42

enum Enum {
    ONE   = 1,
    TWO   = 2,
    THREE = 3,
    FOUR  = 4,
    FIVE  = 5,
    SIX   = 6,
    SEVEN = 7
};

is more readable than:

int someInteger = 42;
const unsigned someUnsigned = 42;
std::string someString = "string";

#define SOME_INTEGER 42
#define SOME_STRING_LITERAL "string"
#define SOME_CONSTANT 42

enum Enum {
    ONE = 1,
    TWO = 2,
    THREE = 3,
    FOUR = 4,
    FIVE = 5,
    SIX = 6,
    SEVEN = 7
};

I realize that it may not be practical for clang-format to always do this, but when code as already been manually formatted like said code, it would be nice for clang-format to leave the formatting in place.

Answer

Michael Anderson picture Michael Anderson · Jun 16, 2015

It looks like 3.7 supports something like this (haven't tested yet).

From the docs

AlignConsecutiveAssignments (bool)
If true, aligns consecutive assignments.

This will align the assignment operators of consecutive lines. This will result in formattings like code int aaaa = 12; int b = 23; int ccc = 23; endcode

(sic)