Skip to content

Instantly share code, notes, and snippets.

@omani
Forked from ironlungx/nvim-pio.md
Created February 26, 2025 15:08
Show Gist options
  • Save omani/465c85ad9bbe645082dc82ae146636db to your computer and use it in GitHub Desktop.
Save omani/465c85ad9bbe645082dc82ae146636db to your computer and use it in GitHub Desktop.
PlatformIO with Neovim

Extensions

Following are the extensions required for neovim:

I use lazy.nvim so, just add the following to your plugins table, or create a new file in lua/plugins/lsp.lua

return {
	{
		"williamboman/mason.nvim",
		config = function()
			require("mason").setup({})
		end,
	},
	{
		"williamboman/mason-lspconfig.nvim",
		config = function()
			require("mason-lspconfig").setup({
				ensure_installed = { "lua_ls", "clangd" },
			})
		end,
	},
	{
		"neovim/nvim-lspconfig",
		config = function()
			local lspconfig = require("lspconfig")
			lspconfig.lua_ls.setup({})
            lspconfig.clangd.setup({})
		end,
	},
}

Setting up the project

I've made a script to do all this for you:

$ cd path/to/the/project
$ curl https://gist.githubusercontent.com/ironlungx/a2b620de74f875c49c1d06999a8c41f8/raw/3f0d5d46c0d6369fc7e51e32487772ac194c27b1/script.sh | sh
$ pio run -t compiledb

BUT I DONT LIKE curl|sh:

  1. Initialize a project with pio init

  2. In the project root create the following files

    • .clangd:
      # clangd controls options for the LSP *server*
      CompileFlags:                    
      Add:
        [
          -DSSIZE_MAX,
          -DLWIP_NO_UNISTD_H=1,
          -Dssize_t=long,
          -D_SSIZE_T_DECLARED,
        ]
      Remove:
        [
          -fno-tree-switch-conversion,
          -mtext-section-literals,
          -mlongcalls,
          -fstrict-volatile-bitfields,
          -free,
          -fipa-pta,
        ]
    • .clang-tidy:
      # Formatter options
      Checks: '-*, -misc-definitions-in-headers' # Fixes some issue with header reinclusion
      IndentWidth: 2 # Only if you like 2, replace with 3/4/.. if you'd like
    • gen_compile_commands.py:
      import os
      Import("env") # platformio specific stuff
      
      # include toolchain paths (i.e. to have stuff like the Arduino framework headers present in the compile commands)
      env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True)
      
      # override compilation DB path
      env.Replace(COMPILATIONDB_PATH="compile_commands.json")
  3. Now add the following to your platformio.ini

    extra_scripts = pre:gen_compile_commands.py
  4. Now in your terminal run: pio run -t compiledb (Just delete the old compile_commands.json before executing this)

Hopefully this helped, I spent a lot of time figuring it out

#! /usr/bin/env bash
echo 'CompileFlags:
Add:
[
-DSSIZE_MAX,
-DLWIP_NO_UNISTD_H=1,
-Dssize_t=long,
-D_SSIZE_T_DECLARED,
]
Remove:
[
-fno-tree-switch-conversion,
-mtext-section-literals,
-mlongcalls,
-fstrict-volatile-bitfields,
-free,
-fipa-pta,
]' > .clangd
echo "Checks: '-*, -misc-definitions-in-headers' " > .clangd-tidy
echo 'import os
Import("env")
# include toolchain paths
env.Replace(COMPILATIONDB_INCLUDE_TOOLCHAIN=True)
# override compilation DB path
env.Replace(COMPILATIONDB_PATH="compile_commands.json")' > gen_compile_commands.py
echo 'extra_scripts = pre:gen_compile_commands.py' >> platformio.ini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment