Skip to content

Commit 19dcacf

Browse files
committed
chore: cleanup change dir module
1 parent 736cc84 commit 19dcacf

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

lua/nvim-tree/actions/change-dir.lua

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,63 @@ local M = {
88
current_tab = a.nvim_get_current_tabpage(),
99
}
1010

11-
function M.fn(name, with_open)
11+
local function clean_input_cwd(name)
12+
local root_parent_cwd = vim.fn.fnamemodify(utils.path_remove_trailing(core.get_cwd()), ":h")
13+
if name == ".." and root_parent_cwd then
14+
return vim.fn.expand(root_parent_cwd)
15+
else
16+
return vim.fn.expand(name)
17+
end
18+
end
19+
20+
local function is_window_event(new_tabpage)
21+
local is_event_scope_window = vim.v.event.scope == "window" or vim.v.event.changed_window
22+
return is_event_scope_window and new_tabpage == M.current_tab
23+
end
24+
25+
local function prevent_cwd_change(foldername)
26+
local is_same_cwd = foldername == core.get_cwd()
27+
local is_restricted_above = M.options.restrict_above_cwd and foldername < vim.fn.getcwd(-1, -1)
28+
return is_same_cwd or is_restricted_above
29+
end
30+
31+
function M.fn(input_cwd, with_open)
1232
if not core.get_explorer() then
1333
return
1434
end
1535

16-
local foldername = name == ".." and vim.fn.fnamemodify(utils.path_remove_trailing(core.get_cwd()), ":h") or name
17-
local no_cwd_change = vim.fn.expand(foldername) == core.get_cwd()
18-
or M.options.restrict_above_cwd and foldername < vim.fn.getcwd(-1, -1)
19-
local new_tab = a.nvim_get_current_tabpage()
20-
local is_window = (vim.v.event.scope == "window" or vim.v.event.changed_window) and new_tab == M.current_tab
21-
if no_cwd_change or is_window then
36+
local new_tabpage = a.nvim_get_current_tabpage()
37+
if is_window_event(new_tabpage) then
38+
return
39+
end
40+
41+
local foldername = clean_input_cwd(input_cwd)
42+
if prevent_cwd_change(foldername) then
2243
return
2344
end
24-
M.current_tab = new_tab
45+
46+
M.current_tab = new_tabpage
2547
M.force_dirchange(foldername, with_open)
2648
end
2749

28-
function M.force_dirchange(foldername, with_open)
50+
local function cd(global, path)
51+
vim.cmd(global and "cd" or "lcd", vim.fn.fnameescape(path))
52+
end
53+
54+
local function should_change_dir()
55+
return M.options.enable and vim.tbl_isempty(vim.v.event)
56+
end
57+
58+
function M.force_dirchange(foldername, should_open_view)
2959
local ps = log.profile_start("change dir %s", foldername)
3060

31-
if M.options.enable and vim.tbl_isempty(vim.v.event) then
32-
if M.options.global then
33-
vim.cmd("cd " .. vim.fn.fnameescape(foldername))
34-
else
35-
vim.cmd("lcd " .. vim.fn.fnameescape(foldername))
36-
end
61+
if should_change_dir() then
62+
cd(M.options.global, foldername)
3763
end
64+
3865
core.init(foldername)
39-
if with_open then
66+
67+
if should_open_view then
4068
require("nvim-tree.lib").open()
4169
else
4270
require("nvim-tree.renderer").draw()

lua/nvim-tree/view.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ function M._prevent_buffer_override()
400400
vim.cmd "setlocal nowinfixheight"
401401
M.open { focus_tree = false }
402402
require("nvim-tree.renderer").draw()
403-
a.nvim_win_close(curwin, { force = true })
403+
pcall(a.nvim_win_close, curwin, { force = true })
404404
require("nvim-tree.actions.open-file").fn("edit", bufname)
405405
end)
406406
end

0 commit comments

Comments
 (0)