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.
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?
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.
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.
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
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.
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)
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.