@@ -6,7 +6,7 @@ local colors = require'nvim-tree.colors'
6
6
local renderer = require ' nvim-tree.renderer'
7
7
local view = require ' nvim-tree.view'
8
8
local utils = require ' nvim-tree.utils'
9
- local ChangeDir = require ' nvim-tree.actions.change-dir'
9
+ local change_dir = require ' nvim-tree.actions.change-dir'
10
10
11
11
local _config = {}
12
12
21
21
M .on_keypress = require ' nvim-tree.actions' .on_keypress
22
22
23
23
function M .toggle (find_file )
24
- if view .win_open () then
24
+ if view .is_visible () then
25
25
view .close ()
26
26
else
27
- if _config .update_focused_file .enable or find_file then
28
- M .find_file (true )
29
- end
30
27
M .open ()
28
+ if TreeExplorer and (_config .update_focused_file .enable or find_file ) then
29
+ M .find_file (false )
30
+ end
31
31
end
32
32
end
33
33
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 )
37
38
end
38
39
end
39
40
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
-
79
41
function M .tab_change ()
80
42
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
82
44
local bufname = vim .api .nvim_buf_get_name (0 )
83
45
if bufname :match (" Neogit" ) ~= nil or bufname :match (" --graph" ) ~= nil then
84
46
return
@@ -88,45 +50,14 @@ function M.tab_change()
88
50
end )
89
51
end
90
52
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
+ )
130
61
end
131
62
132
63
function M .on_enter (netrw_disabled )
@@ -151,13 +82,18 @@ function M.on_enter(netrw_disabled)
151
82
local buf_is_empty = bufname == " " and not buf_has_content
152
83
local should_be_preserved = vim .tbl_contains (ft_ignore , buftype )
153
84
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
155
86
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 ])
158
91
end
159
92
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
161
97
end
162
98
163
99
local function is_file_readable (fname )
@@ -166,19 +102,15 @@ local function is_file_readable(fname)
166
102
end
167
103
168
104
local function update_base_dir_with_filepath (filepath , bufnr )
169
- if not _config .update_focused_file .update_cwd then
170
- return
171
- end
172
-
173
105
local ft = api .nvim_buf_get_option (bufnr , ' filetype' ) or " "
174
106
for _ , value in pairs (_config .update_focused_file .ignore_list ) do
175
107
if utils .str_find (filepath , value ) or utils .str_find (ft , value ) then
176
108
return
177
109
end
178
110
end
179
111
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' ))
182
114
end
183
115
end
184
116
@@ -194,22 +126,19 @@ function M.find_file(with_open)
194
126
195
127
if with_open then
196
128
M .open ()
197
- view .focus ()
198
129
end
199
130
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
201
134
require " nvim-tree.actions.find-file" .fn (filepath )
202
135
end
203
136
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
209
138
210
139
function M .on_leave ()
211
140
vim .defer_fn (function ()
212
- if not view .win_open () then
141
+ if not view .is_visible () then
213
142
return
214
143
end
215
144
@@ -226,35 +155,24 @@ function M.on_leave()
226
155
end , 50 )
227
156
end
228
157
229
- -- TODO: rewrite this to take into account setup by open
230
158
function 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
233
161
return
234
162
end
163
+
235
164
local buf = api .nvim_get_current_buf ()
236
165
local bufname = api .nvim_buf_get_name (buf )
237
166
if vim .fn .isdirectory (bufname ) ~= 1 then
238
167
return
239
168
end
240
169
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 )
253
171
end
254
172
255
173
function M .reset_highlight ()
256
174
colors .setup ()
257
- renderer .render_hl (view .View . bufnr )
175
+ renderer .render_hl (view .get_bufnr () )
258
176
end
259
177
260
178
local prev_line
291
209
292
210
local function setup_vim_commands ()
293
211
vim .cmd [[
294
- command! NvimTreeOpen lua require'nvim-tree'.open()
212
+ command! -nargs=? -complete=dir NvimTreeOpen lua require'nvim-tree'.open("<args>" )
295
213
command! NvimTreeClose lua require'nvim-tree.view'.close()
296
214
command! NvimTreeToggle lua require'nvim-tree'.toggle(false)
297
215
command! NvimTreeFocus lua require'nvim-tree'.focus()
@@ -304,7 +222,7 @@ local function setup_vim_commands()
304
222
end
305
223
306
224
function M .change_dir (name )
307
- ChangeDir .fn (name )
225
+ change_dir .fn (name )
308
226
309
227
if _config .update_focused_file .enable then
310
228
M .find_file (false )
@@ -337,11 +255,13 @@ local function setup_autocommands(opts)
337
255
vim .cmd " au BufEnter * lua require'nvim-tree'.find_file(false)"
338
256
end
339
257
340
- vim .cmd " au BufUnload NvimTree lua require'nvim-tree.view'.View.tabpages = {}"
341
258
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()"
343
264
end
344
- vim .cmd " au BufEnter,BufNewFile * lua require'nvim-tree'.open_on_directory()"
345
265
346
266
vim .cmd " augroup end"
347
267
end
@@ -351,7 +271,7 @@ local DEFAULT_OPTS = {
351
271
hijack_netrw = true ,
352
272
open_on_setup = false ,
353
273
open_on_tab = false ,
354
- update_to_buf_dir = {
274
+ hijack_directories = {
355
275
enable = true ,
356
276
auto_open = true ,
357
277
},
@@ -400,30 +320,38 @@ local DEFAULT_OPTS = {
400
320
}
401
321
}
402
322
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
405
330
406
- manage_netrw (opts .disable_netrw , opts .hijack_netrw )
331
+ function M .setup (conf )
332
+ local opts = merge_options (conf )
407
333
local netrw_disabled = opts .disable_netrw or opts .hijack_netrw
408
334
409
335
_config .update_focused_file = opts .update_focused_file
410
336
_config .open_on_setup = opts .open_on_setup
411
337
_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 )
414
342
415
343
require ' nvim-tree.actions' .setup (opts )
344
+ require ' nvim-tree.colors' .setup ()
416
345
require ' nvim-tree.diagnostics' .setup (opts )
417
346
require ' nvim-tree.explorer' .setup (opts )
418
347
require ' nvim-tree.git' .setup (opts )
419
348
require ' nvim-tree.view' .setup (opts )
349
+
420
350
setup_vim_commands ()
351
+ setup_autocommands (opts )
421
352
422
353
vim .schedule (function ()
423
- require ' nvim-tree.colors' .setup ()
424
- require ' nvim-tree.view' .create_buffer ()
425
354
M .on_enter (netrw_disabled )
426
- setup_autocommands (opts )
427
355
end )
428
356
end
429
357
0 commit comments