Skip to content

Instantly share code, notes, and snippets.

@neta540
Last active October 11, 2023 18:43
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neta540/9e65261be52d6cd4d6c17399b78d34bb to your computer and use it in GitHub Desktop.
Save neta540/9e65261be52d6cd4d6c17399b78d34bb to your computer and use it in GitHub Desktop.

PlatformIO with Vim (neomake, clang, coc-vim, ale)

Requirements

PlatformIO

clangd

bear

vim

Plugins required:

https://www.github.com/neoclide/coc.nvim
https://www.github.com/neomake/neomake
https://www.github.com/w0rp/ale

Ok, let's get started

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 update

Configure 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 all

Once 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.

@masihtehrani
Copy link

Hi Neta
you don't have problem libraries in vim?
in my vim not found Arduino.h and another external libraries but there are found in vs code!

@diegodorado
Copy link

same happens to me:
clang: fatal error: 'Arduino.h' file not found

@diegodorado
Copy link

ok... changed clangd to ccls and now it is working

@masihtehrani
Copy link

ok... changed clangd to ccls and now it is working

it's my :CocConfig

{
    "languageserver": {
      "golang": {
        "command": "gopls",
        "rootPatterns": ["go.mod", ".vim/", ".git/", ".hg/"],
        "filetypes": ["go"],
        "initializationOptions": {
          "usePlaceholders": true
        }
    },
    "ccls": {
        "enable": false,
        "command": "ccls",
        "filetypes": ["c", "cpp", "objc", "objcpp"],
        "rootPatterns": [".ccls", "compile_commands.json", ".vim/", ".git/", ".hg/"],
        "initializationOptions": {
            "cache": {
                "directory": "/tmp/ccls"
            }
        }
    }
  }
}

error-ccls-arduino

@mlthlschr
Copy link

For those who are interested, I managed to get it working via pio init --ide vim in the root folder of the project. This command generates a .ccls file which can be read by ccls. Beware that you have to delete the compile_commands.json then (at least I needed to).

@Kamholtz
Copy link

Kamholtz commented Dec 24, 2021

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.json file for you using the following command:

pio run -t compiledb

By default the compile_commands.json file ends up in a directory that clangd will not detect. In my case it was /.pio/build/nodemcuv2. There is a :CocConfig param for clangd called compilationDatabasePath but 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 the compile_commands.json is created.

There is also a --compile-commands-dir option for clangd which might be worth investigating.

EDIT:

I can now confirm that the following :CocLocalConfg will detect the compile_commands.json file in the specified directory using clangd.args and --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"]
    }
  }
}

@haryandb
Copy link

pio run -t compiledb

thanks, work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment