From 825d85a0cc8a62309aee985d6f87380ec5fb2bb8 Mon Sep 17 00:00:00 2001 From: jaide Date: Fri, 31 Jan 2025 17:50:54 -0500 Subject: [PATCH] Support custom action functions Problem: There are use cases where you may wish to call a function instead of mapping a command directly to a key action for the finder UI. Solution: Supports configuring a `lspsaga.config.finder.actions` object mapping action names to callable functions. ```lua require('lspsaga').setup({ finder = { keys = { open = "", }, actions = { open = function (fname) print("Selected filename", fname) end }, } }) ``` Additionally, this wraps the final `api.nvim_win_set_cursor(0, pos)` call in `xpcall` given that this may fail if the function does not finish synchronously, like spawning a telescope interface. Fixes #1523 --- lua/lspsaga/finder/init.lua | 21 ++++++++++++++++++--- lua/lspsaga/init.lua | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lua/lspsaga/finder/init.lua b/lua/lspsaga/finder/init.lua index c436b93d8..76ab6b35b 100644 --- a/lua/lspsaga/finder/init.lua +++ b/lua/lspsaga/finder/init.lua @@ -438,16 +438,31 @@ function fd:apply_maps() if inexist and (action == 'split' or action == 'vsplit') then local reuse = box.win_reuse(action) if not reuse then - vim.cmd[action](fname) + if (config.finder.actions or {})[action] then + config.finder.actions[action](fname) + else + vim.cmd[action](fname) + end else api.nvim_win_set_buf(reuse, fn.bufadd(fname)) api.nvim_set_current_win(reuse) end else - vim.cmd[action](fname) + if (config.finder.actions or {})[action] then + config.finder.actions[action](fname) + else + vim.cmd[action](fname) + end end restore() - api.nvim_win_set_cursor(0, pos) + xpcall( + function () + api.nvim_win_set_cursor(0, pos) + end, + function (err) + print(err.message) + end + ) beacon({ pos[1] - 1, 0 }, #api.nvim_get_current_line()) return end diff --git a/lua/lspsaga/init.lua b/lua/lspsaga/init.lua index d54040ac5..1b8ae90de 100644 --- a/lua/lspsaga/init.lua +++ b/lua/lspsaga/init.lua @@ -96,6 +96,7 @@ local default_config = { quit = 'q', close = 'k', }, + actions = {}, }, definition = { width = 0.6,