commit 2031287e93295949fbe5349413668eb14c9546f0
parent 0d9bf5b89f3896c1b436ee5acf2134d5495abeab
Author: Riley Bruins <ribru17@hotmail.com>
Date: Mon, 7 Jul 2025 10:05:02 -0700
feat(lsp): support diagnostic related information (#34474)
Diffstat:
5 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt
@@ -1900,6 +1900,12 @@ workspace_symbol({query}, {opts}) *vim.lsp.buf.workspace_symbol()*
==============================================================================
Lua module: vim.lsp.diagnostic *lsp-diagnostic*
+This module provides functionality for requesting LSP diagnostics for a
+document/workspace and populating them using |vim.Diagnostic|s.
+`DiagnosticRelatedInformation` is supported: it is included in the window
+shown by |vim.diagnostic.open_float()|.
+
+
from({diagnostics}) *vim.lsp.diagnostic.from()*
Converts the input `vim.Diagnostic`s to LSP diagnostics.
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt
@@ -196,6 +196,9 @@ LSP
receives the resolved config as the second arg: `cmd(dispatchers, config)`.
• Support for annotated text edits.
• `:checkhealth vim.lsp` is now available to check which buffers the active LSP features are attached to.
+• LSP `DiagnosticRelatedInformation` is now shown in
+ |vim.diagnostic.open_float()|. It is read from the LSP diagnostic object
+ stored in the `user_data` field.
LUA
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
@@ -2354,8 +2354,9 @@ function M.open_float(opts, ...)
end
local hiname = floating_highlight_map[diagnostic.severity]
local message_lines = vim.split(diagnostic.message, '\n')
+ local default_pre = string.rep(' ', #prefix)
for j = 1, #message_lines do
- local pre = j == 1 and prefix or string.rep(' ', #prefix)
+ local pre = j == 1 and prefix or default_pre
local suf = j == #message_lines and suffix or ''
lines[#lines + 1] = pre .. message_lines[j] .. suf
highlights[#highlights + 1] = {
@@ -2365,11 +2366,41 @@ function M.open_float(opts, ...)
hlname = prefix_hl_group,
},
suffix = {
- length = j == #message_lines and #suffix or 0,
+ length = #suf,
hlname = suffix_hl_group,
},
}
end
+
+ ---@type lsp.DiagnosticRelatedInformation[]
+ local related_info = vim.tbl_get(diagnostic, 'user_data', 'lsp', 'relatedInformation') or {}
+
+ -- Below the diagnostic, show its LSP related information (if any) in the form of file name and
+ -- range, plus description.
+ for _, info in ipairs(related_info) do
+ -- TODO: Somehow allow users to open the location when their cursor is over it?
+ local file_name = vim.fs.basename(vim.uri_to_fname(info.location.uri))
+ local info_suffix = ': ' .. info.message
+ lines[#lines + 1] = string.format(
+ '%s%s:%s:%s%s',
+ default_pre,
+ file_name,
+ info.location.range.start.line,
+ info.location.range.start.character,
+ info_suffix
+ )
+ highlights[#highlights + 1] = {
+ hlname = '@string.special.path',
+ prefix = {
+ length = #default_pre,
+ hlname = prefix_hl_group,
+ },
+ suffix = {
+ length = #info_suffix,
+ hlname = 'NormalFloat',
+ },
+ }
+ end
end
-- Used by open_floating_preview to allow the float to be focused
diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua
@@ -1,3 +1,7 @@
+---@brief This module provides functionality for requesting LSP diagnostics for a document/workspace
+---and populating them using |vim.Diagnostic|s. `DiagnosticRelatedInformation` is supported: it is
+---included in the window shown by |vim.diagnostic.open_float()|.
+
local lsp = vim.lsp
local protocol = lsp.protocol
local ms = protocol.Methods
diff --git a/runtime/lua/vim/lsp/protocol.lua b/runtime/lua/vim/lsp/protocol.lua
@@ -349,6 +349,7 @@ function protocol.make_client_capabilities()
valueSet = get_value_set(constants.DiagnosticTag),
},
dataSupport = true,
+ relatedInformation = true,
},
inlayHint = {
dynamicRegistration = true,
@@ -540,6 +541,7 @@ function protocol.make_client_capabilities()
honorsChangeAnnotations = true,
},
publishDiagnostics = {
+ relatedInformation = true,
tagSupport = {
valueSet = get_value_set(constants.DiagnosticTag),
},