Skip to content

Commit

Permalink
fix: use input dir if needed when changing cwd (#487)
Browse files Browse the repository at this point in the history
* fix: use input dir if needed when changing cwd

* fixup! fix: use input dir if needed when changing cwd

* fix unintended change when start yazi with cwd

* test: fix change directory when no last directory is found

* fix error when change cwd on startup

---------

Co-authored-by: Mika Vilpas <[email protected]>
  • Loading branch information
chenxin-yan and mikavilpas authored Oct 2, 2024
1 parent 1f8bcc4 commit 33857bf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
14 changes: 13 additions & 1 deletion lua/yazi/keybinding_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,19 @@ end
---@param context YaziActiveContext
function YaziOpenerActions.change_working_directory(context)
local last_directory = context.ya_process.cwd
if last_directory then
if not last_directory then
assert(
context.input_path,
"No input_path found. Expected yazi to be started with an input_path"
)
if context.input_path:is_file() then
last_directory = context.input_path:parent().filename
else
last_directory = context.input_path.filename
end
end

if last_directory ~= vim.fn.getcwd() then
vim.notify('cwd changed to "' .. last_directory .. '"')
vim.cmd({ cmd = "cd", args = { last_directory } })
end
Expand Down
31 changes: 28 additions & 3 deletions spec/yazi/keybinding_helpers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ local assert = require("luassert")
local config_module = require("yazi.config")
local keybinding_helpers = require("yazi.keybinding_helpers")
local match = require("luassert.match")
local plenary_path = require("plenary.path")
local stub = require("luassert.stub")

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

before_each(function()
snapshot = assert:snapshot()
vim_fn_stub = stub(vim.fn, "getcwd")
vim_notify_stub = stub(vim, "notify")
vim_cmd_stub = stub(vim, "cmd")
end)
Expand Down Expand Up @@ -155,17 +158,39 @@ describe("keybinding_helpers", function()
end
)

it("should not crash when the cwd is not available", function()
it("uses yazi's input_path if no cwd is available yet", function()
---@diagnostic disable-next-line: missing-fields
keybinding_helpers.change_working_directory({
input_path = plenary_path:new("/tmp"),
---@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()
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 change the working directory if the new cwd is already the current one",
function()
vim_fn_stub.returns("/tmp")
---@diagnostic disable-next-line: missing-fields
keybinding_helpers.change_working_directory({
input_path = plenary_path:new("/tmp"),
---@diagnostic disable-next-line: missing-fields
ya_process = {
cwd = "/tmp",
},
})

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

0 comments on commit 33857bf

Please sign in to comment.