aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2021-09-21 16:27:12 -0600
committerGitHub <noreply@github.com>2021-09-21 15:27:12 -0700
commit17b7968f02bca309bb2e7b00752667baa6b4d765 (patch)
treef100835b7844cc49a0f09ad80df02578b846b639
parent8164adc1441b2150737bb7e30c59de73176d6447 (diff)
downloadrneovim-17b7968f02bca309bb2e7b00752667baa6b4d765.tar.gz
rneovim-17b7968f02bca309bb2e7b00752667baa6b4d765.tar.bz2
rneovim-17b7968f02bca309bb2e7b00752667baa6b4d765.zip
fix(diagnostic): preserve fields from LSP diagnostics via user_data (#15735)master
* preserve fields from LSP diagnostics via adding a user_data table to the diagnostic, which can hold arbitrary data in addition to the lsp diagnostic information.
-rw-r--r--runtime/lua/vim/lsp/diagnostic.lua15
-rw-r--r--test/functional/plugin/lsp/diagnostic_spec.lua26
2 files changed, 38 insertions, 3 deletions
diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua
index 148836a93a..a9d93ae65f 100644
--- a/runtime/lua/vim/lsp/diagnostic.lua
+++ b/runtime/lua/vim/lsp/diagnostic.lua
@@ -102,7 +102,16 @@ local function diagnostic_lsp_to_vim(diagnostics, bufnr, client_id)
end_lnum = _end.line,
end_col = line_byte_from_position(buf_lines, _end.line, _end.character, offset_encoding),
severity = severity_lsp_to_vim(diagnostic.severity),
- message = diagnostic.message
+ message = diagnostic.message,
+ user_data = {
+ lsp = {
+ code = diagnostic.code,
+ codeDescription = diagnostic.codeDescription,
+ tags = diagnostic.tags,
+ relatedInformation = diagnostic.relatedInformation,
+ data = diagnostic.data,
+ },
+ },
}
end, diagnostics)
end
@@ -110,7 +119,7 @@ end
---@private
local function diagnostic_vim_to_lsp(diagnostics)
return vim.tbl_map(function(diagnostic)
- return {
+ return vim.tbl_extend("error", {
range = {
start = {
line = diagnostic.lnum,
@@ -123,7 +132,7 @@ local function diagnostic_vim_to_lsp(diagnostics)
},
severity = severity_vim_to_lsp(diagnostic.severity),
message = diagnostic.message,
- }
+ }, diagnostic.user_data and (diagnostic.user_data.lsp or {}) or {})
end, diagnostics)
end
diff --git a/test/functional/plugin/lsp/diagnostic_spec.lua b/test/functional/plugin/lsp/diagnostic_spec.lua
index 353de667ea..243ad6bdb8 100644
--- a/test/functional/plugin/lsp/diagnostic_spec.lua
+++ b/test/functional/plugin/lsp/diagnostic_spec.lua
@@ -428,6 +428,32 @@ describe('vim.lsp.diagnostic', function()
end)
end)
end)
+
+ it('maintains LSP information when translating diagnostics', function()
+ local result = exec_lua [[
+ local diagnostics = {
+ make_error("Error 1", 1, 1, 1, 5),
+ }
+
+ diagnostics[1].code = 42
+ diagnostics[1].tags = {"foo", "bar"}
+ diagnostics[1].data = "Hello world"
+
+ vim.lsp.diagnostic.on_publish_diagnostics(nil, {
+ uri = fake_uri,
+ diagnostics = diagnostics,
+ }, {client_id=1})
+
+ return {
+ vim.diagnostic.get(diagnostic_bufnr, {lnum=1})[1],
+ vim.lsp.diagnostic.get_line_diagnostics(diagnostic_bufnr, 1)[1],
+ }
+ ]]
+ eq({code = 42, tags = {"foo", "bar"}, data = "Hello world"}, result[1].user_data.lsp)
+ eq(42, result[2].code)
+ eq({"foo", "bar"}, result[2].tags)
+ eq("Hello world", result[2].data)
+ end)
end)
describe("vim.lsp.diagnostic.get_line_diagnostics", function()