commit 50c200fcd4749f98ebf7e9f55fddb10f99085327
parent ee1d389fa603a43c70015008736b8a2ed1cd7eb5
Author: Maria José Solano <majosolano99@gmail.com>
Date: Thu, 8 May 2025 11:42:24 -0500
Merge pull request #33850 from MariaSolOs/on-jump
feat(diagnostic): add `on_jump` callback option
Diffstat:
4 files changed, 44 insertions(+), 22 deletions(-)
diff --git a/runtime/doc/deprecated.txt b/runtime/doc/deprecated.txt
@@ -21,7 +21,7 @@ API
DIAGNOSTICS
-• todo
+• "float" in |vim.diagnostic.JumpOpts|. Use "on_jump" instead.
HIGHLIGHTS
diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt
@@ -465,13 +465,9 @@ Lua module: vim.diagnostic *diagnostic-api*
file or not. Similar to 'wrapscan'.
• {severity}? (`vim.diagnostic.SeverityFilter`) See
|diagnostic-severity|.
- • {float}? (`boolean|vim.diagnostic.Opts.Float`, default: `false`)
- If `true`, call |vim.diagnostic.open_float()| after
- moving. If a table, pass the table as the {opts}
- parameter to |vim.diagnostic.open_float()|. Unless
- overridden, the float will show diagnostics at the new
- cursor position (as if "cursor" were passed to the
- "scope" option).
+ • {on_jump}? (`fun(diagnostic:vim.Diagnostic?, bufnr:integer)`)
+ Optional callback invoked with the diagnostic that was
+ jumped to.
• {winid}? (`integer`, default: `0`) Window ID
*vim.diagnostic.NS*
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
@@ -1075,15 +1075,25 @@ local function goto_diagnostic(diagnostic, opts)
vim.cmd('normal! zv')
end)
- local float_opts = opts.float
- if float_opts then
+ if opts.float then
+ vim.deprecate('opts.float', 'opts.on_jump', '0.14')
+ local float_opts = opts.float ---@type table|boolean
float_opts = type(float_opts) == 'table' and float_opts or {}
- vim.schedule(function()
+
+ opts.on_jump = function(_, bufnr)
M.open_float(vim.tbl_extend('keep', float_opts, {
- bufnr = api.nvim_win_get_buf(winid),
+ bufnr = bufnr,
scope = 'cursor',
focus = false,
}))
+ end
+
+ opts.float = nil ---@diagnostic disable-line
+ end
+
+ if opts.on_jump then
+ vim.schedule(function()
+ opts.on_jump(diagnostic, api.nvim_win_get_buf(winid))
end)
end
end
@@ -1287,7 +1297,9 @@ end
function M.goto_prev(opts)
vim.deprecate('vim.diagnostic.goto_prev()', 'vim.diagnostic.jump()', '0.13')
opts = opts or {}
- opts.float = if_nil(opts.float, true)
+
+ opts.float = if_nil(opts.float, true) ---@diagnostic disable-line
+
goto_diagnostic(M.get_prev(opts), opts)
end
@@ -1360,12 +1372,8 @@ end
--- (default: `false`)
--- @field package _highest? boolean
---
---- If `true`, call |vim.diagnostic.open_float()| after moving.
---- If a table, pass the table as the {opts} parameter to |vim.diagnostic.open_float()|.
---- Unless overridden, the float will show diagnostics at the new cursor
---- position (as if "cursor" were passed to the "scope" option).
---- (default: `false`)
---- @field float? boolean|vim.diagnostic.Opts.Float
+--- Optional callback invoked with the diagnostic that was jumped to.
+--- @field on_jump? fun(diagnostic:vim.Diagnostic?, bufnr:integer)
---
--- Window ID
--- (default: `0`)
@@ -1434,7 +1442,7 @@ end
function M.goto_next(opts)
vim.deprecate('vim.diagnostic.goto_next()', 'vim.diagnostic.jump()', '0.13')
opts = opts or {}
- opts.float = if_nil(opts.float, true)
+ opts.float = if_nil(opts.float, true) ---@diagnostic disable-line
goto_diagnostic(M.get_next(opts), opts)
end
diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua
@@ -7,6 +7,7 @@ local exec_lua = n.exec_lua
local eq = t.eq
local neq = t.neq
local matches = t.matches
+local retry = t.retry
local api = n.api
local pcall_err = t.pcall_err
local fn = n.fn
@@ -1094,7 +1095,7 @@ describe('vim.diagnostic', function()
})
vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
vim.api.nvim_win_set_cursor(0, { 1, 1 })
- vim.diagnostic.jump({ count = 1, float = false })
+ vim.diagnostic.jump({ count = 1 })
local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
return { next.lnum, next.col }
end)
@@ -1111,7 +1112,7 @@ describe('vim.diagnostic', function()
})
vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
vim.api.nvim_win_set_cursor(0, { 1, 1 })
- vim.diagnostic.jump({ count = 1, float = false })
+ vim.diagnostic.jump({ count = 1 })
local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
return { next.lnum, next.col }
end)
@@ -1412,6 +1413,23 @@ describe('vim.diagnostic', function()
end)
)
end)
+
+ it('supports on_jump() handler', function()
+ exec_lua(function()
+ _G.jumped = false
+
+ vim.diagnostic.jump({
+ count = 1,
+ on_jump = function()
+ _G.jumped = true
+ end,
+ })
+ end)
+
+ retry(nil, nil, function()
+ eq(true, exec_lua('return _G.jumped'))
+ end)
+ end)
end)
describe('get()', function()