Configuring LSP for QMK project

And other large C projects


Ok, this is a short post, but it's something I've been struggling with for a while and I'm quite happy I found a solution.

The issue is that QMK is quite a complex C project, and LSP servers have a hard time making sense of it. That makes code diagnostics, code actions and autocompletions rather useless. I use clangd, so that's what I'll focus on. I tried ccls to see if it had the same problems and it did, and I don't know if a similar solution exists for it

The solution is to use Bear, an amazing tool that generates a "compilation database" for clangd to use. So, without further ado, this is the command that fixed clangd for me: make clean; bear -- qmk compile -kb <keyboard> -km <layout> , for example make clean; bear -- qmk compile -kb zsa/moonlander -km default . This generates a compile_commands.json file. If you have your user config directly in the qmk_firmware repo, clangd should detect it already. If instead you're using external userspaces, you have to copy that file to the root of your userspace repo.


Edit: I later found out that QMK provides an option to generate this compilation database: qmk generate-compilation-database . It takes the same arguments as qmk compile , and generates a compile_commands.json , similar to Bear. However, in my testing, the results with Bear were better