mirror of
https://github.com/neovim/neovim.git
synced 2024-12-24 13:15:09 -07:00
feat(diagnostic): allow 'prefix' option to return highlight
Extend the 'prefix' option for `open_float` to also provide an optional highlight group for the prefix string.
This commit is contained in:
parent
8f984dc1f2
commit
cc48837622
@ -384,17 +384,24 @@ config({opts}, {namespace}) *vim.diagnostic.config()*
|
|||||||
a diagnostic as input and returns a
|
a diagnostic as input and returns a
|
||||||
string. The return value is the text used
|
string. The return value is the text used
|
||||||
to display the diagnostic.
|
to display the diagnostic.
|
||||||
• prefix: (function or string) Prefix each
|
• prefix: (function, string, or table)
|
||||||
diagnostic in the floating window. If a
|
Prefix each diagnostic in the floating
|
||||||
function, it must have the signature
|
window. If a function, it must have the
|
||||||
(diagnostic, i, total) -> string, where
|
signature (diagnostic, i, total) ->
|
||||||
{i} is the index of the diagnostic being
|
(string, string), where {i} is the index
|
||||||
evaluated and {total} is the total number
|
of the diagnostic being evaluated and
|
||||||
of diagnostics displayed in the window.
|
{total} is the total number of
|
||||||
The returned string is prepended to each
|
diagnostics displayed in the window. The
|
||||||
diagnostic in the window. Otherwise, if
|
function should return a string which is
|
||||||
|
prepended to each diagnostic in the
|
||||||
|
window as well as an (optional) highlight
|
||||||
|
group which will be used to highlight the
|
||||||
|
prefix. If {prefix} is a table, it is
|
||||||
|
interpreted as a [text, hl_group] tuple
|
||||||
|
as in |nvim_echo()|; otherwise, if
|
||||||
{prefix} is a string, it is prepended to
|
{prefix} is a string, it is prepended to
|
||||||
each diagnostic.
|
each diagnostic in the window with no
|
||||||
|
highlight.
|
||||||
|
|
||||||
• update_in_insert: (default false) Update
|
• update_in_insert: (default false) Update
|
||||||
diagnostics in Insert mode (if false,
|
diagnostics in Insert mode (if false,
|
||||||
@ -625,9 +632,10 @@ open_float({bufnr}, {opts}) *vim.diagnostic.open_float()*
|
|||||||
return value is the text used to display the
|
return value is the text used to display the
|
||||||
diagnostic. Overrides the setting from
|
diagnostic. Overrides the setting from
|
||||||
|vim.diagnostic.config()|.
|
|vim.diagnostic.config()|.
|
||||||
• prefix: (function or string) Prefix each
|
• prefix: (function, string, or table) Prefix
|
||||||
diagnostic in the floating window. Overrides
|
each diagnostic in the floating window.
|
||||||
the setting from |vim.diagnostic.config()|.
|
Overrides the setting from
|
||||||
|
|vim.diagnostic.config()|.
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
tuple ({float_bufnr}, {win_id})
|
tuple ({float_bufnr}, {win_id})
|
||||||
|
@ -539,12 +539,17 @@ end
|
|||||||
--- the message. One of "always" or "if_many".
|
--- the message. One of "always" or "if_many".
|
||||||
--- * format: (function) A function that takes a diagnostic as input and returns a
|
--- * format: (function) A function that takes a diagnostic as input and returns a
|
||||||
--- string. The return value is the text used to display the diagnostic.
|
--- string. The return value is the text used to display the diagnostic.
|
||||||
--- * prefix: (function or string) Prefix each diagnostic in the floating window. If
|
--- * prefix: (function, string, or table) Prefix each diagnostic in the floating
|
||||||
--- a function, it must have the signature (diagnostic, i, total) -> string,
|
--- window. If a function, it must have the signature (diagnostic, i,
|
||||||
--- where {i} is the index of the diagnostic being evaluated and {total} is
|
--- total) -> (string, string), where {i} is the index of the diagnostic
|
||||||
--- the total number of diagnostics displayed in the window. The returned
|
--- being evaluated and {total} is the total number of diagnostics
|
||||||
--- string is prepended to each diagnostic in the window. Otherwise,
|
--- displayed in the window. The function should return a string which
|
||||||
--- if {prefix} is a string, it is prepended to each diagnostic.
|
--- is prepended to each diagnostic in the window as well as an
|
||||||
|
--- (optional) highlight group which will be used to highlight the
|
||||||
|
--- prefix. If {prefix} is a table, it is interpreted as a [text,
|
||||||
|
--- hl_group] tuple as in |nvim_echo()|; otherwise, if {prefix} is a
|
||||||
|
--- string, it is prepended to each diagnostic in the window with no
|
||||||
|
--- highlight.
|
||||||
--- - update_in_insert: (default false) Update diagnostics in Insert mode (if false,
|
--- - update_in_insert: (default false) Update diagnostics in Insert mode (if false,
|
||||||
--- diagnostics are updated on InsertLeave)
|
--- diagnostics are updated on InsertLeave)
|
||||||
--- - severity_sort: (default false) Sort diagnostics by severity. This affects the order in
|
--- - severity_sort: (default false) Sort diagnostics by severity. This affects the order in
|
||||||
@ -1148,7 +1153,7 @@ end
|
|||||||
--- - format: (function) A function that takes a diagnostic as input and returns a
|
--- - format: (function) A function that takes a diagnostic as input and returns a
|
||||||
--- string. The return value is the text used to display the diagnostic.
|
--- string. The return value is the text used to display the diagnostic.
|
||||||
--- Overrides the setting from |vim.diagnostic.config()|.
|
--- Overrides the setting from |vim.diagnostic.config()|.
|
||||||
--- - prefix: (function or string) Prefix each diagnostic in the floating window.
|
--- - prefix: (function, string, or table) Prefix each diagnostic in the floating window.
|
||||||
--- Overrides the setting from |vim.diagnostic.config()|.
|
--- Overrides the setting from |vim.diagnostic.config()|.
|
||||||
---@return tuple ({float_bufnr}, {win_id})
|
---@return tuple ({float_bufnr}, {win_id})
|
||||||
function M.open_float(bufnr, opts)
|
function M.open_float(bufnr, opts)
|
||||||
@ -1237,18 +1242,28 @@ function M.open_float(bufnr, opts)
|
|||||||
local prefix_opt = if_nil(opts.prefix, (scope == "cursor" and #diagnostics <= 1) and "" or function(_, i)
|
local prefix_opt = if_nil(opts.prefix, (scope == "cursor" and #diagnostics <= 1) and "" or function(_, i)
|
||||||
return string.format("%d. ", i)
|
return string.format("%d. ", i)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local prefix, prefix_hl_group
|
||||||
if prefix_opt then
|
if prefix_opt then
|
||||||
vim.validate { prefix = { prefix_opt, function(v)
|
vim.validate { prefix = { prefix_opt, function(v)
|
||||||
return type(v) == "string" or type(v) == "function"
|
return type(v) == "string" or type(v) == "table" or type(v) == "function"
|
||||||
end, "'string' or 'function'" } }
|
end, "'string' or 'table' or 'function'" } }
|
||||||
|
if type(prefix_opt) == "string" then
|
||||||
|
prefix, prefix_hl_group = prefix_opt, "NormalFloat"
|
||||||
|
elseif type(prefix_opt) == "table" then
|
||||||
|
prefix, prefix_hl_group = prefix_opt[1] or "", prefix_opt[2] or "NormalFloat"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for i, diagnostic in ipairs(diagnostics) do
|
for i, diagnostic in ipairs(diagnostics) do
|
||||||
local prefix = type(prefix_opt) == "string" and prefix_opt or prefix_opt(diagnostic, i, #diagnostics)
|
if prefix_opt and type(prefix_opt) == "function" then
|
||||||
|
prefix, prefix_hl_group = prefix_opt(diagnostic, i, #diagnostics)
|
||||||
|
prefix, prefix_hl_group = prefix or "", prefix_hl_group or "NormalFloat"
|
||||||
|
end
|
||||||
local hiname = floating_highlight_map[diagnostic.severity]
|
local hiname = floating_highlight_map[diagnostic.severity]
|
||||||
local message_lines = vim.split(diagnostic.message, '\n')
|
local message_lines = vim.split(diagnostic.message, '\n')
|
||||||
table.insert(lines, prefix..message_lines[1])
|
table.insert(lines, prefix..message_lines[1])
|
||||||
table.insert(highlights, {#prefix, hiname})
|
table.insert(highlights, {#prefix, hiname, prefix_hl_group})
|
||||||
for j = 2, #message_lines do
|
for j = 2, #message_lines do
|
||||||
table.insert(lines, string.rep(' ', #prefix) .. message_lines[j])
|
table.insert(lines, string.rep(' ', #prefix) .. message_lines[j])
|
||||||
table.insert(highlights, {0, hiname})
|
table.insert(highlights, {0, hiname})
|
||||||
@ -1261,8 +1276,10 @@ function M.open_float(bufnr, opts)
|
|||||||
end
|
end
|
||||||
local float_bufnr, winnr = require('vim.lsp.util').open_floating_preview(lines, 'plaintext', opts)
|
local float_bufnr, winnr = require('vim.lsp.util').open_floating_preview(lines, 'plaintext', opts)
|
||||||
for i, hi in ipairs(highlights) do
|
for i, hi in ipairs(highlights) do
|
||||||
local prefixlen, hiname = unpack(hi)
|
local prefixlen, hiname, prefix_hiname = unpack(hi)
|
||||||
-- Start highlight after the prefix
|
if prefix_hiname then
|
||||||
|
vim.api.nvim_buf_add_highlight(float_bufnr, -1, prefix_hiname, i-1, 0, prefixlen)
|
||||||
|
end
|
||||||
vim.api.nvim_buf_add_highlight(float_bufnr, -1, hiname, i-1, prefixlen, -1)
|
vim.api.nvim_buf_add_highlight(float_bufnr, -1, hiname, i-1, prefixlen, -1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1397,7 +1397,7 @@ describe('vim.diagnostic', function()
|
|||||||
return lines
|
return lines
|
||||||
]])
|
]])
|
||||||
|
|
||||||
eq("Error executing lua: .../diagnostic.lua:0: prefix: expected 'string' or 'function', got 42",
|
eq("Error executing lua: .../diagnostic.lua:0: prefix: expected 'string' or 'table' or 'function', got 42",
|
||||||
pcall_err(exec_lua, [[ vim.diagnostic.open_float(0, { prefix = 42 }) ]]))
|
pcall_err(exec_lua, [[ vim.diagnostic.open_float(0, { prefix = 42 }) ]]))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user