nvim: Improve marksman diagnostic filtering in zk dirs

When in a ZK dir I do not want any marksman diagnostics polluting my
interface since the linking in (my) ZK is based on anchor IDs and not
full filenames/titles. Thus, every single link will be detected as
non-existent by marksman.

This commit ensures that when in a zk-enabled directory, each
non-existent link diagnostic will be filtered before presenting
diagnostics.
This commit is contained in:
Marty Oehme 2025-06-10 16:18:34 +02:00
parent 99098f56a1
commit 6435a7b51a
Signed by: Marty
GPG key ID: 4E535BC19C61886E
2 changed files with 39 additions and 50 deletions

View file

@ -0,0 +1,39 @@
local util = require("lspconfig.util")
local function filter_marksman_diagnostics(diagnostic)
-- code == 2 filters all 'non-existent link' diagnostics
if diagnostic.source == "marksman" and diagnostic.code == 2 then
return false
end
return true
end
local function handle_diagnostics(err, result, _)
result.diagnostics = vim.tbl_filter(filter_marksman_diagnostics, result.diagnostics)
return result, err
end
return {
filetypes = { "markdown", "quarto", "djot" },
on_attach = function(client, bufnr)
local fname = vim.api.nvim_buf_get_name(bufnr)
local is_zk_root_dir = util.root_pattern(".zk")
-- turn off diagnostics if we are in a ZK directory
if is_zk_root_dir(fname) then
client.capabilities.completionProvider = false
client.capabilities.hoverProvider = false
client.capabilities.publishDiagnostics = false
client.capabilities.diagnostic = false
local namespace = vim.lsp.diagnostic.get_namespace(client.id)
-- hides diagnostics from displaying in buffer
vim.diagnostic.enable(false, { ns_id = namespace, bufnr = bufnr })
vim.diagnostic.get()
-- actually removes all marksman diagnostics
client["handlers"] = client.handlers or {}
client.handlers["textDocument/publishDiagnostics"] = handle_diagnostics
end
end,
}

View file

@ -87,56 +87,6 @@ local lsp = {
register("nushell") register("nushell")
end end
register("marksman", {
filetypes = { "markdown", "quarto" },
on_attach = function(client, _)
-- TODO: for some reason this stays true even after rootdir switch?
if client.config.in_zk_notebook then
local default_handler = vim.diagnostic.handlers.virtual_text
vim.diagnostic.handlers.virtual_text = {
show = function(namespace, bufnrr, diagnostics, opts)
for i, diagnostic in ipairs(diagnostics) do
if
diagnostic.source
== "Marksman" -- You need to check what the correct value should be here
then
table.remove(diagnostics, i)
end
end
default_handler.show(namespace, bufnrr, diagnostics, opts)
end,
hide = function(...)
default_handler.hide(...)
end,
}
default_handler = vim.diagnostic.handlers.signs
vim.diagnostic.handlers.signs = {
show = function(namespace, bufnrr, diagnostics, opts)
for i, diagnostic in ipairs(diagnostics) do
if
diagnostic.source
== "Marksman" -- You need to check what the correct value should be here
then
table.remove(diagnostics, i)
end
end
default_handler.show(namespace, bufnrr, diagnostics, opts)
end,
hide = function(...)
default_handler.hide(...)
end,
}
end
end,
on_new_config = function(conf, new_root)
if require("lspconfig.util").root_pattern(".zk")(new_root) then
conf.in_zk_notebook = true
else
conf.in_zk_notebook = false
end
end,
})
-- arduino lsp only works if arduino-cli is installed -- arduino lsp only works if arduino-cli is installed
if vim.fn.executable("arduino-cli") == 1 then if vim.fn.executable("arduino-cli") == 1 then
register("arduino_language_server") register("arduino_language_server")