Skip to content

Commit

Permalink
fix(lightbulb): convert diagnostic to lsp fix #1478
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed Aug 4, 2024
1 parent 508d01a commit 42eb7b2
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion lua/lspsaga/codeaction/lightbulb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,62 @@ local function update_lightbulb(bufnr, row)
inrender_buf = bufnr
end

local function severity_vim_to_lsp(severity)
if type(severity) == 'string' then
severity = vim.diagnostic.severity[severity]
end
return severity
end

--- @param diagnostic vim.Diagnostic
--- @return lsp.DiagnosticTag[]?
local function tags_vim_to_lsp(diagnostic)
if not diagnostic._tags then
return
end

local tags = {} --- @type lsp.DiagnosticTag[]
if diagnostic._tags.unnecessary then
tags[#tags + 1] = vim.lsp.protocol.DiagnosticTag.Unnecessary
end
if diagnostic._tags.deprecated then
tags[#tags + 1] = vim.lsp.protocol.DiagnosticTag.Deprecated
end
return tags
end
local function diagnostic_vim_to_lsp(diagnostics)
---@param diagnostic vim.Diagnostic
---@return lsp.Diagnostic
return vim.tbl_map(function(diagnostic)
local user_data = diagnostic.user_data or {}
if user_data.lsp then
return user_data.lsp
end
return {
range = {
start = {
line = diagnostic.lnum,
character = diagnostic.col,
},
['end'] = {
line = diagnostic.end_lnum,
character = diagnostic.end_col,
},
},
severity = severity_vim_to_lsp(diagnostic.severity),
message = diagnostic.message,
source = diagnostic.source,
code = diagnostic.code,
tags = tags_vim_to_lsp(diagnostic),
}
end, diagnostics)
end

local function render(bufnr)
local row = api.nvim_win_get_cursor(0)[1] - 1
local params = lsp.util.make_range_params()
params.context = {
diagnostics = vim.diagnostic.get(bufnr, { lnum = row + 1 }),
diagnostics = diagnostic_vim_to_lsp(vim.diagnostic.get(bufnr, { lnum = row + 1 })),
}

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

0 comments on commit 42eb7b2

Please sign in to comment.