aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2021-11-16 08:48:26 -0700
committerGitHub <noreply@github.com>2021-11-16 08:48:26 -0700
commit8fb09bc5129db9d9b2f650ab30f35f55593937f3 (patch)
treefb9bd011c67ec15d2c05d0f19eb594d04886c12d /runtime/lua/vim
parent98af683e0fb129520a9c0cb29f324743e0e2083f (diff)
parent63413bd0478ebbb2aa5fc90b2533a04d8b785bbc (diff)
downloadrneovim-8fb09bc5129db9d9b2f650ab30f35f55593937f3.tar.gz
rneovim-8fb09bc5129db9d9b2f650ab30f35f55593937f3.tar.bz2
rneovim-8fb09bc5129db9d9b2f650ab30f35f55593937f3.zip
Merge pull request #16328 from gpanders/diagnostic-prefix-hi
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r--runtime/lua/vim/diagnostic.lua71
1 files changed, 51 insertions, 20 deletions
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