Skip to content

Commit 42eb7b2

Browse files
committed
fix(lightbulb): convert diagnostic to lsp fix #1478
1 parent 508d01a commit 42eb7b2

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

lua/lspsaga/codeaction/lightbulb.lua

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,62 @@ local function update_lightbulb(bufnr, row)
5252
inrender_buf = bufnr
5353
end
5454

55+
local function severity_vim_to_lsp(severity)
56+
if type(severity) == 'string' then
57+
severity = vim.diagnostic.severity[severity]
58+
end
59+
return severity
60+
end
61+
62+
--- @param diagnostic vim.Diagnostic
63+
--- @return lsp.DiagnosticTag[]?
64+
local function tags_vim_to_lsp(diagnostic)
65+
if not diagnostic._tags then
66+
return
67+
end
68+
69+
local tags = {} --- @type lsp.DiagnosticTag[]
70+
if diagnostic._tags.unnecessary then
71+
tags[#tags + 1] = vim.lsp.protocol.DiagnosticTag.Unnecessary
72+
end
73+
if diagnostic._tags.deprecated then
74+
tags[#tags + 1] = vim.lsp.protocol.DiagnosticTag.Deprecated
75+
end
76+
return tags
77+
end
78+
local function diagnostic_vim_to_lsp(diagnostics)
79+
---@param diagnostic vim.Diagnostic
80+
---@return lsp.Diagnostic
81+
return vim.tbl_map(function(diagnostic)
82+
local user_data = diagnostic.user_data or {}
83+
if user_data.lsp then
84+
return user_data.lsp
85+
end
86+
return {
87+
range = {
88+
start = {
89+
line = diagnostic.lnum,
90+
character = diagnostic.col,
91+
},
92+
['end'] = {
93+
line = diagnostic.end_lnum,
94+
character = diagnostic.end_col,
95+
},
96+
},
97+
severity = severity_vim_to_lsp(diagnostic.severity),
98+
message = diagnostic.message,
99+
source = diagnostic.source,
100+
code = diagnostic.code,
101+
tags = tags_vim_to_lsp(diagnostic),
102+
}
103+
end, diagnostics)
104+
end
105+
55106
local function render(bufnr)
56107
local row = api.nvim_win_get_cursor(0)[1] - 1
57108
local params = lsp.util.make_range_params()
58109
params.context = {
59-
diagnostics = vim.diagnostic.get(bufnr, { lnum = row + 1 }),
110+
diagnostics = diagnostic_vim_to_lsp(vim.diagnostic.get(bufnr, { lnum = row + 1 })),
60111
}
61112

62113
lsp.buf_request(bufnr, 'textDocument/codeAction', params, function(_, result, _)

0 commit comments

Comments
 (0)