Skip to content

Commit 8cc3696

Browse files
authored
fix: replace vim.* "requires" with explicit calls to vim functions (#1701)
1 parent 6d6a446 commit 8cc3696

30 files changed

+263
-321
lines changed

lua/nvim-tree.lua

+41-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local luv = vim.loop
2-
local api = vim.api
3-
41
local lib = require "nvim-tree.lib"
52
local log = require "nvim-tree.log"
63
local colors = require "nvim-tree.colors"
@@ -29,7 +26,7 @@ end
2926

3027
function M.change_root(filepath, bufnr)
3128
-- 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 ""
3330
for _, value in pairs(_config.update_focused_file.ignore_list) do
3431
if utils.str_find(filepath, value) or utils.str_find(ft, value) then
3532
return
@@ -75,7 +72,7 @@ function M.toggle(find_file, no_focus, cwd, bang)
7572
if view.is_visible() then
7673
view.close()
7774
else
78-
local previous_buf = api.nvim_get_current_buf()
75+
local previous_buf = vim.api.nvim_get_current_buf()
7976
M.open(cwd)
8077
if _config.update_focused_file.enable or find_file then
8178
M.find_file(false, previous_buf, bang)
@@ -101,8 +98,8 @@ function M.open_replacing_current_buffer(cwd)
10198
return
10299
end
103100

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)
106103
if bufname == "" or vim.loop.fs_stat(bufname) == nil then
107104
return
108105
end
@@ -121,8 +118,8 @@ end
121118

122119
function M.tab_change()
123120
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")
126123
for _, filter in ipairs(M.config.ignore_buf_on_tab_change) do
127124
if bufname:match(filter) ~= nil or ft:match(filter) ~= nil then
128125
return
@@ -135,26 +132,26 @@ end
135132

136133
local function find_existing_windows()
137134
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())
141138
end
142139

143140
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")
146143
end
147144

148145
function M.find_file(with_open, bufnr, bang)
149146
if not with_open and not core.get_explorer() then
150147
return
151148
end
152149

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
155152
return
156153
end
157-
local bufname = api.nvim_buf_get_name(bufnr)
154+
local bufname = vim.api.nvim_buf_get_name(bufnr)
158155
local filepath = utils.canonical_path(vim.fn.fnamemodify(bufname, ":p"))
159156
if not is_file_readable(filepath) then
160157
return
@@ -181,8 +178,8 @@ function M.open_on_directory()
181178
return
182179
end
183180

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)
186183
if vim.fn.isdirectory(bufname) ~= 1 then
187184
return
188185
end
@@ -198,7 +195,7 @@ end
198195

199196
local prev_line
200197
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]
202199
if l == prev_line then
203200
return
204201
end
@@ -209,22 +206,22 @@ function M.place_cursor_on_node()
209206
return
210207
end
211208

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)
214211
local idx = vim.fn.stridx(line, node.name)
215212

216213
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 })
218215
end
219216
end
220217

221218
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")
225222
local ft_ignore = _config.ignore_ft_on_setup
226223

227-
local stats = luv.fs_stat(bufname)
224+
local stats = vim.loop.fs_stat(bufname)
228225
local is_dir = stats and stats.type == "directory"
229226
local is_file = stats and stats.type == "file"
230227
local cwd
@@ -234,7 +231,7 @@ function M.on_enter(netrw_disabled)
234231
vim.cmd("noautocmd cd " .. cwd)
235232
end
236233

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 {}
238235
local buf_has_content = #lines > 1 or (#lines == 1 and lines[1] ~= "")
239236

240237
local buf_is_dir = is_dir and netrw_disabled
@@ -265,7 +262,7 @@ function M.on_enter(netrw_disabled)
265262
-- Session that left a NvimTree Buffer opened, reopen with it
266263
local existing_tree_wins = find_existing_windows()
267264
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])
269266
end
270267

271268
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)
297294
end
298295

299296
local function setup_vim_commands()
300-
api.nvim_create_user_command("NvimTreeOpen", function(res)
297+
vim.api.nvim_create_user_command("NvimTreeOpen", function(res)
301298
M.open(res.args)
302299
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)
305302
M.toggle(false, false, res.args)
306303
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)
311308
M.find_file(true, nil, res.bang)
312309
end, { bang = true, bar = true })
313-
api.nvim_create_user_command("NvimTreeFindFileToggle", function(res)
310+
vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function(res)
314311
M.toggle(true, false, res.args, res.bang)
315312
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)
317314
M.resize(res.args)
318315
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()
321318
collapse_all.fn(true)
322319
end, { bar = true })
323320
end
@@ -331,10 +328,10 @@ function M.change_dir(name)
331328
end
332329

333330
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 })
335332
local function create_nvim_tree_autocmd(name, custom_opts)
336333
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))
338335
end
339336

340337
-- reset highlights when colorscheme is changed
@@ -410,9 +407,9 @@ local function setup_autocommands(opts)
410407
create_nvim_tree_autocmd("BufEnter", {
411408
pattern = "NvimTree_*",
412409
callback = function()
413-
local bufnr = api.nvim_get_current_buf()
410+
local bufnr = vim.api.nvim_get_current_buf()
414411
vim.schedule(function()
415-
api.nvim_buf_call(bufnr, function()
412+
vim.api.nvim_buf_call(bufnr, function()
416413
vim.cmd [[norm! zz]]
417414
end)
418415
end)

lua/nvim-tree/actions/finders/find-file.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local log = require "nvim-tree.log"
2-
local uv = vim.loop
32
local view = require "nvim-tree.view"
43
local utils = require "nvim-tree.utils"
54
local renderer = require "nvim-tree.renderer"
@@ -18,7 +17,7 @@ function M.fn(fname)
1817
end
1918

2019
-- always match against the real path
21-
local fname_real = uv.fs_realpath(fname)
20+
local fname_real = vim.loop.fs_realpath(fname)
2221
if not fname_real then
2322
return
2423
end

lua/nvim-tree/actions/finders/search-node.lua

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local api = vim.api
2-
local uv = vim.loop
3-
41
local core = require "nvim-tree.core"
52
local filters = require "nvim-tree.explorer.filters"
63
local find_file = require("nvim-tree.actions.finders.find-file").fn
@@ -17,22 +14,22 @@ local function search(search_dir, input_path)
1714
local function iter(dir)
1815
local realpath, path, name, stat, handle, _
1916

20-
handle, _ = uv.fs_scandir(dir)
17+
handle, _ = vim.loop.fs_scandir(dir)
2118
if not handle then
2219
return
2320
end
2421

25-
realpath, _ = uv.fs_realpath(dir)
22+
realpath, _ = vim.loop.fs_realpath(dir)
2623
if not realpath or vim.tbl_contains(realpaths_searched, realpath) then
2724
return
2825
end
2926
table.insert(realpaths_searched, realpath)
3027

31-
name, _ = uv.fs_scandir_next(handle)
28+
name, _ = vim.loop.fs_scandir_next(handle)
3229
while name do
3330
path = dir .. "/" .. name
3431

35-
stat, _ = uv.fs_stat(path)
32+
stat, _ = vim.loop.fs_stat(path)
3633
if not stat then
3734
break
3835
end
@@ -50,7 +47,7 @@ local function search(search_dir, input_path)
5047
end
5148
end
5249

53-
name, _ = uv.fs_scandir_next(handle)
50+
name, _ = vim.loop.fs_scandir_next(handle)
5451
end
5552
end
5653

@@ -63,19 +60,19 @@ function M.fn()
6360
end
6461

6562
-- temporarily set &path
66-
local bufnr = api.nvim_get_current_buf()
67-
local path_existed, path_opt = pcall(api.nvim_buf_get_option, bufnr, "path")
68-
api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")
63+
local bufnr = vim.api.nvim_get_current_buf()
64+
local path_existed, path_opt = pcall(vim.api.nvim_buf_get_option, bufnr, "path")
65+
vim.api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")
6966

7067
vim.ui.input({ prompt = "Search: ", completion = "file_in_path" }, function(input_path)
7168
if not input_path or input_path == "" then
7269
return
7370
end
7471
-- reset &path
7572
if path_existed then
76-
api.nvim_buf_set_option(bufnr, "path", path_opt)
73+
vim.api.nvim_buf_set_option(bufnr, "path", path_opt)
7774
else
78-
api.nvim_buf_set_option(bufnr, "path", nil)
75+
vim.api.nvim_buf_set_option(bufnr, "path", nil)
7976
end
8077

8178
-- strip trailing slash

lua/nvim-tree/actions/fs/copy-paste.lua

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local uv = vim.loop
2-
31
local lib = require "nvim-tree.lib"
42
local log = require "nvim-tree.log"
53
local utils = require "nvim-tree.utils"
@@ -17,7 +15,7 @@ local function do_copy(source, destination)
1715
local source_stats, handle
1816
local success, errmsg
1917

20-
source_stats, errmsg = uv.fs_stat(source)
18+
source_stats, errmsg = vim.loop.fs_stat(source)
2119
if not source_stats then
2220
log.line("copy_paste", "do_copy fs_stat '%s' failed '%s'", source, errmsg)
2321
return false, errmsg
@@ -31,29 +29,29 @@ local function do_copy(source, destination)
3129
end
3230

3331
if source_stats.type == "file" then
34-
success, errmsg = uv.fs_copyfile(source, destination)
32+
success, errmsg = vim.loop.fs_copyfile(source, destination)
3533
if not success then
3634
log.line("copy_paste", "do_copy fs_copyfile failed '%s'", errmsg)
3735
return false, errmsg
3836
end
3937
return true
4038
elseif source_stats.type == "directory" then
41-
handle, errmsg = uv.fs_scandir(source)
39+
handle, errmsg = vim.loop.fs_scandir(source)
4240
if type(handle) == "string" then
4341
return false, handle
4442
elseif not handle then
4543
log.line("copy_paste", "do_copy fs_scandir '%s' failed '%s'", source, errmsg)
4644
return false, errmsg
4745
end
4846

49-
success, errmsg = uv.fs_mkdir(destination, source_stats.mode)
47+
success, errmsg = vim.loop.fs_mkdir(destination, source_stats.mode)
5048
if not success then
5149
log.line("copy_paste", "do_copy fs_mkdir '%s' failed '%s'", destination, errmsg)
5250
return false, errmsg
5351
end
5452

5553
while true do
56-
local name, _ = uv.fs_scandir_next(handle)
54+
local name, _ = vim.loop.fs_scandir_next(handle)
5755
if not name then
5856
break
5957
end
@@ -80,7 +78,7 @@ local function do_single_paste(source, dest, action_type, action_fn)
8078

8179
log.line("copy_paste", "do_single_paste '%s' -> '%s'", source, dest)
8280

83-
dest_stats, errmsg, errcode = uv.fs_stat(dest)
81+
dest_stats, errmsg, errcode = vim.loop.fs_stat(dest)
8482
if not dest_stats and errcode ~= "ENOENT" then
8583
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
8684
return false, errmsg
@@ -155,7 +153,7 @@ local function do_paste(node, action_type, action_fn)
155153
end
156154

157155
local destination = node.absolute_path
158-
local stats, errmsg, errcode = uv.fs_stat(destination)
156+
local stats, errmsg, errcode = vim.loop.fs_stat(destination)
159157
if not stats and errcode ~= "ENOENT" then
160158
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
161159
notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
@@ -188,7 +186,7 @@ local function do_cut(source, destination)
188186
return true
189187
end
190188

191-
local success, errmsg = uv.fs_rename(source, destination)
189+
local success, errmsg = vim.loop.fs_rename(source, destination)
192190
if not success then
193191
log.line("copy_paste", "do_cut fs_rename failed '%s'", errmsg)
194192
return false, errmsg

0 commit comments

Comments
 (0)