Home > Enterprise >  VSCode removing spaces with auto format (C/C )
VSCode removing spaces with auto format (C/C )

Time:01-01

I recently migrated to VSCode and I really like the auto formatting, but how do I configure it not to remove spaces? I have a sort of matrix I use to configure GPIO pins and I use spaces to make it more readable, but as soon as I save and it auto formats, it destroys the spacing!

example:

_InitIO(GPIOA, (GPIO_IN  | GPIO_DN                               ),  //  A0    IRQ-Mag
               (GPIO_IN  | GPIO_DN                               ),  //  A1
               (GPIO_AF7           | GPIO_PP | GPIO_HIGH | GPIO_1),  //  A2
               (GPIO_IN  | GPIO_UP                               ),  //  A3    Switch

Turns into:

_InitIO(GPIOA, (GPIO_IN | GPIO_DN),                //  A0    IRQ-Mag
                (GPIO_IN | GPIO_DN),                       //  A1
                (GPIO_AF7 | GPIO_PP | GPIO_HIGH | GPIO_1), //  A2
                (GPIO_IN | GPIO_UP),                       //  A3    Switch

I have been over all of the formatting options in the settings but can't seem to find it...

CodePudding user response:

VSCode looks for a file named .clang-format in the workspace directory. See: https://code.visualstudio.com/docs/cpp/cpp-ide#_code-formatting

In order to change the auto-format style, you'll have to create the .clang-format file and modify it according to the Clang-Format Style Options.

You may have to turn autoformatting off or continuously save the file without formatting using the File: Save without Formatting command through the Command Palette or by using the Ctrl K Ctrl Shift S key combination.

CodePudding user response:

Alternative

Avoid code art

Sometimes its nice to form a pretty table in code, yet to keep that up is maintenance. With good software, maintenance is the most expensive part, so if the code art is difficult to maintain with basic auto-formatting, its value goes down. It only takes one subsequent maintainer to not have the needed auto format settings to mess things up.

Avoid code art that is difficult to keep with standard auto-format settings.

How about the below? I suspect it will maintain its format with VS and be close enough to meet OP's code art.

  _InitIO(GPIOA, // 
      (GPIO_IN | GPIO_DN),                       //  A0 IRQ-Mag
      (GPIO_IN | GPIO_DN),                       //  A1
      (GPIO_AF7 | GPIO_PP | GPIO_HIGH | GPIO_1), //  A2
      (GPIO_IN | GPIO_UP)                        //  A3 Switch
      )
  • Related