1
- local luv = vim .loop
2
- local api = vim .api
3
-
4
1
local lib = require " nvim-tree.lib"
5
2
local log = require " nvim-tree.log"
6
3
local colors = require " nvim-tree.colors"
29
26
30
27
function M .change_root (filepath , bufnr )
31
28
-- skip if current file is in ignore_list
32
- local ft = api .nvim_buf_get_option (bufnr , " filetype" ) or " "
29
+ local ft = vim . api .nvim_buf_get_option (bufnr , " filetype" ) or " "
33
30
for _ , value in pairs (_config .update_focused_file .ignore_list ) do
34
31
if utils .str_find (filepath , value ) or utils .str_find (ft , value ) then
35
32
return
@@ -75,7 +72,7 @@ function M.toggle(find_file, no_focus, cwd, bang)
75
72
if view .is_visible () then
76
73
M .close ()
77
74
else
78
- local previous_buf = api .nvim_get_current_buf ()
75
+ local previous_buf = vim . api .nvim_get_current_buf ()
79
76
M .open (cwd )
80
77
if _config .update_focused_file .enable or find_file then
81
78
M .find_file (false , previous_buf , bang )
@@ -106,8 +103,8 @@ function M.open_replacing_current_buffer(cwd)
106
103
return
107
104
end
108
105
109
- local buf = api .nvim_get_current_buf ()
110
- local bufname = api .nvim_buf_get_name (buf )
106
+ local buf = vim . api .nvim_get_current_buf ()
107
+ local bufname = vim . api .nvim_buf_get_name (buf )
111
108
if bufname == " " or vim .loop .fs_stat (bufname ) == nil then
112
109
return
113
110
end
126
123
127
124
function M .tab_change ()
128
125
if view .is_visible { any_tabpage = true } then
129
- local bufname = api .nvim_buf_get_name (0 )
130
- local ft = api .nvim_buf_get_option (0 , " ft" )
126
+ local bufname = vim . api .nvim_buf_get_name (0 )
127
+ local ft = vim . api .nvim_buf_get_option (0 , " ft" )
131
128
for _ , filter in ipairs (M .config .ignore_buf_on_tab_change ) do
132
129
if bufname :match (filter ) ~= nil or ft :match (filter ) ~= nil then
133
130
return
@@ -140,26 +137,26 @@ end
140
137
141
138
local function find_existing_windows ()
142
139
return vim .tbl_filter (function (win )
143
- local buf = api .nvim_win_get_buf (win )
144
- return api .nvim_buf_get_name (buf ):match " NvimTree" ~= nil
145
- end , api .nvim_list_wins ())
140
+ local buf = vim . api .nvim_win_get_buf (win )
141
+ return vim . api .nvim_buf_get_name (buf ):match " NvimTree" ~= nil
142
+ end , vim . api .nvim_list_wins ())
146
143
end
147
144
148
145
local function is_file_readable (fname )
149
- local stat = luv .fs_stat (fname )
150
- return stat and stat .type == " file" and luv .fs_access (fname , " R" )
146
+ local stat = vim . loop .fs_stat (fname )
147
+ return stat and stat .type == " file" and vim . loop .fs_access (fname , " R" )
151
148
end
152
149
153
150
function M .find_file (with_open , bufnr , bang )
154
151
if not with_open and not core .get_explorer () then
155
152
return
156
153
end
157
154
158
- bufnr = bufnr or api .nvim_get_current_buf ()
159
- if not api .nvim_buf_is_valid (bufnr ) then
155
+ bufnr = bufnr or vim . api .nvim_get_current_buf ()
156
+ if not vim . api .nvim_buf_is_valid (bufnr ) then
160
157
return
161
158
end
162
- local bufname = api .nvim_buf_get_name (bufnr )
159
+ local bufname = vim . api .nvim_buf_get_name (bufnr )
163
160
local filepath = utils .canonical_path (vim .fn .fnamemodify (bufname , " :p" ))
164
161
if not is_file_readable (filepath ) then
165
162
return
@@ -186,8 +183,8 @@ function M.open_on_directory()
186
183
return
187
184
end
188
185
189
- local buf = api .nvim_get_current_buf ()
190
- local bufname = api .nvim_buf_get_name (buf )
186
+ local buf = vim . api .nvim_get_current_buf ()
187
+ local bufname = vim . api .nvim_buf_get_name (buf )
191
188
if vim .fn .isdirectory (bufname ) ~= 1 then
192
189
return
193
190
end
203
200
204
201
local prev_line
205
202
function M .place_cursor_on_node ()
206
- local l = api .nvim_win_get_cursor (0 )[1 ]
203
+ local l = vim . api .nvim_win_get_cursor (0 )[1 ]
207
204
if l == prev_line then
208
205
return
209
206
end
@@ -214,22 +211,22 @@ function M.place_cursor_on_node()
214
211
return
215
212
end
216
213
217
- local line = api .nvim_get_current_line ()
218
- local cursor = api .nvim_win_get_cursor (0 )
214
+ local line = vim . api .nvim_get_current_line ()
215
+ local cursor = vim . api .nvim_win_get_cursor (0 )
219
216
local idx = vim .fn .stridx (line , node .name )
220
217
221
218
if idx >= 0 then
222
- api .nvim_win_set_cursor (0 , { cursor [1 ], idx })
219
+ vim . api .nvim_win_set_cursor (0 , { cursor [1 ], idx })
223
220
end
224
221
end
225
222
226
223
function M .on_enter (netrw_disabled )
227
- local bufnr = api .nvim_get_current_buf ()
228
- local bufname = api .nvim_buf_get_name (bufnr )
229
- local buftype = api .nvim_buf_get_option (bufnr , " filetype" )
224
+ local bufnr = vim . api .nvim_get_current_buf ()
225
+ local bufname = vim . api .nvim_buf_get_name (bufnr )
226
+ local buftype = vim . api .nvim_buf_get_option (bufnr , " filetype" )
230
227
local ft_ignore = _config .ignore_ft_on_setup
231
228
232
- local stats = luv .fs_stat (bufname )
229
+ local stats = vim . loop .fs_stat (bufname )
233
230
local is_dir = stats and stats .type == " directory"
234
231
local is_file = stats and stats .type == " file"
235
232
local cwd
@@ -239,7 +236,7 @@ function M.on_enter(netrw_disabled)
239
236
vim .cmd (" noautocmd cd " .. cwd )
240
237
end
241
238
242
- local lines = not is_dir and api .nvim_buf_get_lines (bufnr , 0 , - 1 , false ) or {}
239
+ local lines = not is_dir and vim . api .nvim_buf_get_lines (bufnr , 0 , - 1 , false ) or {}
243
240
local buf_has_content = # lines > 1 or (# lines == 1 and lines [1 ] ~= " " )
244
241
245
242
local buf_is_dir = is_dir and netrw_disabled
@@ -270,7 +267,7 @@ function M.on_enter(netrw_disabled)
270
267
-- Session that left a NvimTree Buffer opened, reopen with it
271
268
local existing_tree_wins = find_existing_windows ()
272
269
if existing_tree_wins [1 ] then
273
- api .nvim_set_current_win (existing_tree_wins [1 ])
270
+ vim . api .nvim_set_current_win (existing_tree_wins [1 ])
274
271
end
275
272
276
273
if should_open or should_hijack or existing_tree_wins [1 ] ~= nil then
@@ -302,27 +299,27 @@ local function manage_netrw(disable_netrw, hijack_netrw)
302
299
end
303
300
304
301
local function setup_vim_commands ()
305
- api .nvim_create_user_command (" NvimTreeOpen" , function (res )
302
+ vim . api .nvim_create_user_command (" NvimTreeOpen" , function (res )
306
303
M .open (res .args )
307
304
end , { nargs = " ?" , complete = " dir" })
308
- api .nvim_create_user_command (" NvimTreeClose" , view .close , { bar = true })
309
- api .nvim_create_user_command (" NvimTreeToggle" , function (res )
305
+ vim . api .nvim_create_user_command (" NvimTreeClose" , view .close , { bar = true })
306
+ vim . api .nvim_create_user_command (" NvimTreeToggle" , function (res )
310
307
M .toggle (false , false , res .args )
311
308
end , { nargs = " ?" , complete = " dir" })
312
- api .nvim_create_user_command (" NvimTreeFocus" , M .focus , { bar = true })
313
- api .nvim_create_user_command (" NvimTreeRefresh" , reloaders .reload_explorer , { bar = true })
314
- api .nvim_create_user_command (" NvimTreeClipboard" , copy_paste .print_clipboard , { bar = true })
315
- api .nvim_create_user_command (" NvimTreeFindFile" , function (res )
309
+ vim . api .nvim_create_user_command (" NvimTreeFocus" , M .focus , { bar = true })
310
+ vim . api .nvim_create_user_command (" NvimTreeRefresh" , reloaders .reload_explorer , { bar = true })
311
+ vim . api .nvim_create_user_command (" NvimTreeClipboard" , copy_paste .print_clipboard , { bar = true })
312
+ vim . api .nvim_create_user_command (" NvimTreeFindFile" , function (res )
316
313
M .find_file (true , nil , res .bang )
317
314
end , { bang = true , bar = true })
318
- api .nvim_create_user_command (" NvimTreeFindFileToggle" , function (res )
315
+ vim . api .nvim_create_user_command (" NvimTreeFindFileToggle" , function (res )
319
316
M .toggle (true , false , res .args , res .bang )
320
317
end , { bang = true , nargs = " ?" , complete = " dir" })
321
- api .nvim_create_user_command (" NvimTreeResize" , function (res )
318
+ vim . api .nvim_create_user_command (" NvimTreeResize" , function (res )
322
319
M .resize (res .args )
323
320
end , { nargs = 1 , bar = true })
324
- api .nvim_create_user_command (" NvimTreeCollapse" , collapse_all .fn , { bar = true })
325
- api .nvim_create_user_command (" NvimTreeCollapseKeepBuffers" , function ()
321
+ vim . api .nvim_create_user_command (" NvimTreeCollapse" , collapse_all .fn , { bar = true })
322
+ vim . api .nvim_create_user_command (" NvimTreeCollapseKeepBuffers" , function ()
326
323
collapse_all .fn (true )
327
324
end , { bar = true })
328
325
end
@@ -336,10 +333,10 @@ function M.change_dir(name)
336
333
end
337
334
338
335
local function setup_autocommands (opts )
339
- local augroup_id = api .nvim_create_augroup (" NvimTree" , { clear = true })
336
+ local augroup_id = vim . api .nvim_create_augroup (" NvimTree" , { clear = true })
340
337
local function create_nvim_tree_autocmd (name , custom_opts )
341
338
local default_opts = { group = augroup_id }
342
- api .nvim_create_autocmd (name , vim .tbl_extend (" force" , default_opts , custom_opts ))
339
+ vim . api .nvim_create_autocmd (name , vim .tbl_extend (" force" , default_opts , custom_opts ))
343
340
end
344
341
345
342
-- reset highlights when colorscheme is changed
@@ -415,9 +412,9 @@ local function setup_autocommands(opts)
415
412
create_nvim_tree_autocmd (" BufEnter" , {
416
413
pattern = " NvimTree_*" ,
417
414
callback = function ()
418
- local bufnr = api .nvim_get_current_buf ()
415
+ local bufnr = vim . api .nvim_get_current_buf ()
419
416
vim .schedule (function ()
420
- api .nvim_buf_call (bufnr , function ()
417
+ vim . api .nvim_buf_call (bufnr , function ()
421
418
vim .cmd [[ norm! zz]]
422
419
end )
423
420
end )
0 commit comments