diff --git a/README.md b/README.md index 39888bfd..c316cd5f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ I forked this from for my own use. -So far I have done some maintenance work: +So far I have done some maintenance work and added a bunch of features: - chore: removed unused code - feat: yazi pre-selects the current file when opened @@ -12,6 +12,7 @@ So far I have done some maintenance work: - feat: can optionally open yazi instead of netrw for directories - feat: health check for yazi - feat: files renamed in yazi are kept in sync with open buffers +- feat: allow customizing the method of opening the selected file in neovim If you'd like to collaborate, contact me via GitHub issues. @@ -53,7 +54,13 @@ Using lazy.nvim: -- the path to a temporary file that will be created by yazi to store -- events - events_file_path = '/tmp/yazi.nvim.events.txt' + events_file_path = '/tmp/yazi.nvim.events.txt', + + hooks = { + -- if you want to execute a custom action when yazi has been closed + -- successfully, you can define it here + yazi_closed_successfully = function(chosen_file) end, + }, }, } ``` diff --git a/lua/yazi.lua b/lua/yazi.lua index c9c378f9..1051dca4 100644 --- a/lua/yazi.lua +++ b/lua/yazi.lua @@ -53,6 +53,7 @@ function M.yazi(path) code == 0 and utils.file_exists(M.config.chosen_file_path) == true then local chosen_file = vim.fn.readfile(M.config.chosen_file_path)[1] + M.config.hooks.yazi_closed_successfully(chosen_file) if chosen_file then vim.cmd(string.format('edit %s', chosen_file)) end diff --git a/lua/yazi/config.lua b/lua/yazi/config.lua index fcae2559..21319152 100644 --- a/lua/yazi/config.lua +++ b/lua/yazi/config.lua @@ -2,10 +2,15 @@ local M = {} ---@return YaziConfig function M.default() + ---@type YaziConfig return { open_for_directories = false, chosen_file_path = '/tmp/yazi_filechosen', events_file_path = '/tmp/yazi.nvim.events.txt', + hooks = { + ---@diagnostic disable-next-line: unused-local + yazi_closed_successfully = function(_chosen_file) end, + }, } end diff --git a/lua/yazi/types.lua b/lua/yazi/types.lua index cac66791..72bb11d8 100644 --- a/lua/yazi/types.lua +++ b/lua/yazi/types.lua @@ -2,6 +2,10 @@ ---@field public open_for_directories? boolean ---@field public chosen_file_path? string "the path to a temporary file that will be created by yazi to store the chosen file path" ---@field public events_file_path? string "the path to a temporary file that will be created by yazi to store events" +---@field public hooks? YaziConfigHooks + +---@class YaziConfigHooks +---@field public yazi_closed_successfully? fun(chosen_file: string | nil): nil ---@class YaziRenameEvent ---@field public type "rename" diff --git a/tests/yazi/example_spec.lua b/tests/yazi/example_spec.lua index 071cc401..03a3056b 100644 --- a/tests/yazi/example_spec.lua +++ b/tests/yazi/example_spec.lua @@ -63,4 +63,27 @@ describe('opening a file', function() assert.equals(target_file, vim.fn.expand('%')) end) end) + + it( + "calls the yazi_closed_successfully hook when a file is selected in yazi's chooser", + function() + local spy_hook = spy.new(function(chosen_file) + assert.equals('/abc/test-file.txt', chosen_file) + end) + + ---@diagnostic disable-next-line: missing-fields + plugin.setup({ + hooks = { + ---@diagnostic disable-next-line: assign-type-mismatch + yazi_closed_successfully = spy_hook, + }, + }) + + vim.api.nvim_command('edit /abc/test-file.txt') + + plugin.yazi() + + assert.spy(spy_hook).was_called_with('/abc/test-file.txt') + end + ) end)