Skip to content

Commit

Permalink
feat: add keymap for changing cwd to current directory (#474)
Browse files Browse the repository at this point in the history
Co-authored-by: Mika Vilpas <mika.vilpas@gmail.com>
  • Loading branch information
chenxin-yan and mikavilpas authored Sep 22, 2024
1 parent f7f05ce commit d63165d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ You can optionally configure yazi.nvim by setting any of the options below.
cycle_open_buffers = '<tab>',
copy_relative_path_to_selected_files = '<c-y>',
send_to_quickfix_list = '<c-q>',
change_working_directory = "<c-\\>",
},

-- completely override the keymappings for yazi. This function will be
Expand Down
12 changes: 11 additions & 1 deletion lua/yazi/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function M.default()
cycle_open_buffers = "<tab>",
copy_relative_path_to_selected_files = "<c-y>",
send_to_quickfix_list = "<c-q>",
change_working_directory = "<c-\\>",
},
set_keymappings_function = nil,
hooks = {
Expand Down Expand Up @@ -179,6 +180,12 @@ function M.set_keymappings(yazi_buffer, config, context)
end, { buffer = yazi_buffer })
end

if config.keymaps.change_working_directory ~= false then
vim.keymap.set({ "t" }, config.keymaps.change_working_directory, function()
keybinding_helpers.change_working_directory(context)
end, { buffer = yazi_buffer })
end

if config.keymaps.show_help ~= false then
vim.keymap.set({ "t" }, config.keymaps.show_help, function()
local w = vim.api.nvim_win_get_width(0)
Expand All @@ -191,7 +198,7 @@ function M.set_keymappings(yazi_buffer, config, context)
bufpos = { 5, 30 },
noautocmd = true,
width = math.min(46, math.floor(w * 0.5)),
height = math.min(13, math.floor(h * 0.5)),
height = math.min(14, math.floor(h * 0.5)),
border = config.yazi_floating_window_border,
})

Expand Down Expand Up @@ -226,6 +233,9 @@ function M.set_keymappings(yazi_buffer, config, context)
""
.. show(config.keymaps.copy_relative_path_to_selected_files)
.. " - copy relative path to selected file(s)",
""
.. show(config.keymaps.change_working_directory)
.. " - change cwd to current directory",
"" .. show(config.keymaps.show_help) .. " - show this help",
"",
"version *" .. require("yazi").version .. "*",
Expand Down
9 changes: 9 additions & 0 deletions lua/yazi/keybinding_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,13 @@ function YaziOpenerActions.replace_in_selected_files(config, chosen_files)
end
end

---@param context YaziActiveContext
function YaziOpenerActions.change_working_directory(context)
local last_directory = context.ya_process.cwd
if last_directory then
vim.notify('cwd changed to "' .. last_directory .. '"')
vim.cmd({ cmd = "cd", args = { last_directory } })
end
end

return YaziOpenerActions
1 change: 1 addition & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
---@field cycle_open_buffers? YaziKeymap # When Neovim has multiple splits open and visible, make yazi jump to the directory of the next one
---@field copy_relative_path_to_selected_files? YaziKeymap # Copy the relative paths of the selected files to the clipboard
---@field send_to_quickfix_list? YaziKeymap # Send the selected files to the quickfix list for later processing
---@field change_working_directory? YaziKeymap # Change working directory to the directory opened by yazi

---@class (exact) YaziActiveContext # context state for a single yazi session
---@field api YaziProcessApi
Expand Down
50 changes: 50 additions & 0 deletions spec/yazi/keybinding_helpers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ local match = require("luassert.match")
local stub = require("luassert.stub")

describe("keybinding_helpers", function()
local vim_cmd_stub
local vim_notify_stub
local snapshot

before_each(function()
snapshot = assert:snapshot()
vim_notify_stub = stub(vim, "notify")
vim_cmd_stub = stub(vim, "cmd")
end)

after_each(function()
snapshot:revert()
end)

describe("grep_in_directory", function()
it("should grep in the parent directory for a file", function()
local config = config_module.default()
Expand Down Expand Up @@ -118,4 +132,40 @@ describe("keybinding_helpers", function()
assert.same({ "/tmp/file1", "/tmp/file2" }, paths)
end)
end)

describe("change_working_directory", function()
it(
"should change the working directory when the cwd is available from ya",
function()
-- When yazi changes a directory, knowledge of that cwd becomes
-- available to the plugin. This information is used to update the cwd
-- in neovim

---@diagnostic disable-next-line: missing-fields
keybinding_helpers.change_working_directory({
---@diagnostic disable-next-line: missing-fields
ya_process = { cwd = "/tmp" },
})

assert
.stub(vim_cmd_stub)
.was_called_with({ cmd = "cd", args = { "/tmp" } })

assert.stub(vim_notify_stub).was_called_with('cwd changed to "/tmp"')
end
)

it("should not crash when the cwd is not available", function()
---@diagnostic disable-next-line: missing-fields
keybinding_helpers.change_working_directory({
---@diagnostic disable-next-line: missing-fields
ya_process = {
cwd = nil,
},
})

assert.stub(vim_cmd_stub).was_not_called()
assert.stub(vim_notify_stub).was_not_called()
end)
end)
end)

0 comments on commit d63165d

Please sign in to comment.