fix(linter): correctly clamp column position when greater than line length

This commit is contained in:
2025-03-29 21:29:38 +01:00
parent 71fd6c5dff
commit 4bbda69393
+21 -24
View File
@@ -79,7 +79,21 @@ function M.get_json_value(obj, path)
return current return current
end end
--- Clamp column to line length
---@param diag vim.Diagnostic
---@param bufnr number
function M:clamp_col(diag, bufnr)
local lines =
vim.api.nvim_buf_get_lines(bufnr, diag.lnum, diag.lnum + 1, false)
local line_len = #lines[1] - 1
if diag.col > line_len then
diag.col = line_len
end
end
function M:process_json_output(json, bufnr) function M:process_json_output(json, bufnr)
---@type vim.Diagnostic[]
local diagnostics = {} local diagnostics = {}
local items = json local items = json
@@ -133,18 +147,7 @@ function M:process_json_output(json, bufnr)
end end
end end
if diag.end_lnum and diag.end_col then self:clamp_col(diag, bufnr)
local lines = vim.api.nvim_buf_get_lines(
bufnr,
diag.end_lnum,
diag.end_lnum + 1,
false
)
if #lines > 0 and #lines[1] > 0 then
diag.end_col = math.min(diag.end_col, #lines[1] - 1)
end
end
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)
@@ -156,6 +159,10 @@ function M:process_json_output(json, bufnr)
return diagnostics return diagnostics
end end
--- Validate input
---@param name string
---@param config LinterConfig
---@return boolean
function M.validate(name, config) function M.validate(name, config)
local ok, resp = pcall(vim.validate, { local ok, resp = pcall(vim.validate, {
name = { name, "string" }, name = { name, "string" },
@@ -271,21 +278,11 @@ function M:run(bufnr)
return return
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)
local lines = vim.api.nvim_buf_get_lines(
bufnr,
resp.end_lnum,
resp.end_lnum + 1,
false
)
if #lines > 0 and #lines[1] > 0 then
resp.end_col = math.min(resp.end_col, #lines[1] - 1)
end
table.insert(diagnostics, resp) table.insert(diagnostics, resp)
end end
end end
vim.diagnostic.set(self.namespace, bufnr, diagnostics) vim.diagnostic.set(self.namespace, bufnr, diagnostics)
end) end)
) )