diff options
author | Gregory Anders <8965202+gpanders@users.noreply.github.com> | 2021-11-14 18:40:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-14 18:40:11 -0700 |
commit | 3c74ba4acb87ebf7c5f2090ac9b4644cafec2495 (patch) | |
tree | ac50eb8d30290951b690d9a6e277f3c30359f2c9 /runtime/lua/vim/diagnostic.lua | |
parent | 2f37ffb71920d0b50f658108736a1465e1e5f280 (diff) | |
download | rneovim-3c74ba4acb87ebf7c5f2090ac9b4644cafec2495.tar.gz rneovim-3c74ba4acb87ebf7c5f2090ac9b4644cafec2495.tar.bz2 rneovim-3c74ba4acb87ebf7c5f2090ac9b4644cafec2495.zip |
feat(diagnostic): add 'prefix' option to open_float (#16321)
The 'prefix' option accepts a function or a string that is used to add a
prefix string to each diagnostic displayed in the floating window.
Diffstat (limited to 'runtime/lua/vim/diagnostic.lua')
-rw-r--r-- | runtime/lua/vim/diagnostic.lua | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index d0f729f3b9..b30a678eeb 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -1,3 +1,5 @@ +local if_nil = vim.F.if_nil + local M = {} M.severity = { @@ -65,7 +67,7 @@ end local function prefix_source(source, diagnostics) vim.validate { source = {source, function(v) return v == "always" or v == "if_many" - end, "Invalid value for option 'source'" } } + end, "'always' or 'if_many'" } } if source == "if_many" then local sources = {} @@ -537,6 +539,12 @@ end --- 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. --- - 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 @@ -1140,6 +1148,8 @@ end --- - 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. +--- Overrides the setting from |vim.diagnostic.config()|. ---@return tuple ({float_bufnr}, {win_id}) function M.open_float(bufnr, opts) vim.validate { @@ -1224,8 +1234,17 @@ function M.open_float(bufnr, opts) diagnostics = prefix_source(opts.source, diagnostics) end + local prefix_opt = if_nil(opts.prefix, (scope == "cursor" and #diagnostics <= 1) and "" or function(_, i) + return string.format("%d. ", i) + end) + if prefix_opt then + vim.validate { prefix = { prefix_opt, function(v) + return type(v) == "string" or type(v) == "function" + end, "'string' or 'function'" } } + end + for i, diagnostic in ipairs(diagnostics) do - local prefix = string.format("%d. ", i) + local prefix = type(prefix_opt) == "string" and prefix_opt or prefix_opt(diagnostic, i, #diagnostics) local hiname = floating_highlight_map[diagnostic.severity] local message_lines = vim.split(diagnostic.message, '\n') table.insert(lines, prefix..message_lines[1]) |