@@ -8,88 +8,80 @@ local get_position = function(source_name)
88 return pos
99end
1010
11+ --- @return neotree.Config.HijackNetrwBehavior
1112M .get_hijack_behavior = function ()
1213 nt .ensure_config ()
13- local option = " filesystem.hijack_netrw_behavior"
14- local hijack_behavior = utils .get_value (nt .config , option , " open_default" , true )
15- if hijack_behavior == " disabled" then
16- return hijack_behavior
17- elseif hijack_behavior == " open_default" then
18- return hijack_behavior
19- elseif hijack_behavior == " open_current" then
20- return hijack_behavior
21- else
22- require (" neo-tree.log" ).error (" Invalid value for " .. option .. " : " .. hijack_behavior )
23- return " disabled"
24- end
14+ return nt .config .filesystem .hijack_netrw_behavior
2515end
2616
27- --- @param path string ? Path to hijack (sometimes bufname doesn ' t set in time)
2817--- @return boolean hijacked Whether the hijack was successful
29- M .hijack = function (path )
18+ M .hijack = function ()
3019 local hijack_behavior = M .get_hijack_behavior ()
3120 if hijack_behavior == " disabled" then
3221 return false
3322 end
3423
3524 -- ensure this is a directory
36- local bufname = vim .api .nvim_buf_get_name (0 )
37- if not utils .truthy (bufname ) then
38- bufname = path or " "
39- end
40- local stats = uv .fs_stat (bufname )
41- if not stats then
42- return false
43- end
44- if stats .type ~= " directory" then
25+ local dir_bufnr = vim .api .nvim_get_current_buf ()
26+ local path_to_hijack = vim .api .nvim_buf_get_name (dir_bufnr )
27+ local stats = uv .fs_stat (path_to_hijack )
28+ if not stats or stats .type ~= " directory" then
4529 return false
4630 end
4731
4832 -- record where we are now
4933 local pos = get_position (" filesystem" )
5034 local should_open_current = hijack_behavior == " open_current" or pos == " current"
51- local winid = vim .api .nvim_get_current_win ()
52- local dir_bufnr = vim .api .nvim_get_current_buf ()
35+ local dir_window = vim .api .nvim_get_current_win ()
5336
5437 -- Now actually open the tree, with a very quick debounce because this may be
5538 -- called multiple times in quick succession.
56- utils .debounce (" hijack_netrw_" .. winid , function ()
39+ utils .debounce (" hijack_netrw_" .. dir_window , function ()
5740 local manager = require (" neo-tree.sources.manager" )
5841 local log = require (" neo-tree.log" )
5942 -- We will want to replace the "directory" buffer with either the "alternate"
6043 -- buffer or a new blank one.
61- local replace_with_bufnr = vim .fn .bufnr (" #" )
44+ local replacement_buffer = vim .fn .bufnr (" #" )
6245 local is_currently_neo_tree = false
63- if replace_with_bufnr > 0 then
64- if vim .bo [replace_with_bufnr ].filetype == " neo-tree" then
46+ if replacement_buffer > 0 then
47+ if vim .bo [replacement_buffer ].filetype == " neo-tree" then
6548 -- don't hijack the current window if it's already a Neo-tree sidebar
66- local _ , position = pcall ( vim .api . nvim_buf_get_var , replace_with_bufnr , " neo_tree_position" )
67- if position ~ = " current" then
68- is_currently_neo_tree = true
49+ local position = vim .b [ replacement_buffer ]. neo_tree_position
50+ if position = = " current" then
51+ replacement_buffer = - 1
6952 else
70- replace_with_bufnr = - 1
53+ is_currently_neo_tree = true
7154 end
7255 end
7356 end
7457 if not should_open_current then
75- if replace_with_bufnr == dir_bufnr or replace_with_bufnr < 1 then
76- replace_with_bufnr = vim .api .nvim_create_buf (true , false )
77- log .trace (" Created new buffer for netrw hijack" , replace_with_bufnr )
58+ if replacement_buffer == dir_bufnr or replacement_buffer < 1 then
59+ replacement_buffer = vim .api .nvim_create_buf (true , false )
60+ log .trace (" Created new buffer for netrw hijack" , replacement_buffer )
7861 end
7962 end
80- if replace_with_bufnr > 0 then
81- log .trace (" Replacing buffer in netrw hijack" , replace_with_bufnr )
82- pcall (vim .api .nvim_win_set_buf , winid , replace_with_bufnr )
63+ if replacement_buffer > 0 then
64+ log .trace (" Replacing buffer in netrw hijack" , replacement_buffer )
65+ pcall (vim .api .nvim_win_set_buf , dir_window , replacement_buffer )
8366 end
84- local remove_dir_buf = vim .schedule_wrap (function ()
67+
68+ -- If a window takes focus (e.g. lazy.nvim installing plugins on startup) in the time between the method call and
69+ -- this debounced callback, we should focus that window over neo-tree.
70+ local current_window = vim .api .nvim_get_current_win ()
71+ local should_restore_cursor = current_window ~= dir_window
72+
73+ local cleanup = vim .schedule_wrap (function ()
8574 log .trace (" Deleting buffer in netrw hijack" , dir_bufnr )
8675 pcall (vim .api .nvim_buf_delete , dir_bufnr , { force = true })
76+ if should_restore_cursor then
77+ vim .api .nvim_set_current_win (current_window )
78+ end
8779 end )
8880
8981 local state
9082 if should_open_current and not is_currently_neo_tree then
9183 log .debug (" hijack_netrw: opening current" )
92- state = manager .get_state (" filesystem" , nil , winid )
84+ state = manager .get_state (" filesystem" , nil , dir_window )
9385 state .current_position = " current"
9486 elseif is_currently_neo_tree then
9587 log .debug (" hijack_netrw: opening in existing Neo-tree" )
@@ -99,7 +91,8 @@ M.hijack = function(path)
9991 manager .close_all_except (" filesystem" )
10092 state = manager .get_state (" filesystem" )
10193 end
102- require (" neo-tree.sources.filesystem" )._navigate_internal (state , bufname , nil , remove_dir_buf )
94+
95+ require (" neo-tree.sources.filesystem" )._navigate_internal (state , path_to_hijack , nil , cleanup )
10396 end , 10 , utils .debounce_strategy .CALL_LAST_ONLY )
10497
10598 return true
0 commit comments