From 42eb7b26eebf5221a4ae9e4e7a49a95b50202daa Mon Sep 17 00:00:00 2001 From: glepnir Date: Sun, 4 Aug 2024 10:06:44 +0800 Subject: [PATCH] fix(lightbulb): convert diagnostic to lsp fix #1478 --- lua/lspsaga/codeaction/lightbulb.lua | 53 +++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/lua/lspsaga/codeaction/lightbulb.lua b/lua/lspsaga/codeaction/lightbulb.lua index 3f7abb9a..a34786ee 100644 --- a/lua/lspsaga/codeaction/lightbulb.lua +++ b/lua/lspsaga/codeaction/lightbulb.lua @@ -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, _)