feat(lsp): support diagnostic related information (#34474)

This commit is contained in:
Riley Bruins
2025-07-07 10:05:02 -07:00
committed by GitHub
parent 0d9bf5b89f
commit 2031287e93
5 changed files with 48 additions and 2 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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),
},