diff options
author | Gregory Anders <8965202+gpanders@users.noreply.github.com> | 2024-04-25 08:07:44 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-25 08:07:44 -0500 |
commit | b13e63db1dbc1dbc7e23690653df1b7317660a2b (patch) | |
tree | 71d2f0076c20138b44d55dd2ba44d0c41e49b8d6 /runtime/lua/vim/diagnostic.lua | |
parent | e0d92b9cc20b58179599f53dfa74ca821935a539 (diff) | |
download | rneovim-b13e63db1dbc1dbc7e23690653df1b7317660a2b.tar.gz rneovim-b13e63db1dbc1dbc7e23690653df1b7317660a2b.tar.bz2 rneovim-b13e63db1dbc1dbc7e23690653df1b7317660a2b.zip |
feat(diagnostic): goto functions jump to highest severity (#28490)
When the "severity" option is nil, vim.diagnostic.goto_next() and
vim.diagnostic.goto_prev() jump to the next diagnostic with the highest
severity.
Diffstat (limited to 'runtime/lua/vim/diagnostic.lua')
-rw-r--r-- | runtime/lua/vim/diagnostic.lua | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 3321b6ad71..1ae370e9b2 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -820,11 +820,35 @@ local function next_diagnostic(position, search_forward, bufnr, opts, namespace) position[1] = position[1] - 1 bufnr = get_bufnr(bufnr) local wrap = if_nil(opts.wrap, true) - local line_count = api.nvim_buf_line_count(bufnr) + local diagnostics = get_diagnostics(bufnr, vim.tbl_extend('keep', opts, { namespace = namespace }), true) + + -- When severity is unset we jump to the diagnostic with the highest severity. First sort the + -- diagnostics by severity. The first diagnostic then contains the highest severity, and we can + -- discard all diagnostics with a lower severity. + if opts.severity == nil then + table.sort(diagnostics, function(a, b) + return a.severity < b.severity + end) + + -- Find the first diagnostic where the severity does not match the highest severity, and remove + -- that element and all subsequent elements from the array + local worst = (diagnostics[1] or {}).severity + local len = #diagnostics + for i = 2, len do + if diagnostics[i].severity ~= worst then + for j = i, len do + diagnostics[j] = nil + end + break + end + end + end + local line_diagnostics = diagnostic_lines(diagnostics) + local line_count = api.nvim_buf_line_count(bufnr) for i = 0, line_count do local offset = i * (search_forward and 1 or -1) local lnum = position[1] + offset @@ -1165,8 +1189,8 @@ end --- (default: `true`) --- @field wrap? boolean --- ---- See |diagnostic-severity|. ---- @field severity vim.diagnostic.Severity +--- See |diagnostic-severity|. If `nil`, go to the diagnostic with the highest severity. +--- @field severity? vim.diagnostic.Severity --- --- If `true`, call |vim.diagnostic.open_float()| after moving. --- If a table, pass the table as the {opts} parameter to |vim.diagnostic.open_float()|. |