diff options
author | Gregory Anders <greg@gpanders.com> | 2021-09-17 10:50:25 -0600 |
---|---|---|
committer | Gregory Anders <greg@gpanders.com> | 2021-09-17 10:50:25 -0600 |
commit | 32c0631183a64925d38a13819db9557f8da02738 (patch) | |
tree | b7ab4094cbc2fe0a1e61089d49d7954cd9f3642b | |
parent | f87779a24d7133b04762ad3369704924c4247fba (diff) | |
download | rneovim-32c0631183a64925d38a13819db9557f8da02738.tar.gz rneovim-32c0631183a64925d38a13819db9557f8da02738.tar.bz2 rneovim-32c0631183a64925d38a13819db9557f8da02738.zip |
fix(diagnostic): support severity_sort
-rw-r--r-- | runtime/doc/diagnostic.txt | 21 | ||||
-rw-r--r-- | runtime/lua/vim/diagnostic.lua | 23 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/diagnostic.lua | 4 | ||||
-rw-r--r-- | test/functional/lua/diagnostic_spec.lua | 47 |
4 files changed, 80 insertions, 15 deletions
diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt index 00132d3fd7..9ed75e1356 100644 --- a/runtime/doc/diagnostic.txt +++ b/runtime/doc/diagnostic.txt @@ -216,18 +216,31 @@ config({opts}, {namespace}) *vim.diagnostic.config()* {opts} table Configuration table with the following keys: • underline: (default true) Use underline for - diagnostics + diagnostics. Options: + • severity: Only underline diagnostics + matching the given severity + |diagnostic-severity| + • virtual_text: (default true) Use virtual - text for diagnostics + text for diagnostics. Options: + • severity: Only show virtual text for + diagnostics matching the given severity + |diagnostic-severity| + • signs: (default true) Use signs for - diagnostics + diagnostics. Options: + • severity: Only show signs for diagnostics + matching the given severity + |diagnostic-severity| + • 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 which signs and virtual text are - displayed + displayed. Options: + • reverse: (boolean) Reverse sort order {namespace} number|nil Update the options for the given namespace. When omitted, update the global diagnostic options. diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index dae785647b..6547188594 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -440,13 +440,20 @@ end --- - `function`: Function with signature (namespace, bufnr) that returns any of the above. --- ---@param opts table Configuration table with the following keys: ---- - underline: (default true) Use underline for diagnostics ---- - virtual_text: (default true) Use virtual text for diagnostics ---- - signs: (default true) Use signs for diagnostics +--- - underline: (default true) Use underline for diagnostics. Options: +--- * severity: Only underline diagnostics matching the given severity +--- |diagnostic-severity| +--- - virtual_text: (default true) Use virtual text for diagnostics. Options: +--- * severity: Only show virtual text for diagnostics matching the given +--- severity |diagnostic-severity| +--- - signs: (default true) Use signs for diagnostics. Options: +--- * severity: Only show signs for diagnostics matching the given severity +--- |diagnostic-severity| --- - 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 ---- which signs and virtual text are displayed +--- which signs and virtual text are displayed. Options: +--- * reverse: (boolean) Reverse sort order ---@param namespace number|nil Update the options for the given namespace. When omitted, update the --- global diagnostic options. function M.config(opts, namespace) @@ -989,6 +996,14 @@ function M.show(namespace, bufnr, diagnostics, opts) end end + if vim.F.if_nil(opts.severity_sort, false) then + if type(opts.severity_sort) == "table" and opts.severity_sort.reverse then + table.sort(diagnostics, function(a, b) return a.severity > b.severity end) + else + table.sort(diagnostics, function(a, b) return a.severity < b.severity end) + end + end + if opts.underline then M._set_underline(namespace, bufnr, diagnostics, opts.underline) end diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua index 4d0b6f83bc..41c8bd36ec 100644 --- a/runtime/lua/vim/lsp/diagnostic.lua +++ b/runtime/lua/vim/lsp/diagnostic.lua @@ -195,10 +195,6 @@ function M.on_publish_diagnostics(_, result, ctx, config) local diagnostics = result.diagnostics if config then - if vim.F.if_nil(config.severity_sort, false) then - table.sort(diagnostics, function(a, b) return a.severity > b.severity end) - end - for _, opt in pairs(config) do if type(opt) == 'table' then if not opt.severity and opt.severity_limit then diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 8da33173a2..3c8d6e8f2c 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -35,7 +35,7 @@ describe('vim.diagnostic', function() } end - function make_information(msg, x1, y1, x2, y2) + function make_info(msg, x1, y1, x2, y2) return { lnum = x1, col = y1, @@ -456,7 +456,7 @@ describe('vim.diagnostic', function() vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { make_error("Error 1", 1, 1, 1, 5), make_warning("Warning on Server 1", 1, 1, 2, 5), - make_information("Ignored information", 1, 1, 2, 5), + make_info("Ignored information", 1, 1, 2, 5), make_hint("Here's a hint", 1, 1, 2, 5), }) @@ -478,7 +478,7 @@ describe('vim.diagnostic', function() vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { make_error("Error 1", 1, 1, 1, 5), make_warning("Warning on Server 1", 1, 1, 2, 5), - make_information("Ignored information", 1, 1, 2, 5), + make_info("Ignored information", 1, 1, 2, 5), make_error("Error On Other Line", 2, 1, 1, 5), }) @@ -538,6 +538,47 @@ describe('vim.diagnostic', function() eq(1, get_extmark_count_with_severity("WARN")) eq(1, get_extmark_count_with_severity("HINT")) end) + + it('allows sorting by severity', function() + local result = exec_lua([[ + vim.diagnostic.config({ + underline = true, + virtual_text = false, + severity_sort = false, + }) + + vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { + make_warning('Warning', 4, 4, 4, 4), + make_error('Error', 4, 4, 4, 4), + make_info('Info', 4, 4, 4, 4), + }) + + local extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + + local warn_highlight = extmarks[1][4].hl_group + + vim.diagnostic.config({ + severity_sort = true, + }) + + extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + + local err_highlight = extmarks[1][4].hl_group + + vim.diagnostic.config({ + severity_sort = { reverse = true }, + }) + + extmarks = vim.api.nvim_buf_get_extmarks(diagnostic_bufnr, diagnostic_ns, 0, -1, {details = true}) + + local info_highlight = extmarks[1][4].hl_group + + return { warn_highlight, err_highlight, info_highlight } + ]]) + eq('DiagnosticUnderlineWarn', result[1]) + eq('DiagnosticUnderlineError', result[2]) + eq('DiagnosticUnderlineInfo', result[3]) + end) end) describe('set()', function() |