nvim: fix startup errors, add language + project-run support

Startup errors:
- catppuccin: the bufferline integration moved to catppuccin.special.bufferline
- lspconfig-extended: drop the removed LazyVim keymaps API for
  opts.servers["*"].keys, and move remove-workspace-folder to <leader>clR
  so it stops colliding with LspRestart on <leader>clr
- options: lazyvim_cmp = "blink" is not a valid value, it is "blink.cmp"

Language support (python, c/cpp, rust, zephyr, and the common ones) via
lazyvim.json extras: dap.core, clangd, cmake, docker, git, json, rust,
toml, yaml.

- lang/zephyr.lua: gated entirely on ZEPHYR_BASE / ZEPHYR_SDK_INSTALL_DIR /
  GNUARMEMB_TOOLCHAIN_PATH, returning {} when none are set, so it is inert
  on machines without Zephyr. Nothing is hardcoded: the clangd
  --query-driver globs are built from those vars, falling back to resolving
  the cross-gcc through exepath()+fs_realpath(). Adds devicetree/kconfig
  treesitter and filetypes (*.overlay, Kconfig.*, prj.conf, *_defconfig).
- clangd/config.yaml: strip the GCC-only flags Zephyr emits into
  compile_commands.json (-fno-printf-return-value, -mfp16-format=*, ...),
  which clang rejects as "Unknown argument". Remove only drops flags that
  are present, so it is a no-op for native builds. Takes a built Zephyr app
  from 15 diagnostics to 0, with CONFIG_* resolving into the generated
  autoconf.h.
- npm-servers.lua: npm is not installed here, so yaml/docker servers failed
  to install on every startup and to spawn on every matching file. Sets
  both enabled = false and mason = false for servers whose binary is
  missing from PATH and from mason/bin.

Project runner under <leader>R (<leader>r is refactoring), dispatching on
the detected project type -- zephyr, rust, python, cmake, make:

  Rr run project   Rb build   Rt test   RF build+flash   Rf run file
  Rk configure     Rc clean   Rs select target/board     Ri info

Zephyr boards are picked with vim.ui.select over `west boards`, cached per
scan and persisted per project root under stdpath("state"), never written
into the project. A build with no board set prompts first, then proceeds.
Detection walks up from the last real file buffer, tracked by an autocmd,
because a terminal opened by one of these verbs would otherwise become the
buffer detection ran from.

Also <leader>b1..b9/b0 jump to buffer N (ordinal, no longer skipping the
current one) and <leader>b<Left>/<Right> cycle.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01StGBghPW9Dm7z7uj2VNWHr
This commit is contained in:
2026-09-04 15:22:14 +02:00
parent 108ea36cca
commit 2ea7d6457f
11 changed files with 754 additions and 46 deletions

View File

@@ -0,0 +1,115 @@
-- Zephyr RTOS support.
--
-- This extra is driven entirely by Zephyr's own environment variables and is
-- inert unless one of them is set, so it costs nothing on a machine that does
-- no Zephyr work and needs no per-host editing.
--
-- ZEPHYR_BASE the zephyr repo -- presence of this enables the extra
-- ZEPHYR_SDK_INSTALL_DIR Zephyr SDK root (toolchain variant "zephyr")
-- GNUARMEMB_TOOLCHAIN_PATH toolchain root (variant "gnuarmemb")
-- ZEPHYR_TOOLCHAIN_VARIANT which of the above is in use
--
-- Zephyr is otherwise "C + CMake + Kconfig + devicetree", so clangd and cmake
-- do the heavy lifting. What clangd cannot work out unaided is the built-in
-- include paths of a cross compiler, hence --query-driver.
-- (Zephyr's build/compile_commands.json is found by clangd on its own: it
-- searches ./ and ./build of each parent directory.)
local zephyr_base = os.getenv("ZEPHYR_BASE")
local sdk_dir = os.getenv("ZEPHYR_SDK_INSTALL_DIR")
local gnuarmemb = os.getenv("GNUARMEMB_TOOLCHAIN_PATH")
-- Not a Zephyr environment -> contribute nothing.
if not (zephyr_base or sdk_dir or gnuarmemb) then
return {}
end
--- Toolchain globs for clangd's --query-driver, taken from the Zephyr vars.
--- Falls back to whatever cross gcc the environment puts on $PATH when the
--- toolchain variables are not set (as with a distro-packaged toolchain).
---@return string[]
local function query_driver_globs()
local globs, seen = {}, {}
local function add(p)
if p and p ~= "" and not seen[p] then
seen[p] = true
globs[#globs + 1] = p
end
end
-- Zephyr SDK: <sdk>/<target>-zephyr-elf/bin/<target>-zephyr-elf-gcc
if sdk_dir then
add(sdk_dir .. "/*/bin/*-gcc")
end
-- gnuarmemb variant: <root>/bin/arm-none-eabi-gcc
if gnuarmemb then
add(gnuarmemb .. "/bin/*-gcc")
end
-- No toolchain var set: resolve the cross compiler through $PATH instead of
-- assuming a prefix, and glob its real directory.
if #globs == 0 then
for _, cc in ipairs({
"arm-zephyr-eabi-gcc",
"arm-none-eabi-gcc",
"riscv64-zephyr-elf-gcc",
"xtensa-espressif_esp32-elf-gcc",
}) do
local exe = vim.fn.exepath(cc)
if exe ~= "" then
add(vim.fs.dirname(vim.uv.fs_realpath(exe) or exe) .. "/*-gcc")
end
end
end
return globs
end
return {
-- Syntax for the two Zephyr-specific languages
{
"nvim-treesitter/nvim-treesitter",
opts = { ensure_installed = { "devicetree", "kconfig" } },
},
{
"neovim/nvim-lspconfig",
-- Filetype detection Neovim does not ship with
init = function()
vim.filetype.add({
extension = {
overlay = "dts", -- board/shield overlays are devicetree
},
filename = {
["prj.conf"] = "kconfig",
["west.yml"] = "yaml",
},
pattern = {
["Kconfig.*"] = "kconfig", -- Kconfig.foo, Kconfig.defconfig, ...
[".*/boards/.*%.conf"] = "kconfig",
[".*/prj_.*%.conf"] = "kconfig",
[".*_defconfig"] = "kconfig",
},
})
end,
opts = function(_, opts)
opts.servers = opts.servers or {}
local clangd = opts.servers.clangd or {}
local drivers = query_driver_globs()
if #drivers > 0 then
clangd.cmd = clangd.cmd or { "clangd" }
table.insert(clangd.cmd, "--query-driver=" .. table.concat(drivers, ","))
end
-- Treat a west workspace / Zephyr app as a project root.
clangd.root_markers = clangd.root_markers or {}
vim.list_extend(clangd.root_markers, { "west.yml", ".west", "prj.conf", "CMakeLists.txt" })
opts.servers.clangd = clangd
return opts
end,
},
}

View File

@@ -4,20 +4,6 @@ local prefix = "<leader>cl"
return {
{
"neovim/nvim-lspconfig",
init = function()
local keys = require("lazyvim.plugins.lsp.keymaps").get()
keys[#keys + 1] = { "<leader>cl", false }
keys[#keys + 1] = { "<leader>cil", "<cmd>LspInfo<cr>", desc = "Lsp" }
keys[#keys + 1] = { prefix .. "r", "<cmd>LspRestart<cr>", desc = "Restart Lsp" }
keys[#keys + 1] = { prefix .. "s", "<cmd>LspStart<cr>", desc = "Start Lsp" }
keys[#keys + 1] = { prefix .. "S", "<cmd>LspStop<cr>", desc = "Stop Lsp" }
-- stylua: ignore start
keys[#keys + 1] = { prefix .. "r", function() vim.lsp.buf.remove_workspace_folder() end, desc = "Remove workspace" }
keys[#keys + 1] = { prefix .. "a", function() vim.lsp.buf.add_workspace_folder() end, desc = "Add workspace" }
-- stylua: ignore end
end,
opts = {
diagnostics = {
virtual_text = {
@@ -42,6 +28,22 @@ return {
enabled = false,
},
servers = {
-- Keymaps for every server. Was an `init` function calling
-- require("lazyvim.plugins.lsp.keymaps").get(), which LazyVim
-- deprecated in favour of the `keys` field on the server config.
["*"] = {
keys = {
{ "<leader>cl", false },
{ "<leader>cil", "<cmd>LspInfo<cr>", desc = "Lsp" },
{ prefix .. "r", "<cmd>LspRestart<cr>", desc = "Restart Lsp" },
{ prefix .. "s", "<cmd>LspStart<cr>", desc = "Start Lsp" },
{ prefix .. "S", "<cmd>LspStop<cr>", desc = "Stop Lsp" },
-- stylua: ignore start
{ prefix .. "a", function() vim.lsp.buf.add_workspace_folder() end, desc = "Add workspace" },
{ prefix .. "R", function() vim.lsp.buf.remove_workspace_folder() end, desc = "Remove workspace" },
-- stylua: ignore end
},
},
lua_ls = {
settings = {
Lua = {

View File

@@ -8,6 +8,21 @@ table.insert(keys, { prefix .. "s", "<Cmd>BufferLineSortByExtension<CR>", desc =
table.insert(keys, { prefix .. "<", "<cmd>BufferLineMovePrev<cr>", desc = "Move buffer prev" })
table.insert(keys, { prefix .. ">", "<cmd>BufferLineMoveNext<cr>", desc = "Move buffer next" })
-- Jump straight to a buffer by its position in the bufferline.
-- BufferLineGoToBuffer takes the absolute 1-based slot, so <leader>b1 is always
-- the leftmost buffer and the count never skips the one you are already on.
-- The ordinal shown in the bufferline (see numbers = "ordinal" below) is the
-- number to press.
for i = 1, 9 do
table.insert(keys, { prefix .. i, "<cmd>BufferLineGoToBuffer " .. i .. "<cr>", desc = "Buffer " .. i })
end
-- ...and 0 for the last one, however many there are.
table.insert(keys, { prefix .. "0", "<cmd>BufferLineGoToBuffer -1<cr>", desc = "Last Buffer" })
-- Arrows step through the bufferline in visible order.
table.insert(keys, { prefix .. "<Left>", "<cmd>BufferLineCyclePrev<cr>", desc = "Prev Buffer" })
table.insert(keys, { prefix .. "<Right>", "<cmd>BufferLineCycleNext<cr>", desc = "Next Buffer" })
-- stylua: ignore end
return {
@@ -15,9 +30,11 @@ return {
keys = keys,
opts = {
options = {
modified_icon = "",
modified_icon = "",
color_icons = true,
separator_style = "slope",
-- Show 1, 2, 3 ... so the number you see is the number you press.
numbers = "ordinal",
},
},
}

View File

@@ -50,7 +50,7 @@ return {
{
"akinsho/bufferline.nvim",
opts = {
highlights = require("catppuccin.groups.integrations.bufferline").get_theme({
highlights = require("catppuccin.special.bufferline").get_theme({
styles = { "italic", "bold" },
custom = {
all = {

View File

@@ -0,0 +1,60 @@
-- Some language servers are distributed only as npm packages, and Node on Arch
-- ships without npm. Without this guard you get two separate rounds of errors:
--
-- 1. mason-lspconfig retries the install on every startup and fails, once per
-- server ("failed to install yamlls" ...).
-- 2. LazyVim still enables the server, so opening any matching file fails with
-- "Spawning language server with cmd: { "yaml-language-server", ... } failed".
--
-- So each server below is disabled unless its binary can actually be found.
-- Nothing is hardcoded to this host: the check is a PATH lookup plus Mason's own
-- bin directory, so the moment the server exists it enables itself again.
--
-- To get them: sudo pacman -S npm
-- nvim +"MasonInstall yaml-language-server dockerfile-language-server" +qa
-- LazyVim server name -> the executable Mason/npm installs for it.
local npm_servers = {
yamlls = "yaml-language-server",
dockerls = "docker-langserver",
docker_compose_language_service = "docker-compose-langserver",
}
local mason_bin = vim.fs.joinpath(vim.fn.stdpath("data"), "mason", "bin")
---@param bin string
---@return boolean
local function available(bin)
if vim.fn.executable(bin) == 1 then
return true
end
-- Mason prepends its bin dir to PATH only after setup, so look there directly.
return vim.uv.fs_stat(vim.fs.joinpath(mason_bin, bin)) ~= nil
end
local missing = {}
for server, bin in pairs(npm_servers) do
if not available(bin) then
missing[#missing + 1] = server
end
end
if #missing == 0 then
return {}
end
return {
{
"neovim/nvim-lspconfig",
opts = function(_, opts)
opts.servers = opts.servers or {}
for _, server in ipairs(missing) do
opts.servers[server] = vim.tbl_deep_extend("force", opts.servers[server] or {}, {
enabled = false, -- do not try to spawn a binary that is not there
mason = false, -- and do not retry the impossible install every startup
})
end
return opts
end,
},
}

View File

@@ -0,0 +1,55 @@
-- Keymaps for the language-aware project runner in lua/util/project.lua.
--
-- <leader>r is already the refactoring group, so the run/project verbs live
-- under <leader>R. Each verb dispatches on the detected project type, so the
-- same key does the right thing in a Rust crate, a Python package or a Zephyr
-- application.
local function p(fn)
return function()
require("util.project")[fn]()
end
end
return {
{
"folke/snacks.nvim",
-- Remember the last real file buffer, so the run verbs still know which
-- project you are in after one of them has opened a terminal (at which
-- point the focused buffer is the terminal, not your code).
init = function()
vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
group = vim.api.nvim_create_augroup("project_run_last_file", { clear = true }),
callback = function(ev)
if vim.bo[ev.buf].buftype == "" then
local name = vim.api.nvim_buf_get_name(ev.buf)
if name ~= "" then
vim.g.project_last_file = name
end
end
end,
})
end,
-- stylua: ignore
keys = {
{ "<leader>R", "", desc = "+run/project" },
{ "<leader>Rr", p("run"), desc = "Run project" },
{ "<leader>Rf", p("run_file"), desc = "Run current file" },
{ "<leader>Rb", p("build"), desc = "Build" },
{ "<leader>Rt", p("test"), desc = "Test" },
{ "<leader>RF", p("flash"), desc = "Build + Flash (Zephyr)" },
{ "<leader>Rs", p("select_target"), desc = "Select target/board" },
{ "<leader>Rk", p("configure"), desc = "Configure (menuconfig/clippy)" },
{ "<leader>Rc", p("clean"), desc = "Clean" },
{ "<leader>Ri", p("info"), desc = "Project info" },
},
},
{
"folke/which-key.nvim",
opts = {
spec = {
{ "<leader>R", group = "run/project", icon = " " },
},
},
},
}