Plugins required:
https://www.github.com/neoclide/coc.nvim
https://www.github.com/neomake/neomake
https://www.github.com/w0rp/ale
First, create a Makefile file in the project directory
# Uncomment lines below if you have problems with $PATH
#SHELL := /bin/bash
#PATH := /usr/local/bin:$(PATH)
all:
platformio run
upload:
platformio run --target upload
clean:
platformio run --target clean
program:
platformio run --target program
uploadfs:
platformio run --target uploadfs
update:
platformio updateConfigure coc-vim to play nicely with clangd using :CocConfig
{
"languageserver": {
"clangd": {
"command": "clangd",
"args": ["--background-index"],
"rootPatterns": [
"compile_flags.txt",
"compile_commands.json",
".vim/",
".git/",
".hg/"
],
"filetypes": ["c", "cpp", "objc", "objcpp"]
}
},
"diagnostic.displayByAle": true
}Configure ALE to be nice with clangd in .vimrc
let g:ale_linters = { 'cpp': ['clangd'] }
let g:ale_fixers = { 'cpp': [ 'clang-format' ] }Generate compile_commands.json using Bear.
Since platformio takes care of compile flags, we need to feed clang with the correct flags for our project.
To do so, we need to create a file named compile_commands.json and place it in the project directory. This file includes all the information necessary for clang to be able to build this project. Creating this file automatically is possible using Bear.
make clean
bear make allOnce the file is generated, clangd could work on the project correctly.
It is important to recreate compile_commands.json each time the project structure changes, build flags changed, libraries added or removed.
To build the project in vim, simply call :Neomake! any compilation errors will appear in vim.
I am guessing everyone here has likely moved to ccls. I personally could not get ccls working on windows. I found that you can get platformio to generate a
compile_commands.jsonfile for you using the following command:By default the
compile_commands.jsonfile ends up in a directory that clangd will not detect. In my case it was/.pio/build/nodemcuv2. There is a:CocConfigparam for clangd calledcompilationDatabasePathbut that did not seem to work for me, so I used the work around detailed in https://docs.platformio.org/en/latest/integration/compile_commands.html which uses a python script to change the where thecompile_commands.jsonis created.There is also a
--compile-commands-diroption forclangdwhich might be worth investigating.EDIT:
I can now confirm that the following
:CocLocalConfgwill detect thecompile_commands.jsonfile in the specified directory usingclangd.argsand--compile-commands-dir:{ "languageserver": { "clangd": { "command": "clangd", "args": ["--background-index", "--compile-commands-dir", ".pio/build/nodemcuv2" ], "rootPatterns": [ "compile_flags.txt", "compile_commands.json", ".vim/", ".git/", ".hg/" ], "filetypes": ["c", "cpp", "objc", "objcpp"] } } }