fix(lsp): send back diagnostic tags to the server

Fixes: #27318
This commit is contained in:
Lewis Russell 2024-02-06 12:34:04 +00:00
parent 7fa84f32e7
commit 3be2536ca0
2 changed files with 26 additions and 7 deletions

View File

@ -11,7 +11,7 @@ local M = {}
--- @field severity? vim.diagnostic.Severity --- @field severity? vim.diagnostic.Severity
--- @field message string --- @field message string
--- @field source? string --- @field source? string
--- @field code? string --- @field code? string|integer
--- @field _tags? { deprecated: boolean, unnecessary: boolean} --- @field _tags? { deprecated: boolean, unnecessary: boolean}
--- @field user_data? any arbitrary data plugins can add --- @field user_data? any arbitrary data plugins can add
--- @field namespace? integer --- @field namespace? integer

View File

@ -102,16 +102,17 @@ end
---@param diagnostics lsp.Diagnostic[] ---@param diagnostics lsp.Diagnostic[]
---@param bufnr integer ---@param bufnr integer
---@param client_id integer ---@param client_id integer
---@return Diagnostic[] ---@return vim.Diagnostic[]
local function diagnostic_lsp_to_vim(diagnostics, bufnr, client_id) local function diagnostic_lsp_to_vim(diagnostics, bufnr, client_id)
local buf_lines = get_buf_lines(bufnr) local buf_lines = get_buf_lines(bufnr)
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
local offset_encoding = client and client.offset_encoding or 'utf-16' local offset_encoding = client and client.offset_encoding or 'utf-16'
---@diagnostic disable-next-line:no-unknown --- @param diagnostic lsp.Diagnostic
--- @return vim.Diagnostic
return vim.tbl_map(function(diagnostic) return vim.tbl_map(function(diagnostic)
---@cast diagnostic lsp.Diagnostic
local start = diagnostic.range.start local start = diagnostic.range.start
local _end = diagnostic.range['end'] local _end = diagnostic.range['end']
--- @type vim.Diagnostic
return { return {
lnum = start.line, lnum = start.line,
col = line_byte_from_position(buf_lines, start.line, start.character, offset_encoding), col = line_byte_from_position(buf_lines, start.line, start.character, offset_encoding),
@ -135,12 +136,29 @@ local function diagnostic_lsp_to_vim(diagnostics, bufnr, client_id)
end, diagnostics) end, diagnostics)
end end
--- @param diagnostics Diagnostic[] --- @param diagnostic vim.Diagnostic
--- @return lsp.DiagnosticTag[]?
local function tags_vim_to_vim(diagnostic)
if not diagnostic._tags then
return
end
local tags = {} --- @type lsp.DiagnosticTag[]
if diagnostic._tags.unnecessary then
tags[#tags + 1] = protocol.DiagnosticTag.Unnecessary
end
if diagnostic._tags.deprecated then
tags[#tags + 1] = protocol.DiagnosticTag.Deprecated
end
return tags
end
--- @param diagnostics vim.Diagnostic[]
--- @return lsp.Diagnostic[] --- @return lsp.Diagnostic[]
local function diagnostic_vim_to_lsp(diagnostics) local function diagnostic_vim_to_lsp(diagnostics)
---@diagnostic disable-next-line:no-unknown ---@param diagnostic vim.Diagnostic
---@return lsp.Diagnostic
return vim.tbl_map(function(diagnostic) return vim.tbl_map(function(diagnostic)
---@cast diagnostic Diagnostic
return vim.tbl_extend('keep', { return vim.tbl_extend('keep', {
-- "keep" the below fields over any duplicate fields in diagnostic.user_data.lsp -- "keep" the below fields over any duplicate fields in diagnostic.user_data.lsp
range = { range = {
@ -157,6 +175,7 @@ local function diagnostic_vim_to_lsp(diagnostics)
message = diagnostic.message, message = diagnostic.message,
source = diagnostic.source, source = diagnostic.source,
code = diagnostic.code, code = diagnostic.code,
tags = tags_vim_to_vim(diagnostics),
}, diagnostic.user_data and (diagnostic.user_data.lsp or {}) or {}) }, diagnostic.user_data and (diagnostic.user_data.lsp or {}) or {})
end, diagnostics) end, diagnostics)
end end