vim/diagnostic: only jump to next/prev if there is a visible diagnostic
This commit is contained in:
parent
dfe0e1cd6a
commit
eb021db36b
1 changed files with 16 additions and 11 deletions
|
@ -3,31 +3,36 @@ local M = {}
|
||||||
-- Show/navigate warning and errors by default.
|
-- Show/navigate warning and errors by default.
|
||||||
M.severity = vim.diagnostic.severity.WARN
|
M.severity = vim.diagnostic.severity.WARN
|
||||||
|
|
||||||
-- Make options to pass to goto_next()/goto_prev(). This needs to be a function
|
-- Go to next/prev diagnostic, but only if next item has a visible virtual text.
|
||||||
-- rather than just a table bevause M.severity can change and we always want the
|
-- If we can move, then also center screen at target location.
|
||||||
-- *current* value.
|
local conditional_goto = function(condition, move, opts)
|
||||||
local make_opts = function(opts)
|
opts = vim.tbl_extend('keep', opts or {}, {
|
||||||
local defaults = {
|
|
||||||
wrap = false, -- don't wrap around the begin/end of file
|
wrap = false, -- don't wrap around the begin/end of file
|
||||||
severity = { -- only navigate items with visible virtual text
|
severity = { -- only navigate items with visible virtual text
|
||||||
min = M.severity
|
min = M.severity
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
return vim.tbl_extend('keep', opts or {}, defaults)
|
|
||||||
|
if condition(opts) then
|
||||||
|
move(opts)
|
||||||
|
vim.cmd 'normal zz'
|
||||||
|
else
|
||||||
|
vim.notify(
|
||||||
|
('No more diagnostics [level: %s]'):format(vim.diagnostic.severity[M.severity] or '???'),
|
||||||
|
vim.log.levels.WARN)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Move to the next diagnostic.
|
---Move to the next diagnostic.
|
||||||
---@param opts table\nil: options passed along to `vim.diagnostic.goto_next`.
|
---@param opts table\nil: options passed along to `vim.diagnostic.goto_next`.
|
||||||
M.goto_next= function(opts)
|
M.goto_next= function(opts)
|
||||||
vim.diagnostic.goto_next(make_opts(opts))
|
conditional_goto(vim.diagnostic.get_next_pos, vim.diagnostic.goto_next, opts)
|
||||||
vim.cmd 'normal zz'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---Move to the previous diagnostic.
|
---Move to the previous diagnostic.
|
||||||
---@param opts table|nil: options passed along to `vim.diagnostic.goto_prev`.
|
---@param opts table|nil: options passed along to `vim.diagnostic.goto_prev`.
|
||||||
M.goto_prev= function(opts)
|
M.goto_prev= function(opts)
|
||||||
vim.diagnostic.goto_prev(make_opts(opts))
|
conditional_goto(vim.diagnostic.get_prev_pos, vim.diagnostic.goto_prev, opts)
|
||||||
vim.cmd 'normal zz'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---Show diagnostics in a floating window.
|
---Show diagnostics in a floating window.
|
||||||
|
|
Loading…
Add table
Reference in a new issue