How edit clang-format to allow whitespace padding?

Example from the title:

I have this declaration:

int8_t SW_ROW[24] = {1, 1, 1, 1, 0, 0, 0, 0, 5, 5, 5, 5,
                     20, 20, 20, 20, 19, 19, 19, 19, 18, 18, 18, 18};

and for better readability, I want to add spaces to look like this:

int8_t SW_ROW[24] = { 1,  1,  1,  1,  0,  0,  0,  0,  5,  5,  5,  5,
                     20, 20, 20, 20, 19, 19, 19, 19, 18, 18, 18, 18};

I have not found an entry in clang-format to allow this. Can it be done?

The whitespace in this example have no effect on compiling. If you format, you probably will loose the whitespace.

Yes, it compiles fine, but I like to use the Cntrl+T Auto Format in the IDE to neaten up other things, and when I do that, it removes the whitespace/padding, like you say. That's what I want to avoid. I want the whitespace/padding to survive a Cntrl+T formatting.

I'm looking for something to edit in the clang-format file to allow whitespace. I have not found it yet.

I did find this, which makes a list of declarations a bit neater:

#AlignConsecutiveDeclarations: None
AlignConsecutiveDeclarations: true

I use 3-digit decimal with byte types (0 - 255) and prefix/pad with zeroes for visual alignment.

Hmm, I think that the compiler will complain with certain values or you get the wrong ones :smiley:

010 is not 10 for the compiler and 008 will result in a warning or error.

Leading zeroes indicate octal numbers.

Yes, there may have been compile warnings. I was making RGB colors and wanted the code to be easy-on-my-eyes to spot offending values.

Yes, I just tried padding with leading zeroes, and it took the values as OCTAL, and the compiler complained. But it wasn't so obvious at first...

I thought I had it, because I was using uint8_t and as long as the digits were < 8, the compiler assumed I knew what I was doing. In my first trial, I had only entered things like 000, 001, 010, 123, so the compiler was fine. Good thing I later tried on 080 before I got too excited! :slight_smile:

I really like to line up numbers like this, makes it so much easier to spot a typo, or other issue, and makes it far clearer in some cases. Ought to be a way. Other than keeping a plain text version, and copy back/forth?

OK, a solution of sorts, more of a band-aid, but it gets the job done...

Adding these comments turns the formatting off/on. So do Cntrl-T for the basics, then comment out the formatting and add the leading zeroes, or any spaces you want.

// clang-format off

int8_t SW_ROW[24] = {  1,  1,  1,  1,  0,  0,  0,  0,  5,  5,  5,  5,
                      20, 20, 20, 20, 19, 19, 19, 19, 18, 18, 18, 18};

// clang-format on

edit/add: I found this solution here:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.