1
1
local luv = vim .loop
2
+ local api = vim .api
3
+
2
4
local lib = require ' nvim-tree.lib'
3
5
local config = require ' nvim-tree.config'
4
6
local colors = require ' nvim-tree.colors'
5
7
local renderer = require ' nvim-tree.renderer'
6
8
local fs = require ' nvim-tree.fs'
7
- local utils = require ' nvim-tree.utils'
8
9
local view = require ' nvim-tree.view'
9
10
10
- local api = vim .api
11
+ local _config = {
12
+ is_windows = vim .fn .has (' win32' ) == 1 or vim .fn .has (' win32unix' ) == 1 ,
13
+ is_macos = vim .fn .has (' mac' ) == 1 or vim .fn .has (' macunix' ) == 1 ,
14
+ is_unix = vim .fn .has (' unix' ) == 1 ,
15
+ update_focused_file = {
16
+ enable = false ,
17
+ update_cwd = false ,
18
+ ignore_list = {},
19
+ },
20
+ system_open = {},
21
+ ignore_ft_on_setup = {},
22
+ open_on_setup = {},
23
+ }
11
24
12
25
local M = {}
13
26
@@ -22,7 +35,7 @@ function M.toggle()
22
35
if view .win_open () then
23
36
view .close ()
24
37
else
25
- if vim . g . nvim_tree_follow == 1 then
38
+ if _config . update_focused_file . enable then
26
39
M .find_file (true )
27
40
end
28
41
if not view .win_open () then
@@ -58,7 +71,7 @@ function M.tab_change()
58
71
end )
59
72
end
60
73
61
- local function gen_go_to (mode )
74
+ local function go_to (mode )
62
75
local icon_state = config .get_icon_state ()
63
76
local flags = mode == ' prev_git_item' and ' b' or ' '
64
77
local icons = table.concat (vim .tbl_values (icon_state .icons .git_icons ), ' \\ |' )
@@ -88,36 +101,40 @@ local keypress_funcs = {
88
101
last_sibling = function (node ) lib .sibling (node , math.huge ) end ,
89
102
prev_sibling = function (node ) lib .sibling (node , - 1 ) end ,
90
103
next_sibling = function (node ) lib .sibling (node , 1 ) end ,
91
- prev_git_item = gen_go_to (' prev_git_item' ),
92
- next_git_item = gen_go_to (' next_git_item' ),
104
+ prev_git_item = go_to (' prev_git_item' ),
105
+ next_git_item = go_to (' next_git_item' ),
93
106
dir_up = lib .dir_up ,
94
107
close = function () M .close () end ,
95
108
preview = function (node )
96
109
if node .entries ~= nil or node .name == ' ..' then return end
97
110
return lib .open_file (' preview' , node .absolute_path )
98
111
end ,
99
112
system_open = function (node )
100
- if vim .g .nvim_tree_system_open_command == nil then
101
- if vim .fn .has (' win32' ) == 1 or vim .fn .has (' win32unix' ) == 1 then
102
- vim .g .nvim_tree_system_open_command = ' cmd'
103
- vim .g .nvim_tree_system_open_command_args = {' /c' , ' start' , ' ""' }
104
- elseif vim .fn .has (' mac' ) == 1 or vim .fn .has (' macunix' ) == 1 then
105
- vim .g .nvim_tree_system_open_command = ' open'
106
- elseif vim .fn .has (' unix' ) == 1 then
107
- vim .g .nvim_tree_system_open_command = ' xdg-open'
113
+ if not _config .system_open .cmd then
114
+ if _config .is_windows then
115
+ _config .system_open = {
116
+ cmd = " cmd" ,
117
+ args = {' /c' , ' start' , ' ""' }
118
+ }
119
+ elseif _config .is_macos then
120
+ _config .system_open .cmd = ' open'
121
+ elseif _config .is_linux then
122
+ _config .system_open .cmd = ' xdg-open'
108
123
else
109
- error ( ' \n NvimTree system_open: cannot open file with system application. Unrecognized platform.\n Please fill g:nvim_tree_system_open_command with the name of the system file launcher. ' )
124
+ require ' nvim-tree.utils ' . echo_warning ( " Cannot open file with system application. Unrecognized platform." )
110
125
return
111
126
end
112
127
end
113
128
114
- local process = {}
115
- process .args = vim .g .nvim_tree_system_open_command_args or {}
129
+ local process = {
130
+ cmd = _config .system_open .cmd ,
131
+ args = _config .system_open .args ,
132
+ errors = ' \n ' ,
133
+ stderr = luv .new_pipe (false )
134
+ }
116
135
table.insert (process .args , node .link_to or node .absolute_path )
117
- process .errors = ' \n '
118
- process .stderr = luv .new_pipe (false )
119
- process .handle , process .pid = luv .spawn (vim .g .nvim_tree_system_open_command ,
120
- {args = process .args , stdio = {nil , nil , process .stderr }},
136
+ process .handle , process .pid = luv .spawn (process .cmd ,
137
+ { args = process .args , stdio = { nil , nil , process .stderr }},
121
138
function (code )
122
139
process .stderr :read_stop ()
123
140
process .stderr :close ()
@@ -129,7 +146,7 @@ local keypress_funcs = {
129
146
end
130
147
)
131
148
if not process .handle then
132
- error (" \n " .. process .pid .. " \n NvimTree system_open: failed to spawn process using '" .. vim . g . nvim_tree_system_open_command .. " '." )
149
+ error (" \n " .. process .pid .. " \n NvimTree system_open: failed to spawn process using '" .. process . cmd .. " '." )
133
150
return
134
151
end
135
152
luv .read_start (process .stderr ,
@@ -190,29 +207,31 @@ function M.hijack_current_window()
190
207
end
191
208
end
192
209
193
- function M .on_enter ()
210
+ function M .on_enter (opts )
194
211
local bufnr = api .nvim_get_current_buf ()
195
212
local bufname = api .nvim_buf_get_name (bufnr )
196
213
local buftype = api .nvim_buf_get_option (bufnr , ' filetype' )
197
- local ft_ignore = vim . g . nvim_tree_auto_ignore_ft or {}
214
+ local ft_ignore = _config . ignore_ft_on_setup
198
215
199
216
local stats = luv .fs_stat (bufname )
200
217
local is_dir = stats and stats .type == ' directory'
201
-
202
- local disable_netrw = vim .g .nvim_tree_disable_netrw or 1
203
- local hijack_netrw = vim .g .nvim_tree_hijack_netrw or 1
204
218
if is_dir then
205
219
lib .Tree .cwd = vim .fn .expand (bufname )
206
220
end
207
- local netrw_disabled = hijack_netrw == 1 or disable_netrw == 1
221
+
222
+ local netrw_disabled = opts .disable_netrw or opts .hijack_netrw
223
+
208
224
local lines = not is_dir and api .nvim_buf_get_lines (bufnr , 0 , - 1 , false ) or {}
209
225
local buf_has_content = # lines > 1 or (# lines == 1 and lines [1 ] ~= " " )
210
- local should_open = vim .g .nvim_tree_auto_open == 1
226
+
227
+ local should_open = _config .open_on_setup
211
228
and ((is_dir and netrw_disabled ) or (bufname == " " and not buf_has_content ))
212
229
and not vim .tbl_contains (ft_ignore , buftype )
230
+
213
231
if should_open then
214
232
M .hijack_current_window ()
215
233
end
234
+
216
235
lib .init (should_open , should_open )
217
236
end
218
237
@@ -221,17 +240,26 @@ local function is_file_readable(fname)
221
240
return stat and stat .type == " file" and luv .fs_access (fname , ' R' )
222
241
end
223
242
224
- local function update_base_dir_with_filepath (filepath )
225
- if vim . g . nvim_tree_follow_update_path ~= 1 then
243
+ local function update_base_dir_with_filepath (filepath , bufnr )
244
+ if not _config . update_focused_file . update_cwd then
226
245
return
227
246
end
247
+
248
+ local ft = api .nvim_buf_get_option (bufnr , ' filetype' ) or " "
249
+ for _ , value in pairs (_config .update_focused_file .ignore_list ) do
250
+ if vim .fn .stridx (filepath , value ) ~= - 1 or vim .fn .stridx (ft , value ) ~= - 1 then
251
+ return
252
+ end
253
+ end
254
+
228
255
if not vim .startswith (filepath , lib .Tree .cwd or vim .loop .cwd ()) then
229
256
lib .change_dir (vim .fn .fnamemodify (filepath , ' :p:h' ))
230
257
end
231
258
end
232
259
233
260
function M .find_file (with_open )
234
261
local bufname = vim .fn .bufname ()
262
+ local bufnr = api .nvim_get_current_buf ()
235
263
local filepath = vim .fn .fnamemodify (bufname , ' :p' )
236
264
237
265
if with_open then
@@ -242,7 +270,7 @@ function M.find_file(with_open)
242
270
if not is_file_readable (filepath ) then
243
271
return
244
272
end
245
- update_base_dir_with_filepath (filepath )
273
+ update_base_dir_with_filepath (filepath , bufnr )
246
274
lib .set_index_and_redraw (filepath )
247
275
end
248
276
@@ -270,22 +298,6 @@ function M.on_leave()
270
298
end , 50 )
271
299
end
272
300
273
- local function update_root_dir ()
274
- local bufname = api .nvim_buf_get_name (api .nvim_get_current_buf ())
275
- if not is_file_readable (bufname ) or not lib .Tree .cwd then return end
276
-
277
- -- this logic is a hack
278
- -- depending on vim-rooter or autochdir, it would not behave the same way when those two are not enabled
279
- -- until i implement multiple workspaces/project, it should stay like this
280
- if bufname :match (utils .path_to_matching_str (lib .Tree .cwd )) then
281
- return
282
- end
283
- local new_cwd = luv .cwd ()
284
- if lib .Tree .cwd == new_cwd then return end
285
-
286
- lib .change_dir (new_cwd )
287
- end
288
-
289
301
function M .open_on_directory ()
290
302
local buf = api .nvim_get_current_buf ()
291
303
local bufname = api .nvim_buf_get_name (buf )
@@ -303,13 +315,6 @@ function M.open_on_directory()
303
315
view .replace_window ()
304
316
end
305
317
306
- function M .buf_enter ()
307
- update_root_dir ()
308
- if vim .g .nvim_tree_follow == 1 then
309
- M .find_file (false )
310
- end
311
- end
312
-
313
318
function M .reset_highlight ()
314
319
colors .setup ()
315
320
renderer .render_hl (view .View .bufnr )
@@ -334,8 +339,100 @@ function M.place_cursor_on_node()
334
339
api .nvim_win_set_cursor (0 , {cursor [1 ], idx })
335
340
end
336
341
337
- view .setup ()
338
- colors .setup ()
339
- vim .defer_fn (M .on_enter , 1 )
342
+ local function manage_netrw (disable_netrw , hijack_netrw )
343
+ if disable_netrw then
344
+ vim .g .loaded_netrw = 1
345
+ vim .g .loaded_netrwPlugin = 1
346
+ elseif hijack_netrw then
347
+ vim .cmd " silent! autocmd! FileExplorer *"
348
+ end
349
+ end
350
+
351
+ local function setup_vim_commands ()
352
+ vim .cmd [[
353
+ command! NvimTreeOpen lua require'nvim-tree'.open()
354
+ command! NvimTreeClose lua require'nvim-tree'.close()
355
+ command! NvimTreeToggle lua require'nvim-tree'.toggle()
356
+ command! NvimTreeFocus lua require'nvim-tree'.focus()
357
+ command! NvimTreeRefresh lua require'nvim-tree'.refresh()
358
+ command! NvimTreeClipboard lua require'nvim-tree'.print_clipboard()
359
+ command! NvimTreeFindFile lua require'nvim-tree'.find_file()
360
+ command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize(<args>)
361
+ ]]
362
+ end
363
+
364
+ local function setup_autocommands (opts )
365
+ vim .cmd " augroup NvimTree"
366
+ vim .cmd [[
367
+ """ reset highlights when colorscheme is changed
368
+ au ColorScheme * lua require'nvim-tree'.reset_highlight()
369
+
370
+ au BufWritePost * lua require'nvim-tree'.refresh()
371
+ au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree'.refresh()
372
+
373
+ """ deletes the existing buffer when saved in a session to avoid conflicts
374
+ au SessionLoadPost * lua require'nvim-tree.view'._wipe_rogue_buffer()
375
+ ]]
376
+
377
+ if vim .g .nvim_tree_lsp_diagnostics ~= 1 then
378
+ vim .cmd " au User LspDiagnosticsChanged lua require'nvim-tree.diagnostics'.update()"
379
+ end
380
+ if opts .auto_close then
381
+ vim .cmd " au WinClosed * lua require'nvim-tree'.on_leave()"
382
+ end
383
+ if opts .tab_open then
384
+ vim .cmd " au TabEnter * lua require'nvim-tree'.tab_change()"
385
+ end
386
+ if opts .hijack_cursor then
387
+ vim .cmd " au CursorMoved NvimTree lua require'nvim-tree'.place_cursor_on_node()"
388
+ end
389
+ if opts .update_cwd then
390
+ vim .cmd " au DirChanged * lua require'nvim-tree.lib'.change_dir(vim.loop.cwd())"
391
+ end
392
+ if opts .update_focused_file .enable then
393
+ vim .cmd " au BufEnter * lua require'nvim-tree'.find_file(false)"
394
+ end
395
+
396
+ vim .cmd " augroup end"
397
+ end
398
+
399
+ local DEFAULT_OPTS = {
400
+ disable_netrw = true ,
401
+ hijack_netrw = true ,
402
+ open_on_setup = false ,
403
+ auto_close = false ,
404
+ tab_open = false ,
405
+ hijack_cursor = false ,
406
+ update_cwd = false ,
407
+ update_focused_file = {
408
+ enable = false ,
409
+ update_cwd = false ,
410
+ ignore_list = {}
411
+ },
412
+ ignore_ft_on_setup = {},
413
+ system_open = {
414
+ cmd = nil ,
415
+ args = {}
416
+ },
417
+ }
418
+
419
+ function M .setup (conf )
420
+ local opts = vim .tbl_deep_extend (' force' , DEFAULT_OPTS , conf or {})
421
+
422
+ manage_netrw (opts .disable_netrw , opts .hijack_netrw )
423
+
424
+ _config .update_focused_file = opts .update_focused_file
425
+ _config .system_open = opts .system_open
426
+ _config .open_on_setup = opts .open_on_setup
427
+ _config .ignore_ft_on_setup = opts .ignore_ft_on_setup
428
+
429
+ require ' nvim-tree.colors' .setup ()
430
+ require ' nvim-tree.view' .setup ()
431
+
432
+ setup_autocommands (opts )
433
+ setup_vim_commands ()
434
+
435
+ M .on_enter (opts )
436
+ end
340
437
341
438
return M
0 commit comments