aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/doc/diagnostic.txt49
-rw-r--r--runtime/lua/vim/diagnostic.lua71
-rw-r--r--test/functional/lua/diagnostic_spec.lua78
3 files changed, 140 insertions, 58 deletions
diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt
index dc00d47193..1dd9c4f301 100644
--- a/runtime/doc/diagnostic.txt
+++ b/runtime/doc/diagnostic.txt
@@ -375,8 +375,11 @@ config({opts}, {namespace}) *vim.diagnostic.config()*
• float: Options for floating windows:
• severity: See |diagnostic-severity|.
- • show_header: (boolean, default true) Show
- "Diagnostics:" header
+ • header: (string or table) String to use
+ as the header for the floating window. If
+ a table, it is interpreted as a [text,
+ hl_group] tuple. Defaults to
+ "Diagnostics:".
• source: (string) Include the diagnostic
source in the message. One of "always" or
"if_many".
@@ -384,17 +387,24 @@ config({opts}, {namespace}) *vim.diagnostic.config()*
a diagnostic as input and returns a
string. The return value is the text used
to display the diagnostic.
- • prefix: (function or string) Prefix each
- diagnostic in the floating window. If a
- function, it must have the signature
- (diagnostic, i, total) -> string, where
- {i} is the index of the diagnostic being
- evaluated and {total} is the total number
- of diagnostics displayed in the window.
- The returned string is prepended to each
- diagnostic in the window. Otherwise, if
+ • prefix: (function, string, or table)
+ Prefix each diagnostic in the floating
+ window. If a function, it must have the
+ signature (diagnostic, i, total) ->
+ (string, string), where {i} is the index
+ of the diagnostic being evaluated and
+ {total} is the total number of
+ diagnostics displayed in the window. The
+ 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
- each diagnostic.
+ each diagnostic in the window with no
+ highlight.
• update_in_insert: (default false) Update
diagnostics in Insert mode (if false,
@@ -616,9 +626,11 @@ open_float({bufnr}, {opts}) *vim.diagnostic.open_float()*
|vim.diagnostic.config()|.
• severity: See |diagnostic-severity|. Overrides
the setting from |vim.diagnostic.config()|.
- • show_header: (boolean, default true) Show
- "Diagnostics:" header. Overrides the setting
- from |vim.diagnostic.config()|.
+ • header: (string or table) String to use as the
+ header for the floating window. If a table, it
+ is interpreted as a [text, hl_group] tuple.
+ Overrides the setting from
+ |vim.diagnostic.config()|.
• source: (string) Include the diagnostic source
in the message. One of "always" or "if_many".
Overrides the setting from
@@ -628,9 +640,10 @@ open_float({bufnr}, {opts}) *vim.diagnostic.open_float()*
return value is the text used to display the
diagnostic. Overrides the setting from
|vim.diagnostic.config()|.
- • prefix: (function or string) Prefix each
- diagnostic in the floating window. Overrides
- the setting from |vim.diagnostic.config()|.
+ • prefix: (function, string, or table) Prefix
+ each diagnostic in the floating window.
+ Overrides the setting from
+ |vim.diagnostic.config()|.
Return: ~
tuple ({float_bufnr}, {win_id})
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
index 67ab45b97b..191b9b9145 100644
--- a/runtime/lua/vim/diagnostic.lua
+++ b/runtime/lua/vim/diagnostic.lua
@@ -539,17 +539,24 @@ end
--- its severity. Otherwise, all signs use the same priority.
--- - float: Options for floating windows:
--- * severity: See |diagnostic-severity|.
---- * show_header: (boolean, default true) Show "Diagnostics:" header
+--- * header: (string or table) String to use as the header for the floating
+--- window. If a table, it is interpreted as a [text, hl_group] tuple.
+--- Defaults to "Diagnostics:".
--- * source: (string) Include the diagnostic source in
--- the message. One of "always" or "if_many".
--- * 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.
---- * prefix: (function or string) Prefix each diagnostic in the floating window. If
---- a function, it must have the signature (diagnostic, i, total) -> string,
---- where {i} is the index of the diagnostic being evaluated and {total} is
---- the total number of diagnostics displayed in the window. The returned
---- string is prepended to each diagnostic in the window. Otherwise,
---- if {prefix} is a string, it is prepended to each diagnostic.
+--- * prefix: (function, string, or table) Prefix each diagnostic in the floating
+--- window. If a function, it must have the signature (diagnostic, i,
+--- total) -> (string, string), where {i} is the index of the diagnostic
+--- being evaluated and {total} is the total number of diagnostics
+--- displayed in the window. The 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 each diagnostic in the window with no
+--- highlight.
--- - update_in_insert: (default false) Update diagnostics in Insert mode (if false,
--- diagnostics are updated on InsertLeave)
--- - severity_sort: (default false) Sort diagnostics by severity. This affects the order in
@@ -1158,14 +1165,15 @@ end
--- from |vim.diagnostic.config()|.
--- - severity: See |diagnostic-severity|. Overrides the setting from
--- |vim.diagnostic.config()|.
---- - show_header: (boolean, default true) Show "Diagnostics:" header. Overrides the
---- setting from |vim.diagnostic.config()|.
+--- - header: (string or table) String to use as the header for the floating window. If a
+--- table, it is interpreted as a [text, hl_group] tuple. Overrides the setting
+--- from |vim.diagnostic.config()|.
--- - source: (string) Include the diagnostic source in the message. One of "always" or
--- "if_many". Overrides the setting from |vim.diagnostic.config()|.
--- - 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.
--- 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()|.
---@return tuple ({float_bufnr}, {win_id})
function M.open_float(bufnr, opts)
@@ -1237,10 +1245,21 @@ function M.open_float(bufnr, opts)
local lines = {}
local highlights = {}
- local show_header = vim.F.if_nil(opts.show_header, true)
- if show_header then
- table.insert(lines, "Diagnostics:")
- table.insert(highlights, {0, "Bold"})
+ local header = if_nil(opts.header, "Diagnostics:")
+ if header then
+ vim.validate { header = { header, function(v)
+ return type(v) == "string" or type(v) == "table"
+ end, "'string' or 'table'" } }
+ if type(header) == "table" then
+ -- Don't insert any lines for an empty string
+ if string.len(if_nil(header[1], "")) > 0 then
+ table.insert(lines, header[1])
+ table.insert(highlights, {0, header[2] or "Bold"})
+ end
+ elseif #header > 0 then
+ table.insert(lines, header)
+ table.insert(highlights, {0, "Bold"})
+ end
end
if opts.format then
@@ -1254,18 +1273,28 @@ function M.open_float(bufnr, opts)
local prefix_opt = if_nil(opts.prefix, (scope == "cursor" and #diagnostics <= 1) and "" or function(_, i)
return string.format("%d. ", i)
end)
+
+ local prefix, prefix_hl_group
if prefix_opt then
vim.validate { prefix = { prefix_opt, function(v)
- return type(v) == "string" or type(v) == "function"
- end, "'string' or 'function'" } }
+ return type(v) == "string" or type(v) == "table" or type(v) == "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
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 message_lines = vim.split(diagnostic.message, '\n')
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
table.insert(lines, string.rep(' ', #prefix) .. message_lines[j])
table.insert(highlights, {0, hiname})
@@ -1278,8 +1307,10 @@ function M.open_float(bufnr, opts)
end
local float_bufnr, winnr = require('vim.lsp.util').open_floating_preview(lines, 'plaintext', opts)
for i, hi in ipairs(highlights) do
- local prefixlen, hiname = unpack(hi)
- -- Start highlight after the prefix
+ local prefixlen, hiname, prefix_hiname = unpack(hi)
+ 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)
end
diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua
index 0de1f075c5..6414483c0d 100644
--- a/test/functional/lua/diagnostic_spec.lua
+++ b/test/functional/lua/diagnostic_spec.lua
@@ -1277,6 +1277,44 @@ describe('vim.diagnostic', function()
end)
describe('open_float()', function()
+ it('can display a header', function()
+ eq({'Diagnostics:', '1. Syntax error'}, exec_lua [[
+ local diagnostics = {
+ make_error("Syntax error", 0, 1, 0, 3),
+ }
+ vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
+ vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
+ local float_bufnr, winnr = vim.diagnostic.open_float()
+ local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
+ vim.api.nvim_win_close(winnr, true)
+ return lines
+ ]])
+
+ eq({"We're no strangers to love...", '1. Syntax error'}, exec_lua [[
+ local diagnostics = {
+ make_error("Syntax error", 0, 1, 0, 3),
+ }
+ vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
+ vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = "We're no strangers to love..."})
+ local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
+ vim.api.nvim_win_close(winnr, true)
+ return lines
+ ]])
+
+ eq({'You know the rules', '1. Syntax error'}, exec_lua [[
+ local diagnostics = {
+ make_error("Syntax error", 0, 1, 0, 3),
+ }
+ vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
+ vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = {'You know the rules', 'Search'}})
+ local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
+ vim.api.nvim_win_close(winnr, true)
+ return lines
+ ]])
+ end)
+
it('can show diagnostics from the whole buffer', function()
eq({'1. Syntax error', '2. Some warning'}, exec_lua [[
local diagnostics = {
@@ -1285,7 +1323,7 @@ describe('vim.diagnostic', function()
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header = false})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = false})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1302,7 +1340,7 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
vim.api.nvim_win_set_cursor(0, {2, 1})
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="line"})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="line"})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1317,7 +1355,7 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
vim.api.nvim_win_set_cursor(0, {1, 1})
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="line", pos=1})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="line", pos=1})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1334,7 +1372,7 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
vim.api.nvim_win_set_cursor(0, {2, 2})
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor"})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor"})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1349,7 +1387,7 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
vim.api.nvim_win_set_cursor(0, {1, 1})
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor", pos={1,3}})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor", pos={1,3}})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1364,7 +1402,7 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
vim.api.nvim_win_set_cursor(0, {1, 1})
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header=false, scope="cursor", pos={0,first_line_len}})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header=false, scope="cursor", pos={0,first_line_len}})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1434,7 +1472,7 @@ describe('vim.diagnostic', function()
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {show_header = false})
+ local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {header = false})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return #lines
@@ -1448,7 +1486,7 @@ describe('vim.diagnostic', function()
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {show_header = false, scope = "line", pos = 5})
+ local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {header = false, scope = "line", pos = 5})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return #lines
@@ -1464,7 +1502,7 @@ describe('vim.diagnostic', function()
}
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {
- show_header = false,
+ header = false,
source = "if_many",
})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
@@ -1474,7 +1512,7 @@ describe('vim.diagnostic', function()
eq({"1. source x: Syntax error"}, exec_lua [[
local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {
- show_header = false,
+ header = false,
source = "always",
})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
@@ -1489,7 +1527,7 @@ describe('vim.diagnostic', function()
}
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, {
- show_header = false,
+ header = false,
source = "if_many",
})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
@@ -1513,7 +1551,7 @@ describe('vim.diagnostic', function()
vim.diagnostic.config({severity_sort = false})
- local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false })
+ local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false })
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1521,7 +1559,7 @@ describe('vim.diagnostic', function()
eq({"1. Syntax error", "2. Error", "3. Warning", "4. Info"}, exec_lua [[
vim.diagnostic.config({severity_sort = true})
- local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false })
+ local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false })
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1529,7 +1567,7 @@ describe('vim.diagnostic', function()
eq({"1. Info", "2. Warning", "3. Error", "4. Syntax error"}, exec_lua [[
vim.diagnostic.config({severity_sort = { reverse = true } })
- local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false })
+ local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false })
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1553,7 +1591,7 @@ describe('vim.diagnostic', function()
make_warning('Warning', 0, 0, 0, 1),
})
- local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { show_header = false })
+ local float_bufnr, winnr = vim.diagnostic.open_float(diagnostic_bufnr, { header = false })
if not float_bufnr then
return 0
end
@@ -1580,7 +1618,7 @@ describe('vim.diagnostic', function()
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header = false, number = "always"})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = false})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1593,7 +1631,7 @@ describe('vim.diagnostic', function()
}
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(0, {show_header = false, prefix = ""})
+ local float_bufnr, winnr = vim.diagnostic.open_float(0, {header = false, prefix = ""})
local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
vim.api.nvim_win_close(winnr, true)
return lines
@@ -1607,7 +1645,7 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
local float_bufnr, winnr = vim.diagnostic.open_float(0, {
- show_header = false,
+ header = false,
prefix = function(_, i, total)
-- Only show a number if there is more than one diagnostic
if total > 1 then
@@ -1628,7 +1666,7 @@ describe('vim.diagnostic', function()
vim.api.nvim_win_set_buf(0, diagnostic_bufnr)
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics)
local float_bufnr, winnr = vim.diagnostic.open_float(0, {
- show_header = false,
+ header = false,
prefix = function(_, i, total)
-- Only show a number if there is more than one diagnostic
if total > 1 then
@@ -1642,7 +1680,7 @@ describe('vim.diagnostic', function()
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 }) ]]))
end)
end)