@@ -10,6 +10,9 @@ local utils = require "nvim-tree.utils"
10
10
local change_dir = require " nvim-tree.actions.change-dir"
11
11
local legacy = require " nvim-tree.legacy"
12
12
local core = require " nvim-tree.core"
13
+ local reloaders = require " nvim-tree.actions.reloaders"
14
+ local copy_paste = require " nvim-tree.actions.copy-paste"
15
+ local collapse_all = require " nvim-tree.actions.collapse-all"
13
16
14
17
local _config = {}
15
18
70
73
71
74
function M .tab_change ()
72
75
if view .is_visible { any_tabpage = true } then
73
- local bufname = vim . api .nvim_buf_get_name (0 )
76
+ local bufname = api .nvim_buf_get_name (0 )
74
77
if bufname :match " Neogit" ~= nil or bufname :match " --graph" ~= nil then
75
78
return
76
79
end
153
156
154
157
local prev_line
155
158
function M .place_cursor_on_node ()
156
- local l = vim . api .nvim_win_get_cursor (0 )[1 ]
159
+ local l = api .nvim_win_get_cursor (0 )[1 ]
157
160
if l == prev_line then
158
161
return
159
162
end
@@ -252,19 +255,29 @@ local function manage_netrw(disable_netrw, hijack_netrw)
252
255
end
253
256
254
257
local function setup_vim_commands ()
255
- vim .cmd [[
256
- command! -nargs=? -complete=dir NvimTreeOpen lua require'nvim-tree'.open("<args>")
257
- command! NvimTreeClose lua require'nvim-tree.view'.close()
258
- command! NvimTreeToggle lua require'nvim-tree'.toggle(false)
259
- command! NvimTreeFocus lua require'nvim-tree'.focus()
260
- command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer()
261
- command! NvimTreeClipboard lua require'nvim-tree.actions.copy-paste'.print_clipboard()
262
- command! NvimTreeFindFile lua require'nvim-tree'.find_file(true)
263
- command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true)
264
- command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize("<args>")
265
- command! NvimTreeCollapse lua require'nvim-tree.actions.collapse-all'.fn()
266
- command! NvimTreeCollapseKeepBuffers lua require'nvim-tree.actions.collapse-all'.fn(true)
267
- ]]
258
+ api .nvim_create_user_command (" NvimTreeOpen" , function (res )
259
+ M .open (res .args )
260
+ end , { nargs = " ?" , complete = " dir" })
261
+ api .nvim_create_user_command (" NvimTreeClose" , view .close , {})
262
+ api .nvim_create_user_command (" NvimTreeToggle" , function ()
263
+ M .toggle (false )
264
+ end , {})
265
+ api .nvim_create_user_command (" NvimTreeFocus" , M .focus , {})
266
+ api .nvim_create_user_command (" NvimTreeRefresh" , reloaders .reload_explorer , {})
267
+ api .nvim_create_user_command (" NvimTreeClipboard" , copy_paste .print_clipboard , {})
268
+ api .nvim_create_user_command (" NvimTreeFindFile" , function ()
269
+ M .find_file (true )
270
+ end , {})
271
+ api .nvim_create_user_command (" NvimTreeFindFileToggle" , function ()
272
+ M .toggle (true )
273
+ end , {})
274
+ api .nvim_create_user_command (" NvimTreeResize" , function (res )
275
+ M .resize (res .args )
276
+ end , { nargs = 1 })
277
+ api .nvim_create_user_command (" NvimTreeCollapse" , collapse_all .fn , {})
278
+ api .nvim_create_user_command (" NvimTreeCollapseKeepBuffers" , function ()
279
+ collapse_all .fn (true )
280
+ end , {})
268
281
end
269
282
270
283
function M .change_dir (name )
@@ -276,40 +289,53 @@ function M.change_dir(name)
276
289
end
277
290
278
291
local function setup_autocommands (opts )
279
- vim .cmd " augroup NvimTree"
280
- vim .cmd " autocmd!"
292
+ local augroup_id = api .nvim_create_augroup (" NvimTree" , {})
293
+ local function create_nvim_tree_autocmd (name , custom_opts )
294
+ local default_opts = { group = augroup_id }
295
+ api .nvim_create_autocmd (name , vim .tbl_extend (" force" , default_opts , custom_opts ))
296
+ end
281
297
282
298
-- reset highlights when colorscheme is changed
283
- vim .cmd " au ColorScheme * lua require'nvim-tree'.reset_highlight()"
299
+ create_nvim_tree_autocmd (" ColorScheme" , { callback = M .reset_highlight })
300
+
284
301
if opts .auto_reload_on_write then
285
- vim . cmd " au BufWritePost * lua require'nvim-tree.actions. reloaders' .reload_explorer() "
302
+ create_nvim_tree_autocmd ( " BufWritePost" , { callback = reloaders .reload_explorer })
286
303
end
287
- vim .cmd " au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree.actions.reloaders'.reload_git()"
304
+ create_nvim_tree_autocmd (" User" , {
305
+ pattern = { " FugitiveChanged" , " NeogitStatusRefreshed" },
306
+ callback = reloaders .reload_git ,
307
+ })
288
308
289
309
if opts .open_on_tab then
290
- vim . cmd " au TabEnter * lua require'nvim-tree' .tab_change() "
310
+ create_nvim_tree_autocmd ( " TabEnter" , { callback = M .tab_change })
291
311
end
292
312
if opts .hijack_cursor then
293
- vim . cmd " au CursorMoved NvimTree_* lua require'nvim-tree' .place_cursor_on_node() "
313
+ create_nvim_tree_autocmd ( " CursorMoved" , { pattern = " NvimTree_*" , callback = M .place_cursor_on_node })
294
314
end
295
315
if opts .update_cwd then
296
- vim .cmd " au DirChanged * lua require'nvim-tree'.change_dir(vim.loop.cwd())"
316
+ create_nvim_tree_autocmd (" DirChanged" , {
317
+ callback = function ()
318
+ M .change_dir (vim .loop .cwd ())
319
+ end ,
320
+ })
297
321
end
298
322
if opts .update_focused_file .enable then
299
- vim .cmd " au BufEnter * lua require'nvim-tree'.find_file(false)"
323
+ create_nvim_tree_autocmd (" BufEnter" , {
324
+ callback = function ()
325
+ M .find_file (false )
326
+ end ,
327
+ })
300
328
end
301
329
302
330
if not opts .actions .open_file .quit_on_open then
303
- vim . cmd " au BufWipeout NvimTree_* lua require'nvim-tree. view' ._prevent_buffer_override() "
331
+ create_nvim_tree_autocmd ( " BufWipeout" , { pattern = " NvimTree_*" , callback = view ._prevent_buffer_override })
304
332
else
305
- vim . cmd " au BufWipeout NvimTree_* lua require'nvim-tree. view' .abandon_current_window() "
333
+ create_nvim_tree_autocmd ( " BufWipeout" , { pattern = " NvimTree_*" , callback = view .abandon_current_window })
306
334
end
307
335
308
336
if opts .hijack_directories .enable then
309
- vim . cmd " au BufEnter, BufNewFile * lua require'nvim-tree' .open_on_directory() "
337
+ create_nvim_tree_autocmd ({ " BufEnter" , " BufNewFile" }, { callback = M .open_on_directory })
310
338
end
311
-
312
- vim .cmd " augroup end"
313
339
end
314
340
315
341
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
@@ -476,6 +502,11 @@ local function validate_options(conf)
476
502
end
477
503
478
504
function M .setup (conf )
505
+ if vim .fn .has " nvim-0.7" == 0 then
506
+ utils .warn " nvim-tree.lua requires Neovim 0.7 or higher"
507
+ return
508
+ end
509
+
479
510
legacy .migrate_legacy_options (conf or {})
480
511
481
512
validate_options (conf )
0 commit comments