Getting VSCode autoformat feature to match WPILib/Github code autoformat feature

When you press SHIFT+ALT+F on a windows, VS Code will autoformat your code. This is first thing I do when troubleshooting code as the indents help a lot when trying to work out where brackets are supposed to go.

The question is how do you make the Auto Format feature on VSCode match the autoformat style that is used in the WPILib samples and WPILib library code on github?

The main difference for me is, in WPILib code, the starting { in a code block is not pushed down to the next line by itself. Where as VScode puts it down on the next line. I prefer it to be on the same line as the method definition or if statement block, etc, etc.

I’ve seen something somewhere about getting github to format your code by adding the string /format to the pull request description, but what I’m looking for is how to make VScode do the same style of formatting at github.

The answer is spotless/wpiformat (depending on language). See this: Using a Code Formatter — FIRST Robotics Competition documentation
(You can use it standalone, but with a plug-in like Spotless Gradle - Visual Studio Marketplace it’s convenient)

1 Like

I did the pip3 install wpiformat
Created the 3 text files and stored them in the root project folder.

Then ran wpiformat from the console.
Then got a bunch of errors stating
Error: clang-format not found in PATH. Is it installed?

After going back through the instructions I see that I needed to install clang-format (included with LLVM) and then I re-read the big important message in the green box saying, don’t do as it will break everything.

So, are there any work arounds or is it just not achievable for the C++ language autoformatting on Windows computers?

Manually putting just the clang binary in your path will work.

In VS Code, you can customize the formatter by going to File > Preferences > Settings, and then navigating to Extensions > C/C++ > Formatting

The default seems to be to use clang-format with the settings in .clang-format in the project root directory. So it’s probably as simple as putting the wpilib .clang-format file there.

1 Like

Since the C++ extension installs it’s own version of clang-format (14.0.6) you can probably just add C:\Users\Public\wpilib\2023\vscode\data\extensions\ms-vscode.cpptools-1.13.8\LLVM\bin to the path without installing anything extra.

1 Like

Sorry for the repeated questiones. I have no idea what all this clang-format stuff means.

I added C:\Users\Public\wpilib\2023\vscode\data\extensions\ms-vscode.cpptools-1.13.8\LLVM\bin to the path environment variable and rebooted and relaunched VS Code.

The first time I started VS Code a message along the lines of “There is no formatter for cpp files installed” popped up with an option to close or install one. I closed it. That message hasn’t reappeared again.

I did a SHIFT+ALT+F on one of the files in the project and it still formatting the old way (ie { on line by itself), but now I have 1 problem on the file.
command-line error: language modes specified are incompatible its at line 1 col 1 on the file I ran SHIFT+ALT+F on.

I had a look in the VS code setting you mentioned and there are a gazillion of them, I’m not sure where I’m supposed to put wpilib .clang-format

Here is a screen shot of the first few setting

Do I still need those three files mentioned in the wpilib docs on formatiing in the root of the project?

Thanks again for helping those teams that don’t have professional coding mentors.

Update.
I put those 3 files back in the root of the project and it works!!!
Yeh.
I still have the one problem on that file. It won’t go away even if I restart VS code, refresh intellisense or make changes to the file and press SHIFT+ALT+F again.

But I can live with that. I’ll copy the contents of the file, delete the file, create a new file and paste them back in again if I have to.

Thanks heaps for you help.

For reference
For those of you that are running windows and using C++ here are the steps I took to make VSCode AutoFormat work the same as the WPILib code.

I ran (NB: You python installed for this to work)
pip3 install wpiformat

Three files had to be put into the root folder of the project. They were
.clang-format
.styleguide-license
For me, these opened as text in chrome and I had to use notepad to create the files and copy and paste in the contents. NB: you need to remove the .txt extension on the files you create in notepad.
The third file is called
.styleguide
And I used the example as shown in WPILib Docs (Shown below)

cppHeaderFileInclude {
   \.h$
   \.hpp$
   \.inc$
   \.inl$
}

cppSrcFileInclude {
   \.cpp$
}

modifiableFileExclude {
   gradle/
}

Then I added
C:\Users\Public\wpilib\2023\vscode\data\extensions\ms-vscode.cpptools-1.13.8\LLVM\bin
to the path in environment variables.

Now SHIFT+ALT+F formats the code so that it looks the same as the WPILib samples.

Disclaimer: Use these instructions at your own risk as there is a warning in the WPILib documentation about LLVM breaking robot builds. It’s working for me at the moment.
Now, all I need to do is get the CI stuff happening so that all the code that goes up to github is properly formatted.

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