Skip to content

Commit

Permalink
feat(config): allow customizing the method of opening the file
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 7, 2024
1 parent cb11663 commit 45cc55f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/yazi/example_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 45cc55f

Please sign in to comment.