Aart Stuurman
Aart Stuurman

Reputation: 3628

Clang format unwanted indentation

I'm automating enforcing coding style using clang-format. Everything formats correctly, except for one unwanted artifact. The reduced version of my .clang-format config file is:

ColumnLimit: 120
Cpp11BracedListStyle: false
IndentWidth: 4
Standard: Auto
TabWidth: 4
UseTab: ForIndentation

The code in question after formatting is:

static struct option long_options[] = { { "str1", no_argument, NULL, 'h' },
                                            { "str2", no_argument, NULL, 'v' },
                                            { "str3", required_argument, NULL, 's' },
                                            { NULL, 0, NULL, 0 } };

It is hard to display on Stackoverflow, but this is how it displays using 8 column tabs. All whitespace is spaces, except for the first character, which is a tab(but displays here as 8 spaces). So:

static struct option long_options[] = { { "str1", no_argument, NULL, 'h' },
<__tab_>                                    { "str2", no_argument, NULL, 'v' },
<__tab_>                                    { "str3", required_argument, NULL, 's' },
<__tab_>                                    { NULL, 0, NULL, 0 } };

This is not the formatting I'm looking for. Displaying with 4 column tabs gives the output I expect:

static struct option long_options[] = { { "str1", no_argument, NULL, 'h' },
<tb>                                    { "str2", no_argument, NULL, 'v' },
<tb>                                    { "str3", required_argument, NULL, 's' },
<tb>                                    { NULL, 0, NULL, 0 } };

However, I feel like there should not be a tab at all, and the alignment should be done by spaces alone, preventing the whole display tab size problem.

How can I setup clang-format to solve this?

Upvotes: 0

Views: 1389

Answers (1)

Aart Stuurman
Aart Stuurman

Reputation: 3628

My solution was to use Cpp11BracedListStyle: true and UseTab: ForIndentation. This slightly changes to the look of braced lists, but forces the use of spaces when aligning them.

Upvotes: 2

Related Questions