Files
EMBS/Practicals/hlsdev/nvim/plugin/hls_pragma_lint.lua

93 lines
2.8 KiB
Lua

-- ===========================================================================
-- HLS pragma diagnostics for Neovim (EMBS)
-- ---------------------------------------------------------------------------
-- Runs hls_pragma_lint.py over C/C++ buffers and reports the results through
-- vim.diagnostic, so they show up exactly like clangd's errors.
--
-- No plugin manager involved: this file lives in the hlsdev nvim runtimepath
-- entry, and Neovim sources plugin/*.lua automatically.
--
-- Commands:
-- :HlsPragmaLint re-run on the current buffer
-- :lua vim.diagnostic.reset(...) (see :h vim.diagnostic)
-- ===========================================================================
if vim.g.loaded_hls_pragma_lint then
return
end
vim.g.loaded_hls_pragma_lint = true
local ns = vim.api.nvim_create_namespace('hls_pragma_lint')
-- Locate hls_pragma_lint.py next to this runtimepath entry.
local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':p:h')
local linter = vim.fn.fnamemodify(plugin_dir, ':h:h') .. '/hls_pragma_lint.py'
local function python()
if vim.fn.executable('python3') == 1 then
return 'python3'
end
return nil
end
local SEVERITY = {
error = vim.diagnostic.severity.ERROR,
warning = vim.diagnostic.severity.WARN,
info = vim.diagnostic.severity.INFO,
}
local function parse(output, bufnr)
local diags = {}
for line in vim.gsplit(output, '\n', { plain = true }) do
local _, l, c, sev, msg = line:match('^(.-):(%d+):(%d+): (%a+): (.*)$')
if l then
table.insert(diags, {
bufnr = bufnr,
lnum = tonumber(l) - 1,
col = tonumber(c) - 1,
severity = SEVERITY[sev] or vim.diagnostic.severity.INFO,
message = msg,
source = 'hls-pragma',
})
end
end
return diags
end
local function lint(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
if not vim.api.nvim_buf_is_valid(bufnr) then
return
end
local ft = vim.bo[bufnr].filetype
if ft ~= 'c' and ft ~= 'cpp' then
return
end
if vim.fn.filereadable(linter) ~= 1 then
return
end
local py = python()
if not py then
return
end
local path = vim.api.nvim_buf_get_name(bufnr)
if path == '' then
return
end
local out = vim.fn.system({ py, linter, path })
vim.diagnostic.set(ns, bufnr, parse(out, bufnr))
end
vim.api.nvim_create_autocmd({ 'BufWritePost', 'BufReadPost' }, {
group = vim.api.nvim_create_augroup('HlsPragmaLint', { clear = true }),
pattern = { '*.c', '*.cpp', '*.cc', '*.h', '*.hpp' },
callback = function(ev)
lint(ev.buf)
end,
})
vim.api.nvim_create_user_command('HlsPragmaLint', function()
lint(0)
end, { desc = 'Check #pragma HLS lines in this buffer' })