Skip to content

Commit

Permalink
feat: file opener hooks get access to the last dir visited
Browse files Browse the repository at this point in the history
This makes it possible to add actions to the file opener that depend on
the last directory visited.
  • Loading branch information
mikavilpas committed May 24, 2024
1 parent 4541e44 commit 0abd8e9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 55 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):

> You don't have to set any of these options. The defaults are fine for most
> users.
>
> For advanced configuration, it's recommended to have your Lua language server
> set up so that you can type check your configuration and avoid errors.
You can optionally configure yazi.nvim by setting any of the options below.

Expand Down Expand Up @@ -123,7 +126,7 @@ You can optionally configure yazi.nvim by setting any of the options below.

-- when yazi opened multiple files. The default is to send them to the
-- quickfix list, but if you want to change that, you can define it here
yazi_opened_multiple_files = function(chosen_files, config) end,
yazi_opened_multiple_files = function(chosen_files, config, state) end,
},
},
}
Expand Down
52 changes: 4 additions & 48 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local openers = require('yazi.openers')
local keybinding_helpers = require('yazi.keybinding_helpers')

local M = {}

Expand Down Expand Up @@ -29,61 +30,16 @@ end
---@param config YaziConfig
function M.default_set_keymappings_function(yazi_buffer, config)
vim.keymap.set({ 't' }, '<c-v>', function()
M.open_file_in_vertical_split(config)
keybinding_helpers.open_file_in_vertical_split(config)
end, { buffer = yazi_buffer })

vim.keymap.set({ 't' }, '<c-x>', function()
M.open_file_in_horizontal_split(config)
keybinding_helpers.open_file_in_horizontal_split(config)
end, { buffer = yazi_buffer })

vim.keymap.set({ 't' }, '<c-t>', function()
M.open_file_in_tab(config)
keybinding_helpers.open_file_in_tab(config)
end, { buffer = yazi_buffer })
end

-- This is a utility function that can be used in the set_keymappings_function
-- You can also use it in your own keymappings function
function M.select_current_file_and_close_yazi()
-- select the current file in yazi and close it (enter is the default
-- keybinding for selecting a file)
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes('<enter>', true, false, true),
'n',
true
)
end

---@param config YaziConfig
function M.open_file_in_vertical_split(config)
config.open_file_function = openers.open_file_in_vertical_split
config.hooks.yazi_opened_multiple_files = function(chosen_files)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config)
end
end
M.select_current_file_and_close_yazi()
end

---@param config YaziConfig
function M.open_file_in_horizontal_split(config)
config.open_file_function = openers.open_file_in_horizontal_split
config.hooks.yazi_opened_multiple_files = function(chosen_files)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config)
end
end
M.select_current_file_and_close_yazi()
end

---@param config YaziConfig
function M.open_file_in_tab(config)
config.open_file_function = openers.open_file_in_tab
config.hooks.yazi_opened_multiple_files = function(chosen_files)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config)
end
end
M.select_current_file_and_close_yazi()
end

return M
57 changes: 57 additions & 0 deletions lua/yazi/keybinding_helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local openers = require('yazi.openers')

--- Hacky actions that can be used when yazi is open. They typically select the
--- current file and execute some useful operation on the selected file.
---@class YaziOpenerActions
local YaziOpenerActions = {}

---@param config YaziConfig
function YaziOpenerActions.open_file_in_vertical_split(config)
config.open_file_function = openers.open_file_in_vertical_split
config.hooks.yazi_opened_multiple_files = function(
chosen_files,
_config,
state
)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config, state)
end
end
YaziOpenerActions.select_current_file_and_close_yazi()
end

---@param config YaziConfig
function YaziOpenerActions.open_file_in_horizontal_split(config)
config.open_file_function = openers.open_file_in_horizontal_split
config.hooks.yazi_opened_multiple_files = function(chosen_files)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config)
end
end
YaziOpenerActions.select_current_file_and_close_yazi()
end

---@param config YaziConfig
function YaziOpenerActions.open_file_in_tab(config)
config.open_file_function = openers.open_file_in_tab
config.hooks.yazi_opened_multiple_files = function(chosen_files)
for _, chosen_file in ipairs(chosen_files) do
config.open_file_function(chosen_file, config)
end
end
YaziOpenerActions.select_current_file_and_close_yazi()
end

-- This is a utility function that can be used in the set_keymappings_function
-- You can also use it in your own keymappings function
function YaziOpenerActions.select_current_file_and_close_yazi()
-- select the current file in yazi and close it (enter is the default
-- keybinding for selecting a file)
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes('<enter>', true, false, true),
'n',
true
)
end

return YaziOpenerActions
6 changes: 4 additions & 2 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
-- TODO all config properties are optional when given, but mandatory inside the plugin

---@class YaziConfig
---@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. A random path will be used by default"
---@field public enable_mouse_support? boolean
---@field public open_file_function? fun(chosen_file: string, config: YaziConfig): nil "a function that will be called when a file is chosen in yazi"
---@field public open_file_function? fun(chosen_file: string, config: YaziConfig, state: YaziClosedState): nil "a function that will be called when a file is chosen in yazi"
---@field public set_keymappings_function? fun(buffer: integer, config: YaziConfig): nil "the function that will set the keymappings for the yazi floating window. It will be called after the floating window is created."
---@field public hooks? YaziConfigHooks
---@field public floating_window_scaling_factor? float "the scaling factor for the floating window. 1 means 100%, 0.9 means 90%, etc."
Expand All @@ -14,7 +16,7 @@
---@class YaziConfigHooks
---@field public yazi_opened fun(preselected_path: string | nil, buffer: integer, config: YaziConfig):nil
---@field public yazi_closed_successfully fun(chosen_file: string | nil, config: YaziConfig, state: YaziClosedState): nil
---@field public yazi_opened_multiple_files fun(chosen_files: string[], config: YaziConfig): nil
---@field public yazi_opened_multiple_files fun(chosen_files: string[], config: YaziConfig, state: YaziClosedState): nil

---@alias YaziEvent YaziRenameEvent | YaziMoveEvent | YaziDeleteEvent | YaziTrashEvent | YaziChangeDirectoryEvent

Expand Down
4 changes: 2 additions & 2 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ function M.on_yazi_exited(prev_win, window, config, state)
local chosen_files = vim.fn.readfile(config.chosen_file_path)

if #chosen_files > 1 then
config.hooks.yazi_opened_multiple_files(chosen_files, config)
config.hooks.yazi_opened_multiple_files(chosen_files, config, state)
else
local chosen_file = chosen_files[1]
config.hooks.yazi_closed_successfully(chosen_file, config, state)
if chosen_file then
config.open_file_function(chosen_file, config)
config.open_file_function(chosen_file, config, state)
end
end
else
Expand Down
4 changes: 2 additions & 2 deletions tests/yazi/yazi_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('opening a file', function()

assert
.spy(spy_open_file_function)
.was_called_with(target_file, match.is_table())
.was_called_with(target_file, match.is_table(), match.is_table())
end)
end)

Expand Down Expand Up @@ -188,6 +188,6 @@ describe('opening multiple files', function()
assert.spy(spy_open_multiple_files).was_called_with({
target_file_1,
target_file_2,
}, match.is_table())
}, match.is_table(), match.is_table())
end)
end)

0 comments on commit 0abd8e9

Please sign in to comment.