Skip to main content

Neovim

Goal

Neovim setup to view Codee checks as editor diagnostics.

neovim

Prerequisites

Ensure you have:

  • Codee installed on your system.
  • A compile_commands.json compilation database (You can check the Basic Workflow)
  • Neovim >= 0.9.5 and basic familiarity with how to configure it

Install nvim-lint

To easily display Codee checkers in Neovim, install the nvim-lint extension using its installation instructions.

Configure nvim-lint for Codee

Add the following configuration to your init.lua Neovim configuration file ( or in a separate configuration file within your existing setup):

local lint = require('lint')
local codee_parse = function(output, bufnr)
local parse_severity = function(checkid)
if string.find(checkid, "^RMK%d{3}") then
return vim.diagnostic.severity.HINT
end
if string.find(checkid, "^PWR%d{3}") then
return vim.diagnostic.severity.WARN
end
if string.find(checkid, "^PWD%d{3}") then
return vim.diagnostic.severity.ERROR
end
return vim.diagnostic.severity.WARN
end
local parse_location = function(location)
local line, col = string.match(location, "^.+:(%d+):(%d+)$")
if line then
line = line - 1
end
if col then
col = col - 1
end
return { line = line, col = col }
end
local diagnostics = {}
local ok, results = pcall(vim.json.decode, output)
if not ok or not results or not results.Checks then
return diagnostics
end
for _, result in ipairs(results.Checks or {}) do
local location = parse_location(result.Location)
if location.line then
local diagnostic = {
bufnr = bufnr,
message = result.Title .. '\n' .. result.Suggestion,
col = location.col,
end_col = location.col,
lnum = location.line,
end_lnum = location.line,
code = result.Check,
severity = parse_severity(result.Check),
source = "codee",
}
table.insert(diagnostics, diagnostic)
end
end
return diagnostics
end
lint.linters.codee = {
cmd = 'codee',
append_fname = true,
args = { "checks", "--json" },
parser = codee_parse
}
lint.linters_by_ft = {
c = { "codee" },
cpp = { "codee" },
fortran = { "codee" },
}
vim.api.nvim_create_autocmd({ "BufReadPost", "BufWritePost", "InsertLeave" }, {
callback = function()
lint.try_lint()
end
})

The diagnostics will be displayed using Neovim's builtin diagnostics API.

Configure Neovim diagnostics (Optional)

You can consult :help vim.diagnostic.config for how to configure when and how diagnostics are displayed.

A popular configuration is

vim.diagnostic.config({virtual_text=true})

This will show every checker as a virtual text next to the code.

Neovim has default mappings to navigate diagnostics (]d and [d) and to open a diagnostic (<Ctrl-w>d).