From 07b60efd8058bb515998f50048b511d50f9671f8 Mon Sep 17 00:00:00 2001 From: Isak Samsten Date: Mon, 17 Apr 2023 13:53:34 +0200 Subject: feat(diagnostic): specify diagnostic virtual text prefix as a function - vim.diagnostic.config() now accepts a function for the virtual_text.prefix option, which allows for rendering e.g., diagnostic severities differently. --- runtime/lua/vim/diagnostic.lua | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index d1b50304c7..0d1d01b391 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -600,7 +600,10 @@ end --- means to always show the diagnostic source. --- * spacing: (number) Amount of empty spaces inserted at the beginning --- of the virtual text. ---- * prefix: (string) Prepend diagnostic message with prefix. +--- * prefix: (string or function) prepend diagnostic message with prefix. +--- If a function, it must have the signature (diagnostic) -> string, +--- where {diagnostic} is of type |diagnostic-structure|. This can be +--- used to render diagnostic symbols or error codes. --- * suffix: (string or function) Append diagnostic message with suffix. --- If a function, it must have the signature (diagnostic) -> --- string, where {diagnostic} is of type |diagnostic-structure|. @@ -1066,8 +1069,15 @@ function M._get_virt_text_chunks(line_diags, opts) -- Create a little more space between virtual text and contents local virt_texts = { { string.rep(' ', spacing) } } - for i = 1, #line_diags - 1 do - table.insert(virt_texts, { prefix, virtual_text_highlight_map[line_diags[i].severity] }) + for i = 1, #line_diags do + local resolved_prefix = prefix + if type(prefix) == 'function' then + resolved_prefix = prefix(line_diags[i]) or '' + end + table.insert( + virt_texts, + { resolved_prefix, virtual_text_highlight_map[line_diags[i].severity] } + ) end local last = line_diags[#line_diags] @@ -1078,7 +1088,7 @@ function M._get_virt_text_chunks(line_diags, opts) suffix = suffix(last) or '' end table.insert(virt_texts, { - string.format('%s %s%s', prefix, last.message:gsub('\r', ''):gsub('\n', ' '), suffix), + string.format(' %s%s', last.message:gsub('\r', ''):gsub('\n', ' '), suffix), virtual_text_highlight_map[last.severity], }) -- cgit