feat(linter): add diagnostic tags
This commit is contained in:
@@ -32,6 +32,10 @@ M.__index = M
|
|||||||
---@field zero_idx? boolean
|
---@field zero_idx? boolean
|
||||||
---@field callback? fun(diag: vim.Diagnostic)
|
---@field callback? fun(diag: vim.Diagnostic)
|
||||||
|
|
||||||
|
---@class DiagnosticTagMap
|
||||||
|
---@field unnecessary? string[]
|
||||||
|
---@field deprecated? string[]
|
||||||
|
|
||||||
---@class LinterConfig
|
---@class LinterConfig
|
||||||
---@field cmd string[]
|
---@field cmd string[]
|
||||||
---@field stdin? boolean
|
---@field stdin? boolean
|
||||||
@@ -43,6 +47,7 @@ M.__index = M
|
|||||||
---@field source? string
|
---@field source? string
|
||||||
---@field debounce? number
|
---@field debounce? number
|
||||||
---@field json? JsonConfig
|
---@field json? JsonConfig
|
||||||
|
---@field tags? DiagnosticTagMap
|
||||||
M.config = {}
|
M.config = {}
|
||||||
|
|
||||||
-- Extract a value from a JSON object using a path
|
-- Extract a value from a JSON object using a path
|
||||||
@@ -92,6 +97,39 @@ function M:clamp_col(diag, bufnr)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Add diagnostic tags
|
||||||
|
---@param diag vim.Diagnostic
|
||||||
|
function M:add_tags(diag)
|
||||||
|
if not self.config.tags then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local have_unnecessary = vim.islist(self.config.tags.unnecessary)
|
||||||
|
local have_deprecated = vim.islist(self.config.tags.deprecated)
|
||||||
|
|
||||||
|
if not have_unnecessary and not have_deprecated then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
diag._tags = {}
|
||||||
|
|
||||||
|
if
|
||||||
|
have_unnecessary
|
||||||
|
and vim.list_contains(self.config.tags.unnecessary, diag.code)
|
||||||
|
then
|
||||||
|
diag._tags.unnecessary = true
|
||||||
|
diag.severity = vim.diagnostic.severity.HINT
|
||||||
|
end
|
||||||
|
|
||||||
|
if
|
||||||
|
have_deprecated
|
||||||
|
and vim.list_contains(self.config.tags.deprecated, diag.code)
|
||||||
|
then
|
||||||
|
diag._tags.deprecated = true
|
||||||
|
diag.severity = vim.diagnostic.severity.WARN
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function M:process_json_output(json, bufnr)
|
function M:process_json_output(json, bufnr)
|
||||||
---@type vim.Diagnostic[]
|
---@type vim.Diagnostic[]
|
||||||
local diagnostics = {}
|
local diagnostics = {}
|
||||||
@@ -148,6 +186,7 @@ function M:process_json_output(json, bufnr)
|
|||||||
end
|
end
|
||||||
|
|
||||||
self:clamp_col(diag, bufnr)
|
self:clamp_col(diag, bufnr)
|
||||||
|
self:add_tags(diag)
|
||||||
|
|
||||||
if type(self.config.json.callback) == "function" then
|
if type(self.config.json.callback) == "function" then
|
||||||
self.config.json.callback(diag)
|
self.config.json.callback(diag)
|
||||||
@@ -201,6 +240,7 @@ function M.validate(name, config)
|
|||||||
debounce = { config.debounce, "number", true },
|
debounce = { config.debounce, "number", true },
|
||||||
source = { config.source, "string", true },
|
source = { config.source, "string", true },
|
||||||
json = { config.json, "table", true },
|
json = { config.json, "table", true },
|
||||||
|
tags = { config.tags, "table", true },
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -279,6 +319,7 @@ function M:run(bufnr)
|
|||||||
elseif resp then
|
elseif resp then
|
||||||
resp.source = resp.source or self.config.source
|
resp.source = resp.source or self.config.source
|
||||||
self:clamp_col(resp, bufnr)
|
self:clamp_col(resp, bufnr)
|
||||||
|
self:add_tags(resp)
|
||||||
table.insert(diagnostics, resp)
|
table.insert(diagnostics, resp)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user