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
view .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 )
@@ -101,8 +98,8 @@ function M.open_replacing_current_buffer(cwd)
101
98
return
102
99
end
103
100
104
- local buf = api .nvim_get_current_buf ()
105
- local bufname = api .nvim_buf_get_name (buf )
101
+ local buf = vim . api .nvim_get_current_buf ()
102
+ local bufname = vim . api .nvim_buf_get_name (buf )
106
103
if bufname == " " or vim .loop .fs_stat (bufname ) == nil then
107
104
return
108
105
end
121
118
122
119
function M .tab_change ()
123
120
if view .is_visible { any_tabpage = true } then
124
- local bufname = api .nvim_buf_get_name (0 )
125
- local ft = api .nvim_buf_get_option (0 , " ft" )
121
+ local bufname = vim . api .nvim_buf_get_name (0 )
122
+ local ft = vim . api .nvim_buf_get_option (0 , " ft" )
126
123
for _ , filter in ipairs (M .config .ignore_buf_on_tab_change ) do
127
124
if bufname :match (filter ) ~= nil or ft :match (filter ) ~= nil then
128
125
return
@@ -135,26 +132,26 @@ end
135
132
136
133
local function find_existing_windows ()
137
134
return vim .tbl_filter (function (win )
138
- local buf = api .nvim_win_get_buf (win )
139
- return api .nvim_buf_get_name (buf ):match " NvimTree" ~= nil
140
- end , api .nvim_list_wins ())
135
+ local buf = vim . api .nvim_win_get_buf (win )
136
+ return vim . api .nvim_buf_get_name (buf ):match " NvimTree" ~= nil
137
+ end , vim . api .nvim_list_wins ())
141
138
end
142
139
143
140
local function is_file_readable (fname )
144
- local stat = luv .fs_stat (fname )
145
- return stat and stat .type == " file" and luv .fs_access (fname , " R" )
141
+ local stat = vim . loop .fs_stat (fname )
142
+ return stat and stat .type == " file" and vim . loop .fs_access (fname , " R" )
146
143
end
147
144
148
145
function M .find_file (with_open , bufnr , bang )
149
146
if not with_open and not core .get_explorer () then
150
147
return
151
148
end
152
149
153
- bufnr = bufnr or api .nvim_get_current_buf ()
154
- if not api .nvim_buf_is_valid (bufnr ) then
150
+ bufnr = bufnr or vim . api .nvim_get_current_buf ()
151
+ if not vim . api .nvim_buf_is_valid (bufnr ) then
155
152
return
156
153
end
157
- local bufname = api .nvim_buf_get_name (bufnr )
154
+ local bufname = vim . api .nvim_buf_get_name (bufnr )
158
155
local filepath = utils .canonical_path (vim .fn .fnamemodify (bufname , " :p" ))
159
156
if not is_file_readable (filepath ) then
160
157
return
@@ -181,8 +178,8 @@ function M.open_on_directory()
181
178
return
182
179
end
183
180
184
- local buf = api .nvim_get_current_buf ()
185
- local bufname = api .nvim_buf_get_name (buf )
181
+ local buf = vim . api .nvim_get_current_buf ()
182
+ local bufname = vim . api .nvim_buf_get_name (buf )
186
183
if vim .fn .isdirectory (bufname ) ~= 1 then
187
184
return
188
185
end
198
195
199
196
local prev_line
200
197
function M .place_cursor_on_node ()
201
- local l = api .nvim_win_get_cursor (0 )[1 ]
198
+ local l = vim . api .nvim_win_get_cursor (0 )[1 ]
202
199
if l == prev_line then
203
200
return
204
201
end
@@ -209,22 +206,22 @@ function M.place_cursor_on_node()
209
206
return
210
207
end
211
208
212
- local line = api .nvim_get_current_line ()
213
- local cursor = api .nvim_win_get_cursor (0 )
209
+ local line = vim . api .nvim_get_current_line ()
210
+ local cursor = vim . api .nvim_win_get_cursor (0 )
214
211
local idx = vim .fn .stridx (line , node .name )
215
212
216
213
if idx >= 0 then
217
- api .nvim_win_set_cursor (0 , { cursor [1 ], idx })
214
+ vim . api .nvim_win_set_cursor (0 , { cursor [1 ], idx })
218
215
end
219
216
end
220
217
221
218
function M .on_enter (netrw_disabled )
222
- local bufnr = api .nvim_get_current_buf ()
223
- local bufname = api .nvim_buf_get_name (bufnr )
224
- local buftype = api .nvim_buf_get_option (bufnr , " filetype" )
219
+ local bufnr = vim . api .nvim_get_current_buf ()
220
+ local bufname = vim . api .nvim_buf_get_name (bufnr )
221
+ local buftype = vim . api .nvim_buf_get_option (bufnr , " filetype" )
225
222
local ft_ignore = _config .ignore_ft_on_setup
226
223
227
- local stats = luv .fs_stat (bufname )
224
+ local stats = vim . loop .fs_stat (bufname )
228
225
local is_dir = stats and stats .type == " directory"
229
226
local is_file = stats and stats .type == " file"
230
227
local cwd
@@ -234,7 +231,7 @@ function M.on_enter(netrw_disabled)
234
231
vim .cmd (" noautocmd cd " .. cwd )
235
232
end
236
233
237
- local lines = not is_dir and api .nvim_buf_get_lines (bufnr , 0 , - 1 , false ) or {}
234
+ local lines = not is_dir and vim . api .nvim_buf_get_lines (bufnr , 0 , - 1 , false ) or {}
238
235
local buf_has_content = # lines > 1 or (# lines == 1 and lines [1 ] ~= " " )
239
236
240
237
local buf_is_dir = is_dir and netrw_disabled
@@ -265,7 +262,7 @@ function M.on_enter(netrw_disabled)
265
262
-- Session that left a NvimTree Buffer opened, reopen with it
266
263
local existing_tree_wins = find_existing_windows ()
267
264
if existing_tree_wins [1 ] then
268
- api .nvim_set_current_win (existing_tree_wins [1 ])
265
+ vim . api .nvim_set_current_win (existing_tree_wins [1 ])
269
266
end
270
267
271
268
if should_open or should_hijack or existing_tree_wins [1 ] ~= nil then
@@ -297,27 +294,27 @@ local function manage_netrw(disable_netrw, hijack_netrw)
297
294
end
298
295
299
296
local function setup_vim_commands ()
300
- api .nvim_create_user_command (" NvimTreeOpen" , function (res )
297
+ vim . api .nvim_create_user_command (" NvimTreeOpen" , function (res )
301
298
M .open (res .args )
302
299
end , { nargs = " ?" , complete = " dir" })
303
- api .nvim_create_user_command (" NvimTreeClose" , view .close , { bar = true })
304
- api .nvim_create_user_command (" NvimTreeToggle" , function (res )
300
+ vim . api .nvim_create_user_command (" NvimTreeClose" , view .close , { bar = true })
301
+ vim . api .nvim_create_user_command (" NvimTreeToggle" , function (res )
305
302
M .toggle (false , false , res .args )
306
303
end , { nargs = " ?" , complete = " dir" })
307
- api .nvim_create_user_command (" NvimTreeFocus" , M .focus , { bar = true })
308
- api .nvim_create_user_command (" NvimTreeRefresh" , reloaders .reload_explorer , { bar = true })
309
- api .nvim_create_user_command (" NvimTreeClipboard" , copy_paste .print_clipboard , { bar = true })
310
- api .nvim_create_user_command (" NvimTreeFindFile" , function (res )
304
+ vim . api .nvim_create_user_command (" NvimTreeFocus" , M .focus , { bar = true })
305
+ vim . api .nvim_create_user_command (" NvimTreeRefresh" , reloaders .reload_explorer , { bar = true })
306
+ vim . api .nvim_create_user_command (" NvimTreeClipboard" , copy_paste .print_clipboard , { bar = true })
307
+ vim . api .nvim_create_user_command (" NvimTreeFindFile" , function (res )
311
308
M .find_file (true , nil , res .bang )
312
309
end , { bang = true , bar = true })
313
- api .nvim_create_user_command (" NvimTreeFindFileToggle" , function (res )
310
+ vim . api .nvim_create_user_command (" NvimTreeFindFileToggle" , function (res )
314
311
M .toggle (true , false , res .args , res .bang )
315
312
end , { bang = true , nargs = " ?" , complete = " dir" })
316
- api .nvim_create_user_command (" NvimTreeResize" , function (res )
313
+ vim . api .nvim_create_user_command (" NvimTreeResize" , function (res )
317
314
M .resize (res .args )
318
315
end , { nargs = 1 , bar = true })
319
- api .nvim_create_user_command (" NvimTreeCollapse" , collapse_all .fn , { bar = true })
320
- api .nvim_create_user_command (" NvimTreeCollapseKeepBuffers" , function ()
316
+ vim . api .nvim_create_user_command (" NvimTreeCollapse" , collapse_all .fn , { bar = true })
317
+ vim . api .nvim_create_user_command (" NvimTreeCollapseKeepBuffers" , function ()
321
318
collapse_all .fn (true )
322
319
end , { bar = true })
323
320
end
@@ -331,10 +328,10 @@ function M.change_dir(name)
331
328
end
332
329
333
330
local function setup_autocommands (opts )
334
- local augroup_id = api .nvim_create_augroup (" NvimTree" , { clear = true })
331
+ local augroup_id = vim . api .nvim_create_augroup (" NvimTree" , { clear = true })
335
332
local function create_nvim_tree_autocmd (name , custom_opts )
336
333
local default_opts = { group = augroup_id }
337
- api .nvim_create_autocmd (name , vim .tbl_extend (" force" , default_opts , custom_opts ))
334
+ vim . api .nvim_create_autocmd (name , vim .tbl_extend (" force" , default_opts , custom_opts ))
338
335
end
339
336
340
337
-- reset highlights when colorscheme is changed
@@ -410,9 +407,9 @@ local function setup_autocommands(opts)
410
407
create_nvim_tree_autocmd (" BufEnter" , {
411
408
pattern = " NvimTree_*" ,
412
409
callback = function ()
413
- local bufnr = api .nvim_get_current_buf ()
410
+ local bufnr = vim . api .nvim_get_current_buf ()
414
411
vim .schedule (function ()
415
- api .nvim_buf_call (bufnr , function ()
412
+ vim . api .nvim_buf_call (bufnr , function ()
416
413
vim .cmd [[ norm! zz]]
417
414
end )
418
415
end )
0 commit comments