From ce48debc3dfc7694ef568d286db48b2f2a2f0106 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Mon, 8 Apr 2024 20:10:18 +0300 Subject: [PATCH] feat(hooks): add yazi_opened hook --- README.md | 7 +++++-- lua/yazi.lua | 2 ++ lua/yazi/config.lua | 2 ++ lua/yazi/types.lua | 1 + tests/yazi/example_spec.lua | 18 ++++++++++++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5ae06d01..cc5e1d26 100644 --- a/README.md +++ b/README.md @@ -62,8 +62,11 @@ Using lazy.nvim: open_file_function = function(chosen_file) end, hooks = { - -- if you want to execute a custom action when yazi has been closed - -- successfully, you can define it here + -- if you want to execute a custom action when yazi has been opened, + -- you can define it here + yazi_opened = function(preselected_path) end, + + -- when yazi was successfully closed yazi_closed_successfully = function(chosen_file) end, }, }, diff --git a/lua/yazi.lua b/lua/yazi.lua index 18adde4b..c1391eb5 100644 --- a/lua/yazi.lua +++ b/lua/yazi.lua @@ -84,6 +84,8 @@ function M.yazi(path) end end, }) + + M.config.hooks.yazi_opened(path) end vim.schedule(function() vim.cmd('startinsert') diff --git a/lua/yazi/config.lua b/lua/yazi/config.lua index a2bdd5a0..065fd977 100644 --- a/lua/yazi/config.lua +++ b/lua/yazi/config.lua @@ -11,6 +11,8 @@ function M.default() vim.cmd(string.format('edit %s', chosen_file)) end, hooks = { + ---@diagnostic disable-next-line: unused-local + yazi_opened = function(_preselected_path) end, ---@diagnostic disable-next-line: unused-local yazi_closed_successfully = function(_chosen_file) end, }, diff --git a/lua/yazi/types.lua b/lua/yazi/types.lua index a4ac4182..71adb48c 100644 --- a/lua/yazi/types.lua +++ b/lua/yazi/types.lua @@ -6,6 +6,7 @@ ---@field public hooks? YaziConfigHooks ---@class YaziConfigHooks +---@field public yazi_opened? fun(preselected_path: string | nil): nil ---@field public yazi_closed_successfully? fun(chosen_file: string | nil): nil ---@class YaziRenameEvent diff --git a/tests/yazi/example_spec.lua b/tests/yazi/example_spec.lua index a52bebb1..61a4a298 100644 --- a/tests/yazi/example_spec.lua +++ b/tests/yazi/example_spec.lua @@ -87,6 +87,24 @@ describe('opening a file', function() end ) + it('calls the yazi_opened hook when yazi is opened', function() + local spy_hook = spy.new() + + ---@diagnostic disable-next-line: missing-fields + plugin.setup({ + hooks = { + ---@diagnostic disable-next-line: assign-type-mismatch + yazi_opened = spy_hook, + }, + }) + + vim.api.nvim_command('edit /abc/yazi_opened_hook_file.txt') + + plugin.yazi() + + assert.spy(spy_hook).was_called_with('/abc/yazi_opened_hook_file.txt') + end) + it('calls the open_file_function to open the selected file', function() local spy_hook = spy.new(function(chosen_file) assert.equals('/abc/test-file.txt', chosen_file)