VS Code Setup
Language Integration with clangd
clangd
is a C++ language server that provides intelligent code completion and errors messages.
Why clangd over other language servers?
clangd is smart. It analyzes our build system to known exactly how all of the files fit together so you never need to manually configure include paths or compiler flags.
Whenever you build a project with the Makefile, clangd
will see the new build/compile_commands.json
and immediately update your IDE's include paths. When you start developing for a different project or platform, simply rebuild the project and your language server will automatically update.
Installation
- Install the official clangd VS Code extension from LLVM https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd.
- Open the VS Code command palette with Ctrl+Shift+P. Search and run
clangd: Download Language Server
. - Disable any other C++ language extensions that may conflict with clangd, such at the Microsoft C/C++ extension.
- Create an empty file named
.clangd
in thefirmware/
directory. This ensures clangd knows where to find thebuild/compile_commands.json
file.
clangd Configuration
Configure the firmware/.clangd
file for your architecture.
CompileFlags:
Add:
- --target=x86_64-w64-mingw64
CompileFlags:
Add:
- --target=arm64-apple-darwin22.6.0
The
--target
flag should be#commented out
unless you are compiling for thecli
platform.
For more configuration options, see the official clangd docs https://clangd.llvm.org/config.
Use clangd as a formatter
You can use clangd to automatically format your code whenever you save your file. This will prevent your commits being rejected by the GitHub action.
- Open any
.cc
file in the repo, such asprojects/Demo/Blink/main.cc
. - Configure clangd as your default C++ formatter:
- Open the VS Code command palette. Run
Format Document With...
- Select
Configure Default Formatter -> clangd
- Open the VS Code command palette. Run
-
Edit your settings to enable format-on-save:
- Open the VS Code command palette. Run
Preferences: Open Workspace Settings (JSON)
- Edit the
[cpp]
section (create it if necessary) to add
"[cpp]": { "editor.formatOnSave": true, },
- Open the VS Code command palette. Run
To verify the formatter is working, open any .cc
file and type some poorly-formatted code. When you Ctrl+S save, the code should be immediately formatted.
int x = 100;
for(int i = 0;
i<10; i++)
{
x += i;
}
int x = 100;
for (int i = 0; i < 10; i++) {
x += i;
}