feat: Add goto-preview and close_handler

This commit is contained in:
Martin Larsson 2025-05-31 00:25:29 +02:00
parent 8cd86d3088
commit fbc9cb8b16
4 changed files with 77 additions and 3 deletions

View file

@ -0,0 +1,41 @@
local utils = require("utils")
local M = {}
local callbacks = {}
local handle_count = 1
function M.setup()
utils.set_keymap_list({
{
"q",
function()
for _, cb in ipairs(callbacks) do
if cb ~= nil then
cb()
end
end
end,
},
})
end
function M.register_on_close_cb(cb)
assert(type(cb) == "function", "Callback must be a function")
local handle = handle_count
handle_count = handle_count + 1
table.insert(callbacks, cb)
return handle
end
function M.remove_on_close_cb(cb_handle)
assert(type(cb_handle) == "number", "Callback handle must be a number")
assert(cb_handle > 0 and cb_handle <= #callbacks, "Invalid callback handle")
assert(callbacks[cb_handle] ~= nil, "Callback does not exist")
callbacks[cb_handle] = nil
end
return M

View file

@ -0,0 +1,29 @@
local close_handler = require("close_handler")
local close_cb_handle = 0
return {
"rmagatti/goto-preview",
dependencies = { "rmagatti/logger.nvim" },
event = "BufEnter",
config = true, -- necessary as per https://github.com/rmagatti/goto-preview/issues/88
opts = {
post_open_hook = function(_, _)
close_cb_handle = close_handler.register_on_close_cb(function()
require("goto-preview").close_all_win()
end)
end,
post_close_hook = function(_, _)
close_handler.remove_on_close_cb(close_cb_handle)
end,
border = { "", "", "", "", "", "", "", "" },
focus_on_open = true,
stack_floating_preview_windows = false,
preview_window_title = { enable = true, position = "left" },
vim_ui_input = false,
},
keys = {
{ "gp", function() require("goto-preview").goto_preview_definition() end, },
{ "gy", function() require("goto-preview").goto_preview_type_definition() end, },
},
}