diff --git a/README.md b/README.md index c316cd5f..5ae06d01 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,11 @@ Using lazy.nvim: -- events events_file_path = '/tmp/yazi.nvim.events.txt', + -- what neovim should do a when a file was opened (selected) in yazi. + -- Defaults to simply opening the file. + -- If you want to open it in a split / new tab, you can define it here. + 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 diff --git a/lua/yazi.lua b/lua/yazi.lua index 1051dca4..18adde4b 100644 --- a/lua/yazi.lua +++ b/lua/yazi.lua @@ -55,7 +55,7 @@ function M.yazi(path) 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)) + M.config.open_file_function(chosen_file) end end diff --git a/lua/yazi/config.lua b/lua/yazi/config.lua index 21319152..a2bdd5a0 100644 --- a/lua/yazi/config.lua +++ b/lua/yazi/config.lua @@ -7,6 +7,9 @@ function M.default() open_for_directories = false, chosen_file_path = '/tmp/yazi_filechosen', events_file_path = '/tmp/yazi.nvim.events.txt', + open_file_function = function(chosen_file) + vim.cmd(string.format('edit %s', chosen_file)) + end, hooks = { ---@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 72bb11d8..a4ac4182 100644 --- a/lua/yazi/types.lua +++ b/lua/yazi/types.lua @@ -2,6 +2,7 @@ ---@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 open_file_function? fun(chosen_file: string): nil "a function that will be called when a file is chosen in yazi" ---@field public hooks? YaziConfigHooks ---@class YaziConfigHooks diff --git a/tests/yazi/example_spec.lua b/tests/yazi/example_spec.lua index 03a3056b..a52bebb1 100644 --- a/tests/yazi/example_spec.lua +++ b/tests/yazi/example_spec.lua @@ -86,4 +86,22 @@ describe('opening a file', function() assert.spy(spy_hook).was_called_with('/abc/test-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) + end) + + ---@diagnostic disable-next-line: missing-fields + plugin.setup({ + ---@diagnostic disable-next-line: assign-type-mismatch + open_file_function = 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)