@@ -6,7 +6,7 @@ local colors = require'nvim-tree.colors'
66local renderer = require ' nvim-tree.renderer'
77local view = require ' nvim-tree.view'
88local utils = require ' nvim-tree.utils'
9- local ChangeDir = require ' nvim-tree.actions.change-dir'
9+ local change_dir = require ' nvim-tree.actions.change-dir'
1010
1111local _config = {}
1212
2121M .on_keypress = require ' nvim-tree.actions' .on_keypress
2222
2323function M .toggle (find_file )
24- if view .win_open () then
24+ if view .is_visible () then
2525 view .close ()
2626 else
27- if _config .update_focused_file .enable or find_file then
28- M .find_file (true )
29- end
3027 M .open ()
28+ if TreeExplorer and (_config .update_focused_file .enable or find_file ) then
29+ M .find_file (false )
30+ end
3131 end
3232end
3333
34- function M .open ()
35- if not view .win_open () then
36- lib .open ()
34+ function M .open (cwd )
35+ cwd = cwd ~= " " and cwd or nil
36+ if not view .is_visible () then
37+ lib .open (cwd )
3738 end
3839end
3940
40- local move_cmd = {
41- right = ' h' ,
42- left = ' l' ,
43- top = ' j' ,
44- bottom = ' k' ,
45- }
46-
47- function M ._prevent_buffer_override ()
48- vim .schedule (function ()
49- local curwin = api .nvim_get_current_win ()
50- local curbuf = api .nvim_win_get_buf (curwin )
51-
52- if curwin ~= view .get_winnr () or curbuf == view .View .bufnr then
53- return
54- end
55-
56- if view .is_buf_valid (view .View .bufnr ) then
57- -- pcall necessary to avoid erroring with `mark not set` although no mark are set
58- -- this avoid other issues
59- pcall (api .nvim_win_set_buf , view .get_winnr (), view .View .bufnr )
60- end
61-
62- local bufname = api .nvim_buf_get_name (curbuf )
63- local isdir = vim .fn .isdirectory (bufname ) == 1
64- if isdir or not bufname or bufname == " " then
65- return
66- end
67-
68- if # vim .api .nvim_list_wins () < 2 then
69- local cmd = view .is_vertical () and " vsplit" or " split"
70- vim .cmd (cmd )
71- else
72- vim .cmd (" wincmd " .. move_cmd [view .View .side ])
73- end
74- vim .cmd (" buffer " .. curbuf )
75- view .resize ()
76- end )
77- end
78-
7941function M .tab_change ()
8042 vim .schedule (function ()
81- if not view .win_open () and view .win_open ({ any_tabpage = true }) then
43+ if not view .is_visible () and view .is_visible ({ any_tabpage = true }) then
8244 local bufname = vim .api .nvim_buf_get_name (0 )
8345 if bufname :match (" Neogit" ) ~= nil or bufname :match (" --graph" ) ~= nil then
8446 return
@@ -88,45 +50,14 @@ function M.tab_change()
8850 end )
8951end
9052
91- local function remove_empty_buffer ()
92- if not view .win_open () or # api .nvim_list_wins () ~= 1 then
93- return
94- end
95-
96- local bufs = vim .api .nvim_list_bufs ()
97- for _ , buf in ipairs (bufs ) do
98- if api .nvim_buf_is_valid (buf ) and api .nvim_buf_is_loaded (buf ) then
99- local name = api .nvim_buf_get_name (buf )
100- if name == " " then
101- api .nvim_buf_delete (buf , {})
102- end
103- end
104- end
105- end
106-
107- function M .hijack_current_window ()
108- local View = require ' nvim-tree.view' .View
109- if not View .bufnr then
110- View .bufnr = api .nvim_get_current_buf ()
111- else
112- local bufs = api .nvim_list_bufs ()
113- for _ , buf in ipairs (bufs ) do
114- local bufname = api .nvim_buf_get_name (buf )
115- local stat = luv .fs_stat (bufname )
116- if stat and stat .type == " directory" then
117- api .nvim_buf_delete (buf , { force = true })
118- end
119- end
120- end
121- local current_tab = api .nvim_get_current_tabpage ()
122- if not View .tabpages then
123- View .tabpages = {
124- [current_tab ] = { winnr = api .nvim_get_current_win () }
125- }
126- else
127- View .tabpages [current_tab ] = { winnr = api .nvim_get_current_win () }
128- end
129- vim .defer_fn (remove_empty_buffer , 20 )
53+ local function find_existing_windows ()
54+ return vim .tbl_filter (
55+ function (win )
56+ local buf = api .nvim_win_get_buf (win )
57+ return api .nvim_buf_get_name (buf ):match (" NvimTree" ) ~= nil
58+ end ,
59+ api .nvim_list_wins ()
60+ )
13061end
13162
13263function M .on_enter (netrw_disabled )
@@ -151,13 +82,18 @@ function M.on_enter(netrw_disabled)
15182 local buf_is_empty = bufname == " " and not buf_has_content
15283 local should_be_preserved = vim .tbl_contains (ft_ignore , buftype )
15384 local should_open = _config .open_on_setup and not should_be_preserved and (buf_is_dir or buf_is_empty )
154- local should_hijack = _config .update_to_buf_dir .enable and _config .update_to_buf_dir .auto_open and is_dir and not should_be_preserved
85+ local should_hijack = _config .hijack_directories .enable and _config .hijack_directories .auto_open and is_dir and not should_be_preserved
15586
156- if should_hijack or should_open then
157- M .hijack_current_window ()
87+ -- Session that left a NvimTree Buffer opened, reopen with it
88+ local existing_tree_wins = find_existing_windows ()
89+ if existing_tree_wins [1 ] then
90+ api .nvim_set_current_win (existing_tree_wins [1 ])
15891 end
15992
160- lib .init (should_open or should_hijack , cwd )
93+ if should_open or should_hijack or existing_tree_wins [1 ] ~= nil then
94+ lib .init (true , cwd )
95+ end
96+ M .initialized = true
16197end
16298
16399local function is_file_readable (fname )
@@ -166,19 +102,15 @@ local function is_file_readable(fname)
166102end
167103
168104local function update_base_dir_with_filepath (filepath , bufnr )
169- if not _config .update_focused_file .update_cwd then
170- return
171- end
172-
173105 local ft = api .nvim_buf_get_option (bufnr , ' filetype' ) or " "
174106 for _ , value in pairs (_config .update_focused_file .ignore_list ) do
175107 if utils .str_find (filepath , value ) or utils .str_find (ft , value ) then
176108 return
177109 end
178110 end
179111
180- if not vim .startswith (filepath , TreeExplorer .cwd or vim . loop . cwd () ) then
181- ChangeDir .fn (vim .fn .fnamemodify (filepath , ' :p:h' ))
112+ if not vim .startswith (filepath , TreeExplorer .cwd ) then
113+ change_dir .fn (vim .fn .fnamemodify (filepath , ' :p:h' ))
182114 end
183115end
184116
@@ -194,22 +126,19 @@ function M.find_file(with_open)
194126
195127 if with_open then
196128 M .open ()
197- view .focus ()
198129 end
199130
200- update_base_dir_with_filepath (filepath , bufnr )
131+ if _config .update_focused_file .update_cwd then
132+ update_base_dir_with_filepath (filepath , bufnr )
133+ end
201134 require " nvim-tree.actions.find-file" .fn (filepath )
202135end
203136
204- function M .resize (size )
205- view .View .width = size
206- view .View .height = size
207- view .resize ()
208- end
137+ M .resize = view .resize
209138
210139function M .on_leave ()
211140 vim .defer_fn (function ()
212- if not view .win_open () then
141+ if not view .is_visible () then
213142 return
214143 end
215144
@@ -226,35 +155,24 @@ function M.on_leave()
226155 end , 50 )
227156end
228157
229- -- TODO: rewrite this to take into account setup by open
230158function M .open_on_directory ()
231- local should_proceed = _config .update_to_buf_dir .auto_open or view .win_open ( )
232- if not _config . update_to_buf_dir . enable or not should_proceed then
159+ local should_proceed = M . initialized and ( _config .hijack_directories .auto_open or view .is_visible () )
160+ if not should_proceed then
233161 return
234162 end
163+
235164 local buf = api .nvim_get_current_buf ()
236165 local bufname = api .nvim_buf_get_name (buf )
237166 if vim .fn .isdirectory (bufname ) ~= 1 then
238167 return
239168 end
240169
241- view .close ()
242- if bufname ~= TreeExplorer .cwd then
243- ChangeDir .fn (bufname )
244- end
245-
246- M .hijack_current_window ()
247-
248- view .open ()
249- view .focus ()
250- view .replace_window ()
251-
252- require " nvim-tree.actions.find-file" .fn (bufname )
170+ change_dir .force_dirchange (bufname , true )
253171end
254172
255173function M .reset_highlight ()
256174 colors .setup ()
257- renderer .render_hl (view .View . bufnr )
175+ renderer .render_hl (view .get_bufnr () )
258176end
259177
260178local prev_line
291209
292210local function setup_vim_commands ()
293211 vim .cmd [[
294- command! NvimTreeOpen lua require'nvim-tree'.open()
212+ command! -nargs=? -complete=dir NvimTreeOpen lua require'nvim-tree'.open("<args>" )
295213 command! NvimTreeClose lua require'nvim-tree.view'.close()
296214 command! NvimTreeToggle lua require'nvim-tree'.toggle(false)
297215 command! NvimTreeFocus lua require'nvim-tree'.focus()
@@ -304,7 +222,7 @@ local function setup_vim_commands()
304222end
305223
306224function M .change_dir (name )
307- ChangeDir .fn (name )
225+ change_dir .fn (name )
308226
309227 if _config .update_focused_file .enable then
310228 M .find_file (false )
@@ -337,11 +255,13 @@ local function setup_autocommands(opts)
337255 vim .cmd " au BufEnter * lua require'nvim-tree'.find_file(false)"
338256 end
339257
340- vim .cmd " au BufUnload NvimTree lua require'nvim-tree.view'.View.tabpages = {}"
341258 if not opts .actions .open_file .quit_on_open then
342- vim .cmd " au BufWinEnter,BufWinLeave * lua require'nvim-tree'._prevent_buffer_override()"
259+ vim .cmd " au BufWipeout NvimTree lua require'nvim-tree.view'._prevent_buffer_override()"
260+ end
261+
262+ if opts .hijack_directories .enable then
263+ vim .cmd " au BufEnter,BufNewFile * lua require'nvim-tree'.open_on_directory()"
343264 end
344- vim .cmd " au BufEnter,BufNewFile * lua require'nvim-tree'.open_on_directory()"
345265
346266 vim .cmd " augroup end"
347267end
@@ -351,7 +271,7 @@ local DEFAULT_OPTS = {
351271 hijack_netrw = true ,
352272 open_on_setup = false ,
353273 open_on_tab = false ,
354- update_to_buf_dir = {
274+ hijack_directories = {
355275 enable = true ,
356276 auto_open = true ,
357277 },
@@ -400,30 +320,38 @@ local DEFAULT_OPTS = {
400320 }
401321}
402322
403- function M .setup (conf )
404- local opts = vim .tbl_deep_extend (' force' , DEFAULT_OPTS , conf or {})
323+ local function merge_options (conf )
324+ if conf and conf .update_to_buf_dir then
325+ conf .hijack_directories = conf .update_to_buf_dir
326+ conf .update_to_buf_dir = nil
327+ end
328+ return vim .tbl_deep_extend (' force' , DEFAULT_OPTS , conf or {})
329+ end
405330
406- manage_netrw (opts .disable_netrw , opts .hijack_netrw )
331+ function M .setup (conf )
332+ local opts = merge_options (conf )
407333 local netrw_disabled = opts .disable_netrw or opts .hijack_netrw
408334
409335 _config .update_focused_file = opts .update_focused_file
410336 _config .open_on_setup = opts .open_on_setup
411337 _config .ignore_ft_on_setup = opts .ignore_ft_on_setup
412- _config .update_to_buf_dir = opts .update_to_buf_dir
413- _config .update_to_buf_dir .enable = _config .update_to_buf_dir .enable and netrw_disabled
338+ _config .hijack_directories = opts .hijack_directories
339+ _config .hijack_directories .enable = _config .hijack_directories .enable and netrw_disabled
340+
341+ manage_netrw (opts .disable_netrw , opts .hijack_netrw )
414342
415343 require ' nvim-tree.actions' .setup (opts )
344+ require ' nvim-tree.colors' .setup ()
416345 require ' nvim-tree.diagnostics' .setup (opts )
417346 require ' nvim-tree.explorer' .setup (opts )
418347 require ' nvim-tree.git' .setup (opts )
419348 require ' nvim-tree.view' .setup (opts )
349+
420350 setup_vim_commands ()
351+ setup_autocommands (opts )
421352
422353 vim .schedule (function ()
423- require ' nvim-tree.colors' .setup ()
424- require ' nvim-tree.view' .create_buffer ()
425354 M .on_enter (netrw_disabled )
426- setup_autocommands (opts )
427355 end )
428356end
429357
0 commit comments