-- Neovim config for the Linux kernel workshop (Neovim >= 0.12, Arch). -- Kernel modules and eBPF via clangd (compile_commands.json from bear), Python via pyright + ruff, -- $KDIR/scripts/checkpatch.pl on save for kernel modules. Setup: tooling/editors/nvim.md. vim.g.mapleader = " " vim.g.maplocalleader = " " local o = vim.opt o.number = true o.relativenumber = true o.signcolumn = "yes" o.mouse = "a" o.clipboard = "unnamedplus" o.ignorecase = true o.smartcase = true o.undofile = true o.splitright = true o.splitbelow = true o.scrolloff = 5 o.updatetime = 250 o.list = true o.listchars = { tab = "» ", trail = "·", nbsp = "␣" } o.completeopt = { "menu", "menuone", "noselect" } -- plugins: built-in vim.pack (:lua vim.pack.update() to update) vim.pack.add({ "https://github.com/neovim/nvim-lspconfig", { src = "https://github.com/nvim-treesitter/nvim-treesitter", version = "main" }, -- release tags ship a prebuilt fuzzy matcher { src = "https://github.com/saghen/blink.cmp", version = vim.version.range("1.*") }, "https://github.com/ibhagwan/fzf-lua", "https://github.com/lewis6991/gitsigns.nvim", "https://github.com/folke/which-key.nvim", }, { confirm = false }) -- treesitter: c, lua, vimdoc, markdown and query ship with Neovim; the rest is compiled on first -- start (needs tree-sitter-cli and a C compiler) require("nvim-treesitter").install({ "c", "python", "bash", "make", "kconfig", "devicetree", "asm", "diff", "gitcommit", }) vim.api.nvim_create_autocmd("FileType", { callback = function(ev) if pcall(vim.treesitter.start, ev.buf) then vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()" vim.wo[0][0].foldmethod = "expr" vim.wo[0][0].foldlevel = 99 end end, }) -- kernel coding style (Documentation/process/coding-style.rst): 8-wide tabs, 100 columns vim.api.nvim_create_autocmd("FileType", { pattern = { "c", "cpp", "make", "kconfig", "asm" }, callback = function() vim.bo.expandtab = false vim.bo.tabstop = 8 vim.bo.shiftwidth = 8 vim.bo.softtabstop = 0 vim.bo.cinoptions = ":0,l1,t0,g0,(0" vim.wo.colorcolumn = "100" end, }) vim.filetype.add({ filename = { Kbuild = "make" } }) -- completion require("blink.cmp").setup({ keymap = { preset = "default" }, -- accept, / select, open, close completion = { documentation = { auto_show = true } }, signature = { enabled = true }, fuzzy = { implementation = "prefer_rust" }, }) -- LSP: server configs come from nvim-lspconfig, enabled when the binary is installed vim.lsp.config("*", { capabilities = require("blink.cmp").get_lsp_capabilities() }) vim.lsp.config("clangd", { cmd = { "clangd", "--background-index", "--clang-tidy", -- checks: ~/.config/clangd/config.yaml "--header-insertion=never", "--completion-style=detailed", }, }) -- ruff: lint + format; leave hover to pyright vim.lsp.config("ruff", { on_attach = function(client) client.server_capabilities.hoverProvider = false end, }) for server, bin in pairs({ clangd = "clangd", pyright = "pyright-langserver", ruff = "ruff", bashls = "bash-language-server", }) do if vim.fn.executable(bin) == 1 then vim.lsp.enable(server) end end vim.diagnostic.config({ virtual_text = { source = "if_many" }, severity_sort = true, float = { source = true, border = "rounded" }, }) -- $KDIR/scripts/checkpatch.pl on save, for C files next to a Kbuild (or a Makefile with obj-m) local checkpatch_ns = vim.api.nvim_create_namespace("checkpatch") local function checkpatch_bin() local p = (vim.env.KDIR or "") .. "/scripts/checkpatch.pl" return vim.fn.filereadable(p) == 1 and p or "" end local function is_module_dir(dir) if vim.fn.filereadable(dir .. "/Kbuild") == 1 then return true end local mk = dir .. "/Makefile" if vim.fn.filereadable(mk) == 0 then return false end for _, line in ipairs(vim.fn.readfile(mk)) do if line:match("^obj%-[my]") then return true end end return false end local function checkpatch(buf) local file = vim.api.nvim_buf_get_name(buf) local bin = checkpatch_bin() if bin == "" or file:match("%.bpf%.c$") or not is_module_dir(vim.fs.dirname(file)) then vim.diagnostic.reset(checkpatch_ns, buf) return end vim.system( { "perl", bin, "--no-tree", "--terse", "--show-types", "--no-summary", "--file", file }, { text = true }, vim.schedule_wrap(function(res) if not vim.api.nvim_buf_is_valid(buf) then return end local diags = {} -- file:line: WARNING:TYPE: message for line in (res.stdout or ""):gmatch("[^\n]+") do local lnum, sev, typ, msg = line:match(":(%d+): (%u+):([%w_]+): (.*)$") if lnum then diags[#diags + 1] = { lnum = tonumber(lnum) - 1, col = 0, severity = sev == "ERROR" and vim.diagnostic.severity.ERROR or sev == "WARNING" and vim.diagnostic.severity.WARN or vim.diagnostic.severity.INFO, source = "checkpatch", code = typ, message = msg, } end end vim.diagnostic.set(checkpatch_ns, buf, diags) end) ) end vim.api.nvim_create_autocmd({ "BufReadPost", "BufWritePost" }, { pattern = "*.[ch]", callback = function(ev) checkpatch(ev.buf) end, }) vim.api.nvim_create_user_command("Checkpatch", function() checkpatch(0) end, {}) -- pickers, git, key hints local fzf = require("fzf-lua") fzf.setup({}) require("gitsigns").setup({}) require("which-key").setup({ delay = 500 }) -- keys. Neovim defaults: K hover, grn rename, gra code action, grr references, gri implementation, -- gO document symbols, [d / ]d diagnostics, signature help (insert mode) local map = function(lhs, rhs, desc, mode) vim.keymap.set(mode or "n", lhs, rhs, { desc = desc }) end map("gd", vim.lsp.buf.definition, "Go to definition") map("gD", vim.lsp.buf.declaration, "Go to declaration") map("ff", fzf.files, "Find files") map("fg", fzf.live_grep, "Grep") map("fw", fzf.grep_cword, "Grep word under cursor") map("fb", fzf.buffers, "Buffers") map("fh", fzf.helptags, "Help") map("fr", fzf.resume, "Resume last picker") map("ls", fzf.lsp_live_workspace_symbols, "Workspace symbols") map("lr", fzf.lsp_references, "References") map("li", fzf.lsp_incoming_calls, "Incoming calls") map("lo", fzf.lsp_outgoing_calls, "Outgoing calls") map("ld", fzf.diagnostics_document, "Diagnostics (buffer)") map("lD", fzf.diagnostics_workspace, "Diagnostics (workspace)") map("lf", function() vim.lsp.buf.format() end, "Format (clang-format / ruff)", { "n", "x" }) map("lh", "LspClangdSwitchSourceHeader", "Switch source/header") map("e", vim.diagnostic.open_float, "Show diagnostic")