@@ -8,35 +8,63 @@ local M = {
8
8
current_tab = a .nvim_get_current_tabpage (),
9
9
}
10
10
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 )
12
32
if not core .get_explorer () then
13
33
return
14
34
end
15
35
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
22
43
return
23
44
end
24
- M .current_tab = new_tab
45
+
46
+ M .current_tab = new_tabpage
25
47
M .force_dirchange (foldername , with_open )
26
48
end
27
49
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 )
29
59
local ps = log .profile_start (" change dir %s" , foldername )
30
60
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 )
37
63
end
64
+
38
65
core .init (foldername )
39
- if with_open then
66
+
67
+ if should_open_view then
40
68
require (" nvim-tree.lib" ).open ()
41
69
else
42
70
require (" nvim-tree.renderer" ).draw ()
0 commit comments