mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	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.
This commit is contained in:
		| @@ -392,8 +392,11 @@ config({opts}, {namespace})                          *vim.diagnostic.config()* | |||||||
|                          always show the diagnostic source. |                          always show the diagnostic source. | ||||||
|                        • spacing: (number) Amount of empty spaces inserted at |                        • spacing: (number) Amount of empty spaces inserted at | ||||||
|                          the beginning of the virtual text. |                          the beginning of the virtual text. | ||||||
|                        • prefix: (string) Prepend diagnostic message with |                        • prefix: (string or function) prepend diagnostic | ||||||
|                          prefix. |                          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 |                        • suffix: (string or function) Append diagnostic | ||||||
|                          message with suffix. If a function, it must have the |                          message with suffix. If a function, it must have the | ||||||
|                          signature (diagnostic) -> string, where {diagnostic} |                          signature (diagnostic) -> string, where {diagnostic} | ||||||
|   | |||||||
| @@ -48,6 +48,9 @@ The following changes to existing APIs or features add new behavior. | |||||||
|  |  | ||||||
| • |vim.region()| can use a string accepted by |getpos()| as position. | • |vim.region()| can use a string accepted by |getpos()| as position. | ||||||
|  |  | ||||||
|  | • vim.diagnostic.config() now accepts a function for the virtual_text.prefix | ||||||
|  |   option, which allows for rendering e.g., diagnostic severities differently. | ||||||
|  |  | ||||||
| ============================================================================== | ============================================================================== | ||||||
| REMOVED FEATURES                                                 *news-removed* | REMOVED FEATURES                                                 *news-removed* | ||||||
|  |  | ||||||
|   | |||||||
| @@ -600,7 +600,10 @@ end | |||||||
| ---                                 means to always show the diagnostic source. | ---                                 means to always show the diagnostic source. | ||||||
| ---                       * spacing: (number) Amount of empty spaces inserted at the beginning | ---                       * spacing: (number) Amount of empty spaces inserted at the beginning | ||||||
| ---                                  of the virtual text. | ---                                  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. | ---                       * suffix: (string or function) Append diagnostic message with suffix. | ||||||
| ---                                 If a function, it must have the signature (diagnostic) -> | ---                                 If a function, it must have the signature (diagnostic) -> | ||||||
| ---                                 string, where {diagnostic} is of type |diagnostic-structure|. | ---                                 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 |   -- Create a little more space between virtual text and contents | ||||||
|   local virt_texts = { { string.rep(' ', spacing) } } |   local virt_texts = { { string.rep(' ', spacing) } } | ||||||
|  |  | ||||||
|   for i = 1, #line_diags - 1 do |   for i = 1, #line_diags do | ||||||
|     table.insert(virt_texts, { prefix, virtual_text_highlight_map[line_diags[i].severity] }) |     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 |   end | ||||||
|   local last = line_diags[#line_diags] |   local last = line_diags[#line_diags] | ||||||
|  |  | ||||||
| @@ -1078,7 +1088,7 @@ function M._get_virt_text_chunks(line_diags, opts) | |||||||
|       suffix = suffix(last) or '' |       suffix = suffix(last) or '' | ||||||
|     end |     end | ||||||
|     table.insert(virt_texts, { |     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], |       virtual_text_highlight_map[last.severity], | ||||||
|     }) |     }) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1042,7 +1042,7 @@ end) | |||||||
|           local virt_text = get_virt_text_extmarks(diagnostic_ns)[1][4].virt_text |           local virt_text = get_virt_text_extmarks(diagnostic_ns)[1][4].virt_text | ||||||
|  |  | ||||||
|           local virt_texts = {} |           local virt_texts = {} | ||||||
|           for i = 2, #virt_text do |           for i = 2, #virt_text - 1 do | ||||||
|             table.insert(virt_texts, (string.gsub(virt_text[i][2], "DiagnosticVirtualText", ""))) |             table.insert(virt_texts, (string.gsub(virt_text[i][2], "DiagnosticVirtualText", ""))) | ||||||
|           end |           end | ||||||
|  |  | ||||||
| @@ -1086,7 +1086,7 @@ end) | |||||||
|         }) |         }) | ||||||
|  |  | ||||||
|         local extmarks = get_virt_text_extmarks(diagnostic_ns) |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|         local virt_text = extmarks[1][4].virt_text[2][1] |         local virt_text = extmarks[1][4].virt_text[3][1] | ||||||
|         return virt_text |         return virt_text | ||||||
|       ]] |       ]] | ||||||
|       eq(' source x: Some error', result) |       eq(' source x: Some error', result) | ||||||
| @@ -1101,7 +1101,7 @@ end) | |||||||
|         }, diagnostic_ns) |         }, diagnostic_ns) | ||||||
|  |  | ||||||
|         local extmarks = get_virt_text_extmarks(diagnostic_ns) |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|         local virt_text = extmarks[1][4].virt_text[2][1] |         local virt_text = extmarks[1][4].virt_text[3][1] | ||||||
|         return virt_text |         return virt_text | ||||||
|       ]] |       ]] | ||||||
|       eq(' Some error', result) |       eq(' Some error', result) | ||||||
| @@ -1121,7 +1121,7 @@ end) | |||||||
|         }) |         }) | ||||||
|  |  | ||||||
|         local extmarks = get_virt_text_extmarks(diagnostic_ns) |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|         local virt_text = {extmarks[1][4].virt_text[2][1], extmarks[2][4].virt_text[2][1]} |         local virt_text = {extmarks[1][4].virt_text[3][1], extmarks[2][4].virt_text[3][1]} | ||||||
|         return virt_text |         return virt_text | ||||||
|       ]] |       ]] | ||||||
|       eq(' source x: Some error', result[1]) |       eq(' source x: Some error', result[1]) | ||||||
| @@ -1151,8 +1151,8 @@ end) | |||||||
|         local extmarks = get_virt_text_extmarks(diagnostic_ns) |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|         return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} |         return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} | ||||||
|       ]] |       ]] | ||||||
|       eq(" 👀 Warning", result[1][2][1]) |       eq(" 👀 Warning", result[1][3][1]) | ||||||
|       eq(" 🔥 Error", result[2][2][1]) |       eq(" 🔥 Error", result[2][3][1]) | ||||||
|     end) |     end) | ||||||
|  |  | ||||||
|     it('includes source for formatted diagnostics', function() |     it('includes source for formatted diagnostics', function() | ||||||
| @@ -1179,8 +1179,48 @@ end) | |||||||
|         local extmarks = get_virt_text_extmarks(diagnostic_ns) |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|         return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} |         return {extmarks[1][4].virt_text, extmarks[2][4].virt_text} | ||||||
|       ]] |       ]] | ||||||
|       eq(" some_linter: 👀 Warning", result[1][2][1]) |       eq(" some_linter: 👀 Warning", result[1][3][1]) | ||||||
|       eq(" another_linter: 🔥 Error", result[2][2][1]) |       eq(" another_linter: 🔥 Error", result[2][3][1]) | ||||||
|  |     end) | ||||||
|  |  | ||||||
|  |     it('can add a prefix to virtual text', function() | ||||||
|  |       eq('E Some error',  exec_lua [[ | ||||||
|  |         local diagnostics = { | ||||||
|  |           make_error('Some error', 0, 0, 0, 0), | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { | ||||||
|  |           underline = false, | ||||||
|  |           virtual_text = { | ||||||
|  |             prefix = 'E', | ||||||
|  |             suffix = '', | ||||||
|  |           } | ||||||
|  |         }) | ||||||
|  |  | ||||||
|  |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|  |         local prefix = extmarks[1][4].virt_text[2][1] | ||||||
|  |         local message = extmarks[1][4].virt_text[3][1] | ||||||
|  |         return prefix .. message | ||||||
|  |       ]]) | ||||||
|  |  | ||||||
|  |       eq('[err-code] Some error',  exec_lua [[ | ||||||
|  |         local diagnostics = { | ||||||
|  |           make_error('Some error', 0, 0, 0, 0, nil, 'err-code'), | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, diagnostics, { | ||||||
|  |           underline = false, | ||||||
|  |           virtual_text = { | ||||||
|  |             prefix = function(diag) return string.format('[%s]', diag.code) end, | ||||||
|  |             suffix = '', | ||||||
|  |           } | ||||||
|  |         }) | ||||||
|  |  | ||||||
|  |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|  |         local prefix = extmarks[1][4].virt_text[2][1] | ||||||
|  |         local message = extmarks[1][4].virt_text[3][1] | ||||||
|  |         return prefix .. message | ||||||
|  |       ]]) | ||||||
|     end) |     end) | ||||||
|  |  | ||||||
|     it('can add a suffix to virtual text', function() |     it('can add a suffix to virtual text', function() | ||||||
| @@ -1198,7 +1238,7 @@ end) | |||||||
|         }) |         }) | ||||||
|  |  | ||||||
|         local extmarks = get_virt_text_extmarks(diagnostic_ns) |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|         local virt_text = extmarks[1][4].virt_text[2][1] |         local virt_text = extmarks[1][4].virt_text[3][1] | ||||||
|         return virt_text |         return virt_text | ||||||
|       ]]) |       ]]) | ||||||
|  |  | ||||||
| @@ -1216,7 +1256,7 @@ end) | |||||||
|         }) |         }) | ||||||
|  |  | ||||||
|         local extmarks = get_virt_text_extmarks(diagnostic_ns) |         local extmarks = get_virt_text_extmarks(diagnostic_ns) | ||||||
|         local virt_text = extmarks[1][4].virt_text[2][1] |         local virt_text = extmarks[1][4].virt_text[3][1] | ||||||
|         return virt_text |         return virt_text | ||||||
|       ]]) |       ]]) | ||||||
|     end) |     end) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Isak Samsten
					Isak Samsten