Skip to content

Instantly share code, notes, and snippets.

@sandikodev
Forked from neta540/PlatformIO-vim.md
Created January 31, 2021 01:40
Show Gist options
  • Save sandikodev/03ab0fe0e4e35bdb38ea7684ce4615ad to your computer and use it in GitHub Desktop.
Save sandikodev/03ab0fe0e4e35bdb38ea7684ce4615ad 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.

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