fix: replace vim.* "requires" with explicit calls to vim functions (#1701)

This commit is contained in:
Alexander Courtis 2022-11-06 10:37:33 +11:00 committed by GitHub
parent 6d6a44626d
commit 8cc369695b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 263 additions and 321 deletions

View File

@ -1,6 +1,3 @@
local luv = vim.loop
local api = vim.api
local lib = require "nvim-tree.lib" local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local colors = require "nvim-tree.colors" local colors = require "nvim-tree.colors"
@ -29,7 +26,7 @@ end
function M.change_root(filepath, bufnr) function M.change_root(filepath, bufnr)
-- skip if current file is in ignore_list -- skip if current file is in ignore_list
local ft = api.nvim_buf_get_option(bufnr, "filetype") or "" local ft = vim.api.nvim_buf_get_option(bufnr, "filetype") or ""
for _, value in pairs(_config.update_focused_file.ignore_list) do for _, value in pairs(_config.update_focused_file.ignore_list) do
if utils.str_find(filepath, value) or utils.str_find(ft, value) then if utils.str_find(filepath, value) or utils.str_find(ft, value) then
return return
@ -75,7 +72,7 @@ function M.toggle(find_file, no_focus, cwd, bang)
if view.is_visible() then if view.is_visible() then
view.close() view.close()
else else
local previous_buf = api.nvim_get_current_buf() local previous_buf = vim.api.nvim_get_current_buf()
M.open(cwd) M.open(cwd)
if _config.update_focused_file.enable or find_file then if _config.update_focused_file.enable or find_file then
M.find_file(false, previous_buf, bang) M.find_file(false, previous_buf, bang)
@ -101,8 +98,8 @@ function M.open_replacing_current_buffer(cwd)
return return
end end
local buf = api.nvim_get_current_buf() local buf = vim.api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(buf) local bufname = vim.api.nvim_buf_get_name(buf)
if bufname == "" or vim.loop.fs_stat(bufname) == nil then if bufname == "" or vim.loop.fs_stat(bufname) == nil then
return return
end end
@ -121,8 +118,8 @@ end
function M.tab_change() function M.tab_change()
if view.is_visible { any_tabpage = true } then if view.is_visible { any_tabpage = true } then
local bufname = api.nvim_buf_get_name(0) local bufname = vim.api.nvim_buf_get_name(0)
local ft = api.nvim_buf_get_option(0, "ft") local ft = vim.api.nvim_buf_get_option(0, "ft")
for _, filter in ipairs(M.config.ignore_buf_on_tab_change) do for _, filter in ipairs(M.config.ignore_buf_on_tab_change) do
if bufname:match(filter) ~= nil or ft:match(filter) ~= nil then if bufname:match(filter) ~= nil or ft:match(filter) ~= nil then
return return
@ -135,14 +132,14 @@ end
local function find_existing_windows() local function find_existing_windows()
return vim.tbl_filter(function(win) return vim.tbl_filter(function(win)
local buf = api.nvim_win_get_buf(win) local buf = vim.api.nvim_win_get_buf(win)
return api.nvim_buf_get_name(buf):match "NvimTree" ~= nil return vim.api.nvim_buf_get_name(buf):match "NvimTree" ~= nil
end, api.nvim_list_wins()) end, vim.api.nvim_list_wins())
end end
local function is_file_readable(fname) local function is_file_readable(fname)
local stat = luv.fs_stat(fname) local stat = vim.loop.fs_stat(fname)
return stat and stat.type == "file" and luv.fs_access(fname, "R") return stat and stat.type == "file" and vim.loop.fs_access(fname, "R")
end end
function M.find_file(with_open, bufnr, bang) function M.find_file(with_open, bufnr, bang)
@ -150,11 +147,11 @@ function M.find_file(with_open, bufnr, bang)
return return
end end
bufnr = bufnr or api.nvim_get_current_buf() bufnr = bufnr or vim.api.nvim_get_current_buf()
if not api.nvim_buf_is_valid(bufnr) then if not vim.api.nvim_buf_is_valid(bufnr) then
return return
end end
local bufname = api.nvim_buf_get_name(bufnr) local bufname = vim.api.nvim_buf_get_name(bufnr)
local filepath = utils.canonical_path(vim.fn.fnamemodify(bufname, ":p")) local filepath = utils.canonical_path(vim.fn.fnamemodify(bufname, ":p"))
if not is_file_readable(filepath) then if not is_file_readable(filepath) then
return return
@ -181,8 +178,8 @@ function M.open_on_directory()
return return
end end
local buf = api.nvim_get_current_buf() local buf = vim.api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(buf) local bufname = vim.api.nvim_buf_get_name(buf)
if vim.fn.isdirectory(bufname) ~= 1 then if vim.fn.isdirectory(bufname) ~= 1 then
return return
end end
@ -198,7 +195,7 @@ end
local prev_line local prev_line
function M.place_cursor_on_node() function M.place_cursor_on_node()
local l = api.nvim_win_get_cursor(0)[1] local l = vim.api.nvim_win_get_cursor(0)[1]
if l == prev_line then if l == prev_line then
return return
end end
@ -209,22 +206,22 @@ function M.place_cursor_on_node()
return return
end end
local line = api.nvim_get_current_line() local line = vim.api.nvim_get_current_line()
local cursor = api.nvim_win_get_cursor(0) local cursor = vim.api.nvim_win_get_cursor(0)
local idx = vim.fn.stridx(line, node.name) local idx = vim.fn.stridx(line, node.name)
if idx >= 0 then if idx >= 0 then
api.nvim_win_set_cursor(0, { cursor[1], idx }) vim.api.nvim_win_set_cursor(0, { cursor[1], idx })
end end
end end
function M.on_enter(netrw_disabled) function M.on_enter(netrw_disabled)
local bufnr = api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(bufnr) local bufname = vim.api.nvim_buf_get_name(bufnr)
local buftype = api.nvim_buf_get_option(bufnr, "filetype") local buftype = vim.api.nvim_buf_get_option(bufnr, "filetype")
local ft_ignore = _config.ignore_ft_on_setup local ft_ignore = _config.ignore_ft_on_setup
local stats = luv.fs_stat(bufname) local stats = vim.loop.fs_stat(bufname)
local is_dir = stats and stats.type == "directory" local is_dir = stats and stats.type == "directory"
local is_file = stats and stats.type == "file" local is_file = stats and stats.type == "file"
local cwd local cwd
@ -234,7 +231,7 @@ function M.on_enter(netrw_disabled)
vim.cmd("noautocmd cd " .. cwd) vim.cmd("noautocmd cd " .. cwd)
end end
local lines = not is_dir and api.nvim_buf_get_lines(bufnr, 0, -1, false) or {} local lines = not is_dir and vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) or {}
local buf_has_content = #lines > 1 or (#lines == 1 and lines[1] ~= "") local buf_has_content = #lines > 1 or (#lines == 1 and lines[1] ~= "")
local buf_is_dir = is_dir and netrw_disabled local buf_is_dir = is_dir and netrw_disabled
@ -265,7 +262,7 @@ function M.on_enter(netrw_disabled)
-- Session that left a NvimTree Buffer opened, reopen with it -- Session that left a NvimTree Buffer opened, reopen with it
local existing_tree_wins = find_existing_windows() local existing_tree_wins = find_existing_windows()
if existing_tree_wins[1] then if existing_tree_wins[1] then
api.nvim_set_current_win(existing_tree_wins[1]) vim.api.nvim_set_current_win(existing_tree_wins[1])
end end
if should_open or should_hijack or existing_tree_wins[1] ~= nil then 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)
end end
local function setup_vim_commands() local function setup_vim_commands()
api.nvim_create_user_command("NvimTreeOpen", function(res) vim.api.nvim_create_user_command("NvimTreeOpen", function(res)
M.open(res.args) M.open(res.args)
end, { nargs = "?", complete = "dir" }) end, { nargs = "?", complete = "dir" })
api.nvim_create_user_command("NvimTreeClose", view.close, { bar = true }) vim.api.nvim_create_user_command("NvimTreeClose", view.close, { bar = true })
api.nvim_create_user_command("NvimTreeToggle", function(res) vim.api.nvim_create_user_command("NvimTreeToggle", function(res)
M.toggle(false, false, res.args) M.toggle(false, false, res.args)
end, { nargs = "?", complete = "dir" }) end, { nargs = "?", complete = "dir" })
api.nvim_create_user_command("NvimTreeFocus", M.focus, { bar = true }) vim.api.nvim_create_user_command("NvimTreeFocus", M.focus, { bar = true })
api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, { bar = true }) vim.api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, { bar = true })
api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, { bar = true }) vim.api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, { bar = true })
api.nvim_create_user_command("NvimTreeFindFile", function(res) vim.api.nvim_create_user_command("NvimTreeFindFile", function(res)
M.find_file(true, nil, res.bang) M.find_file(true, nil, res.bang)
end, { bang = true, bar = true }) end, { bang = true, bar = true })
api.nvim_create_user_command("NvimTreeFindFileToggle", function(res) vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function(res)
M.toggle(true, false, res.args, res.bang) M.toggle(true, false, res.args, res.bang)
end, { bang = true, nargs = "?", complete = "dir" }) end, { bang = true, nargs = "?", complete = "dir" })
api.nvim_create_user_command("NvimTreeResize", function(res) vim.api.nvim_create_user_command("NvimTreeResize", function(res)
M.resize(res.args) M.resize(res.args)
end, { nargs = 1, bar = true }) end, { nargs = 1, bar = true })
api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, { bar = true }) vim.api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, { bar = true })
api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function() vim.api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function()
collapse_all.fn(true) collapse_all.fn(true)
end, { bar = true }) end, { bar = true })
end end
@ -331,10 +328,10 @@ function M.change_dir(name)
end end
local function setup_autocommands(opts) local function setup_autocommands(opts)
local augroup_id = api.nvim_create_augroup("NvimTree", { clear = true }) local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true })
local function create_nvim_tree_autocmd(name, custom_opts) local function create_nvim_tree_autocmd(name, custom_opts)
local default_opts = { group = augroup_id } local default_opts = { group = augroup_id }
api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end end
-- reset highlights when colorscheme is changed -- reset highlights when colorscheme is changed
@ -410,9 +407,9 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufEnter", { create_nvim_tree_autocmd("BufEnter", {
pattern = "NvimTree_*", pattern = "NvimTree_*",
callback = function() callback = function()
local bufnr = api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
vim.schedule(function() vim.schedule(function()
api.nvim_buf_call(bufnr, function() vim.api.nvim_buf_call(bufnr, function()
vim.cmd [[norm! zz]] vim.cmd [[norm! zz]]
end) end)
end) end)

View File

@ -1,5 +1,4 @@
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local uv = vim.loop
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local renderer = require "nvim-tree.renderer" local renderer = require "nvim-tree.renderer"
@ -18,7 +17,7 @@ function M.fn(fname)
end end
-- always match against the real path -- always match against the real path
local fname_real = uv.fs_realpath(fname) local fname_real = vim.loop.fs_realpath(fname)
if not fname_real then if not fname_real then
return return
end end

View File

@ -1,6 +1,3 @@
local api = vim.api
local uv = vim.loop
local core = require "nvim-tree.core" local core = require "nvim-tree.core"
local filters = require "nvim-tree.explorer.filters" local filters = require "nvim-tree.explorer.filters"
local find_file = require("nvim-tree.actions.finders.find-file").fn local find_file = require("nvim-tree.actions.finders.find-file").fn
@ -17,22 +14,22 @@ local function search(search_dir, input_path)
local function iter(dir) local function iter(dir)
local realpath, path, name, stat, handle, _ local realpath, path, name, stat, handle, _
handle, _ = uv.fs_scandir(dir) handle, _ = vim.loop.fs_scandir(dir)
if not handle then if not handle then
return return
end end
realpath, _ = uv.fs_realpath(dir) realpath, _ = vim.loop.fs_realpath(dir)
if not realpath or vim.tbl_contains(realpaths_searched, realpath) then if not realpath or vim.tbl_contains(realpaths_searched, realpath) then
return return
end end
table.insert(realpaths_searched, realpath) table.insert(realpaths_searched, realpath)
name, _ = uv.fs_scandir_next(handle) name, _ = vim.loop.fs_scandir_next(handle)
while name do while name do
path = dir .. "/" .. name path = dir .. "/" .. name
stat, _ = uv.fs_stat(path) stat, _ = vim.loop.fs_stat(path)
if not stat then if not stat then
break break
end end
@ -50,7 +47,7 @@ local function search(search_dir, input_path)
end end
end end
name, _ = uv.fs_scandir_next(handle) name, _ = vim.loop.fs_scandir_next(handle)
end end
end end
@ -63,9 +60,9 @@ function M.fn()
end end
-- temporarily set &path -- temporarily set &path
local bufnr = api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local path_existed, path_opt = pcall(api.nvim_buf_get_option, bufnr, "path") local path_existed, path_opt = pcall(vim.api.nvim_buf_get_option, bufnr, "path")
api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**") vim.api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")
vim.ui.input({ prompt = "Search: ", completion = "file_in_path" }, function(input_path) vim.ui.input({ prompt = "Search: ", completion = "file_in_path" }, function(input_path)
if not input_path or input_path == "" then if not input_path or input_path == "" then
@ -73,9 +70,9 @@ function M.fn()
end end
-- reset &path -- reset &path
if path_existed then if path_existed then
api.nvim_buf_set_option(bufnr, "path", path_opt) vim.api.nvim_buf_set_option(bufnr, "path", path_opt)
else else
api.nvim_buf_set_option(bufnr, "path", nil) vim.api.nvim_buf_set_option(bufnr, "path", nil)
end end
-- strip trailing slash -- strip trailing slash

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local lib = require "nvim-tree.lib" local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
@ -17,7 +15,7 @@ local function do_copy(source, destination)
local source_stats, handle local source_stats, handle
local success, errmsg local success, errmsg
source_stats, errmsg = uv.fs_stat(source) source_stats, errmsg = vim.loop.fs_stat(source)
if not source_stats then if not source_stats then
log.line("copy_paste", "do_copy fs_stat '%s' failed '%s'", source, errmsg) log.line("copy_paste", "do_copy fs_stat '%s' failed '%s'", source, errmsg)
return false, errmsg return false, errmsg
@ -31,14 +29,14 @@ local function do_copy(source, destination)
end end
if source_stats.type == "file" then if source_stats.type == "file" then
success, errmsg = uv.fs_copyfile(source, destination) success, errmsg = vim.loop.fs_copyfile(source, destination)
if not success then if not success then
log.line("copy_paste", "do_copy fs_copyfile failed '%s'", errmsg) log.line("copy_paste", "do_copy fs_copyfile failed '%s'", errmsg)
return false, errmsg return false, errmsg
end end
return true return true
elseif source_stats.type == "directory" then elseif source_stats.type == "directory" then
handle, errmsg = uv.fs_scandir(source) handle, errmsg = vim.loop.fs_scandir(source)
if type(handle) == "string" then if type(handle) == "string" then
return false, handle return false, handle
elseif not handle then elseif not handle then
@ -46,14 +44,14 @@ local function do_copy(source, destination)
return false, errmsg return false, errmsg
end end
success, errmsg = uv.fs_mkdir(destination, source_stats.mode) success, errmsg = vim.loop.fs_mkdir(destination, source_stats.mode)
if not success then if not success then
log.line("copy_paste", "do_copy fs_mkdir '%s' failed '%s'", destination, errmsg) log.line("copy_paste", "do_copy fs_mkdir '%s' failed '%s'", destination, errmsg)
return false, errmsg return false, errmsg
end end
while true do while true do
local name, _ = uv.fs_scandir_next(handle) local name, _ = vim.loop.fs_scandir_next(handle)
if not name then if not name then
break break
end end
@ -80,7 +78,7 @@ local function do_single_paste(source, dest, action_type, action_fn)
log.line("copy_paste", "do_single_paste '%s' -> '%s'", source, dest) log.line("copy_paste", "do_single_paste '%s' -> '%s'", source, dest)
dest_stats, errmsg, errcode = uv.fs_stat(dest) dest_stats, errmsg, errcode = vim.loop.fs_stat(dest)
if not dest_stats and errcode ~= "ENOENT" then if not dest_stats and errcode ~= "ENOENT" then
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???")) notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg return false, errmsg
@ -155,7 +153,7 @@ local function do_paste(node, action_type, action_fn)
end end
local destination = node.absolute_path local destination = node.absolute_path
local stats, errmsg, errcode = uv.fs_stat(destination) local stats, errmsg, errcode = vim.loop.fs_stat(destination)
if not stats and errcode ~= "ENOENT" then if not stats and errcode ~= "ENOENT" then
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg) log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???")) notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
@ -188,7 +186,7 @@ local function do_cut(source, destination)
return true return true
end end
local success, errmsg = uv.fs_rename(source, destination) local success, errmsg = vim.loop.fs_rename(source, destination)
if not success then if not success then
log.line("copy_paste", "do_cut fs_rename failed '%s'", errmsg) log.line("copy_paste", "do_cut fs_rename failed '%s'", errmsg)
return false, errmsg return false, errmsg

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events" local events = require "nvim-tree.events"
local lib = require "nvim-tree.lib" local lib = require "nvim-tree.lib"
@ -9,12 +7,12 @@ local notify = require "nvim-tree.notify"
local M = {} local M = {}
local function create_and_notify(file) local function create_and_notify(file)
local ok, fd = pcall(uv.fs_open, file, "w", 420) local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
if not ok then if not ok then
notify.error("Couldn't create file " .. file) notify.error("Couldn't create file " .. file)
return return
end end
uv.fs_close(fd) vim.loop.fs_close(fd)
events._dispatch_file_created(file) events._dispatch_file_created(file)
end end
@ -94,7 +92,7 @@ function M.fn(node)
if is_last_path_file and idx == num_nodes then if is_last_path_file and idx == num_nodes then
create_file(path_to_create) create_file(path_to_create)
elseif not utils.file_exists(path_to_create) then elseif not utils.file_exists(path_to_create) then
local success = uv.fs_mkdir(path_to_create, 493) local success = vim.loop.fs_mkdir(path_to_create, 493)
if not success then if not success then
notify.error("Could not create folder " .. path_to_create) notify.error("Could not create folder " .. path_to_create)
is_error = true is_error = true

View File

@ -1,6 +1,3 @@
local a = vim.api
local luv = vim.loop
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events" local events = require "nvim-tree.events"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
@ -10,13 +7,13 @@ local notify = require "nvim-tree.notify"
local M = {} local M = {}
local function close_windows(windows) local function close_windows(windows)
if view.View.float.enable and #a.nvim_list_wins() == 1 then if view.View.float.enable and #vim.api.nvim_list_wins() == 1 then
return return
end end
for _, window in ipairs(windows) do for _, window in ipairs(windows) do
if a.nvim_win_is_valid(window) then if vim.api.nvim_win_is_valid(window) then
a.nvim_win_close(window, true) vim.api.nvim_win_close(window, true)
end end
end end
end end
@ -26,14 +23,14 @@ local function clear_buffer(absolute_path)
for _, buf in pairs(bufs) do for _, buf in pairs(bufs) do
if buf.name == absolute_path then if buf.name == absolute_path then
if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then if buf.hidden == 0 and (#bufs > 1 or view.View.float.enable) then
local winnr = a.nvim_get_current_win() local winnr = vim.api.nvim_get_current_win()
a.nvim_set_current_win(buf.windows[1]) vim.api.nvim_set_current_win(buf.windows[1])
vim.cmd ":bn" vim.cmd ":bn"
if not view.View.float.enable then if not view.View.float.enable then
a.nvim_set_current_win(winnr) vim.api.nvim_set_current_win(winnr)
end end
end end
a.nvim_buf_delete(buf.bufnr, { force = true }) vim.api.nvim_buf_delete(buf.bufnr, { force = true })
if M.close_window then if M.close_window then
close_windows(buf.windows) close_windows(buf.windows)
end end
@ -43,13 +40,13 @@ local function clear_buffer(absolute_path)
end end
local function remove_dir(cwd) local function remove_dir(cwd)
local handle = luv.fs_scandir(cwd) local handle = vim.loop.fs_scandir(cwd)
if type(handle) == "string" then if type(handle) == "string" then
return notify.error(handle) return notify.error(handle)
end end
while true do while true do
local name, t = luv.fs_scandir_next(handle) local name, t = vim.loop.fs_scandir_next(handle)
if not name then if not name then
break break
end end
@ -61,7 +58,7 @@ local function remove_dir(cwd)
return false return false
end end
else else
local success = luv.fs_unlink(new_cwd) local success = vim.loop.fs_unlink(new_cwd)
if not success then if not success then
return false return false
end end
@ -69,7 +66,7 @@ local function remove_dir(cwd)
end end
end end
return luv.fs_rmdir(cwd) return vim.loop.fs_rmdir(cwd)
end end
function M.fn(node) function M.fn(node)
@ -88,7 +85,7 @@ function M.fn(node)
end end
events._dispatch_folder_removed(node.absolute_path) events._dispatch_folder_removed(node.absolute_path)
else else
local success = luv.fs_unlink(node.absolute_path) local success = vim.loop.fs_unlink(node.absolute_path)
if not success then if not success then
return notify.error("Could not remove " .. node.name) return notify.error("Could not remove " .. node.name)
end end

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local lib = require "nvim-tree.lib" local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events" local events = require "nvim-tree.events"
@ -17,7 +15,7 @@ function M.rename(node, to)
return return
end end
local success, err = uv.fs_rename(node.absolute_path, to) local success, err = vim.loop.fs_rename(node.absolute_path, to)
if not success then if not success then
return notify.warn(err_fmt(node.absolute_path, to, err)) return notify.warn(err_fmt(node.absolute_path, to, err))
end end

View File

@ -1,5 +1,3 @@
local a = vim.api
local lib = require "nvim-tree.lib" local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify" local notify = require "nvim-tree.notify"
@ -19,10 +17,10 @@ local function clear_buffer(absolute_path)
for _, buf in pairs(bufs) do for _, buf in pairs(bufs) do
if buf.name == absolute_path then if buf.name == absolute_path then
if buf.hidden == 0 and #bufs > 1 then if buf.hidden == 0 and #bufs > 1 then
local winnr = a.nvim_get_current_win() local winnr = vim.api.nvim_get_current_win()
a.nvim_set_current_win(buf.windows[1]) vim.api.nvim_set_current_win(buf.windows[1])
vim.cmd ":bn" vim.cmd ":bn"
a.nvim_set_current_win(winnr) vim.api.nvim_set_current_win(winnr)
end end
vim.api.nvim_buf_delete(buf.bufnr, {}) vim.api.nvim_buf_delete(buf.bufnr, {})
return return

View File

@ -1,7 +1,5 @@
-- @deprecated: new implementation in nvim-tree.keymap. Please do not edit this file. -- @deprecated: new implementation in nvim-tree.keymap. Please do not edit this file.
local a = vim.api
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local notify = require "nvim-tree.notify" local notify = require "nvim-tree.notify"
@ -350,7 +348,7 @@ end
local function cleanup_existing_mappings() local function cleanup_existing_mappings()
local bufnr = view.get_bufnr() local bufnr = view.get_bufnr()
if bufnr == nil or not a.nvim_buf_is_valid(bufnr) then if bufnr == nil or not vim.api.nvim_buf_is_valid(bufnr) then
return return
end end

View File

@ -1,5 +1,4 @@
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local a = vim.api
local M = {} local M = {}
@ -34,19 +33,19 @@ local function setup_window(node)
noautocmd = true, noautocmd = true,
zindex = 60, zindex = 60,
}) })
local winnr = a.nvim_open_win(0, false, open_win_config) local winnr = vim.api.nvim_open_win(0, false, open_win_config)
current_popup = { current_popup = {
winnr = winnr, winnr = winnr,
file_path = node.absolute_path, file_path = node.absolute_path,
} }
local bufnr = a.nvim_create_buf(false, true) local bufnr = vim.api.nvim_create_buf(false, true)
a.nvim_buf_set_lines(bufnr, 0, -1, false, lines) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
a.nvim_win_set_buf(winnr, bufnr) vim.api.nvim_win_set_buf(winnr, bufnr)
end end
function M.close_popup() function M.close_popup()
if current_popup ~= nil then if current_popup ~= nil then
a.nvim_win_close(current_popup.winnr, { force = true }) vim.api.nvim_win_close(current_popup.winnr, { force = true })
vim.cmd "augroup NvimTreeRemoveFilePopup | au! CursorMoved | augroup END" vim.cmd "augroup NvimTreeRemoveFilePopup | au! CursorMoved | augroup END"
current_popup = nil current_popup = nil
@ -69,8 +68,8 @@ function M.toggle_file_info(node)
setup_window(node) setup_window(node)
a.nvim_create_autocmd("CursorMoved", { vim.api.nvim_create_autocmd("CursorMoved", {
group = a.nvim_create_augroup("NvimTreeRemoveFilePopup", {}), group = vim.api.nvim_create_augroup("NvimTreeRemoveFilePopup", {}),
callback = M.close_popup, callback = M.close_popup,
}) })
end end

View File

@ -1,6 +1,4 @@
-- Copyright 2019 Yazdani Kiyan under MIT License -- Copyright 2019 Yazdani Kiyan under MIT License
local api = vim.api
local lib = require "nvim-tree.lib" local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
@ -18,20 +16,20 @@ end
---Get all windows in the current tabpage that aren't NvimTree. ---Get all windows in the current tabpage that aren't NvimTree.
---@return table with valid win_ids ---@return table with valid win_ids
local function usable_win_ids() local function usable_win_ids()
local tabpage = api.nvim_get_current_tabpage() local tabpage = vim.api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage) local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
local tree_winid = view.get_winnr(tabpage) local tree_winid = view.get_winnr(tabpage)
return vim.tbl_filter(function(id) return vim.tbl_filter(function(id)
local bufid = api.nvim_win_get_buf(id) local bufid = vim.api.nvim_win_get_buf(id)
for option, v in pairs(M.window_picker.exclude) do for option, v in pairs(M.window_picker.exclude) do
local ok, option_value = pcall(api.nvim_buf_get_option, bufid, option) local ok, option_value = pcall(vim.api.nvim_buf_get_option, bufid, option)
if ok and vim.tbl_contains(v, option_value) then if ok and vim.tbl_contains(v, option_value) then
return false return false
end end
end end
local win_config = api.nvim_win_get_config(id) local win_config = vim.api.nvim_win_get_config(id)
return id ~= tree_winid and win_config.focusable and not win_config.external return id ~= tree_winid and win_config.focusable and not win_config.external
end, win_ids) end, win_ids)
end end
@ -68,8 +66,8 @@ local function pick_win_id()
local laststatus = vim.o.laststatus local laststatus = vim.o.laststatus
vim.o.laststatus = 2 vim.o.laststatus = 2
local tabpage = api.nvim_get_current_tabpage() local tabpage = vim.api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage) local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
local not_selectable = vim.tbl_filter(function(id) local not_selectable = vim.tbl_filter(function(id)
return not vim.tbl_contains(selectable, id) return not vim.tbl_contains(selectable, id)
@ -77,8 +75,8 @@ local function pick_win_id()
if laststatus == 3 then if laststatus == 3 then
for _, win_id in ipairs(not_selectable) do for _, win_id in ipairs(not_selectable) do
local ok_status, statusline = pcall(api.nvim_win_get_option, win_id, "statusline") local ok_status, statusline = pcall(vim.api.nvim_win_get_option, win_id, "statusline")
local ok_hl, winhl = pcall(api.nvim_win_get_option, win_id, "winhl") local ok_hl, winhl = pcall(vim.api.nvim_win_get_option, win_id, "winhl")
win_opts[win_id] = { win_opts[win_id] = {
statusline = ok_status and statusline or "", statusline = ok_status and statusline or "",
@ -86,15 +84,15 @@ local function pick_win_id()
} }
-- Clear statusline for windows not selectable -- Clear statusline for windows not selectable
api.nvim_win_set_option(win_id, "statusline", " ") vim.api.nvim_win_set_option(win_id, "statusline", " ")
end end
end end
-- Setup UI -- Setup UI
for _, id in ipairs(selectable) do for _, id in ipairs(selectable) do
local char = M.window_picker.chars:sub(i, i) local char = M.window_picker.chars:sub(i, i)
local ok_status, statusline = pcall(api.nvim_win_get_option, id, "statusline") local ok_status, statusline = pcall(vim.api.nvim_win_get_option, id, "statusline")
local ok_hl, winhl = pcall(api.nvim_win_get_option, id, "winhl") local ok_hl, winhl = pcall(vim.api.nvim_win_get_option, id, "winhl")
win_opts[id] = { win_opts[id] = {
statusline = ok_status and statusline or "", statusline = ok_status and statusline or "",
@ -102,8 +100,8 @@ local function pick_win_id()
} }
win_map[char] = id win_map[char] = id
api.nvim_win_set_option(id, "statusline", "%=" .. char .. "%=") vim.api.nvim_win_set_option(id, "statusline", "%=" .. char .. "%=")
api.nvim_win_set_option(id, "winhl", "StatusLine:NvimTreeWindowPicker,StatusLineNC:NvimTreeWindowPicker") vim.api.nvim_win_set_option(id, "winhl", "StatusLine:NvimTreeWindowPicker,StatusLineNC:NvimTreeWindowPicker")
i = i + 1 i = i + 1
if i > #M.window_picker.chars then if i > #M.window_picker.chars then
@ -122,14 +120,14 @@ local function pick_win_id()
-- Restore window options -- Restore window options
for _, id in ipairs(selectable) do for _, id in ipairs(selectable) do
for opt, value in pairs(win_opts[id]) do for opt, value in pairs(win_opts[id]) do
api.nvim_win_set_option(id, opt, value) vim.api.nvim_win_set_option(id, opt, value)
end end
end end
if laststatus == 3 then if laststatus == 3 then
for _, id in ipairs(not_selectable) do for _, id in ipairs(not_selectable) do
for opt, value in pairs(win_opts[id]) do for opt, value in pairs(win_opts[id]) do
api.nvim_win_set_option(id, opt, value) vim.api.nvim_win_set_option(id, opt, value)
end end
end end
end end
@ -154,9 +152,9 @@ local function on_preview(buf_loaded)
if not buf_loaded then if not buf_loaded then
vim.bo.bufhidden = "delete" vim.bo.bufhidden = "delete"
api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
group = api.nvim_create_augroup("RemoveBufHidden", {}), group = vim.api.nvim_create_augroup("RemoveBufHidden", {}),
buffer = api.nvim_get_current_buf(), buffer = vim.api.nvim_get_current_buf(),
callback = function() callback = function()
vim.bo.bufhidden = "" vim.bo.bufhidden = ""
end, end,
@ -195,7 +193,7 @@ end
local function set_current_win_no_autocmd(winid, autocmd) local function set_current_win_no_autocmd(winid, autocmd)
local eventignore = vim.opt.eventignore:get() local eventignore = vim.opt.eventignore:get()
vim.opt.eventignore:append(autocmd) vim.opt.eventignore:append(autocmd)
api.nvim_set_current_win(winid) vim.api.nvim_set_current_win(winid)
vim.opt.eventignore = eventignore vim.opt.eventignore = eventignore
end end
@ -209,13 +207,13 @@ local function open_in_new_window(filename, mode, win_ids)
return return
end end
local create_new_window = #api.nvim_list_wins() == 1 local create_new_window = #vim.api.nvim_list_wins() == 1
local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright" local new_window_side = (view.View.side == "right") and "aboveleft" or "belowright"
-- Target is invalid or window does not exist in current tabpage: create new window -- Target is invalid or window does not exist in current tabpage: create new window
if not vim.tbl_contains(win_ids, target_winid) then if not vim.tbl_contains(win_ids, target_winid) then
vim.cmd(new_window_side .. " vsplit") vim.cmd(new_window_side .. " vsplit")
target_winid = api.nvim_get_current_win() target_winid = vim.api.nvim_get_current_win()
lib.target_winid = target_winid lib.target_winid = target_winid
-- No need to split, as we created a new window. -- No need to split, as we created a new window.
@ -226,8 +224,8 @@ local function open_in_new_window(filename, mode, win_ids)
elseif not vim.o.hidden then elseif not vim.o.hidden then
-- If `hidden` is not enabled, check if buffer in target window is -- If `hidden` is not enabled, check if buffer in target window is
-- modified, and create new split if it is. -- modified, and create new split if it is.
local target_bufid = api.nvim_win_get_buf(target_winid) local target_bufid = vim.api.nvim_win_get_buf(target_winid)
if api.nvim_buf_get_option(target_bufid, "modified") then if vim.api.nvim_buf_get_option(target_bufid, "modified") then
mode = "vsplit" mode = "vsplit"
end end
end end
@ -257,8 +255,8 @@ local function open_in_new_window(filename, mode, win_ids)
end end
local function is_already_loaded(filename) local function is_already_loaded(filename)
for _, buf_id in ipairs(api.nvim_list_bufs()) do for _, buf_id in ipairs(vim.api.nvim_list_bufs()) do
if api.nvim_buf_is_loaded(buf_id) and filename == api.nvim_buf_get_name(buf_id) then if vim.api.nvim_buf_is_loaded(buf_id) and filename == vim.api.nvim_buf_get_name(buf_id) then
return true return true
end end
end end
@ -283,8 +281,8 @@ function M.fn(mode, filename)
return edit_in_current_buf(filename) return edit_in_current_buf(filename)
end end
local tabpage = api.nvim_get_current_tabpage() local tabpage = vim.api.nvim_get_current_tabpage()
local win_ids = api.nvim_tabpage_list_wins(tabpage) local win_ids = vim.api.nvim_tabpage_list_wins(tabpage)
local buf_loaded = is_already_loaded(filename) local buf_loaded = is_already_loaded(filename)
local found_win = utils.get_win_buf_from_path(filename) local found_win = utils.get_win_buf_from_path(filename)
@ -295,7 +293,7 @@ function M.fn(mode, filename)
if not found_win then if not found_win then
open_in_new_window(filename, mode, win_ids) open_in_new_window(filename, mode, win_ids)
else else
api.nvim_set_current_win(found_win) vim.api.nvim_set_current_win(found_win)
vim.bo.bufhidden = "" vim.bo.bufhidden = ""
end end

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local M = { local M = {
config = { config = {
is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1, is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1,
@ -18,10 +16,10 @@ function M.fn(node)
cmd = M.config.system_open.cmd, cmd = M.config.system_open.cmd,
args = M.config.system_open.args, args = M.config.system_open.args,
errors = "\n", errors = "\n",
stderr = uv.new_pipe(false), stderr = vim.loop.new_pipe(false),
} }
table.insert(process.args, node.link_to or node.absolute_path) table.insert(process.args, node.link_to or node.absolute_path)
process.handle, process.pid = uv.spawn( process.handle, process.pid = vim.loop.spawn(
process.cmd, process.cmd,
{ args = process.args, stdio = { nil, nil, process.stderr }, detached = true }, { args = process.args, stdio = { nil, nil, process.stderr }, detached = true },
function(code) function(code)
@ -39,7 +37,7 @@ function M.fn(node)
error("\n" .. process.pid .. "\nNvimTree system_open: failed to spawn process using '" .. process.cmd .. "'.") error("\n" .. process.pid .. "\nNvimTree system_open: failed to spawn process using '" .. process.cmd .. "'.")
return return
end end
uv.read_start(process.stderr, function(err, data) vim.loop.read_start(process.stderr, function(err, data)
if err then if err then
return return
end end
@ -47,7 +45,7 @@ function M.fn(node)
process.errors = process.errors .. data process.errors = process.errors .. data
end end
end) end)
uv.unref(process.handle) vim.loop.unref(process.handle)
end end
function M.setup(opts) function M.setup(opts)

View File

@ -1,11 +1,9 @@
local a = vim.api
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core" local core = require "nvim-tree.core"
local M = { local M = {
current_tab = a.nvim_get_current_tabpage(), current_tab = vim.api.nvim_get_current_tabpage(),
} }
local function clean_input_cwd(name) local function clean_input_cwd(name)
@ -33,7 +31,7 @@ function M.fn(input_cwd, with_open)
return return
end end
local new_tabpage = a.nvim_get_current_tabpage() local new_tabpage = vim.api.nvim_get_current_tabpage()
if is_window_event(new_tabpage) then if is_window_event(new_tabpage) then
return return
end end

View File

@ -1,5 +1,3 @@
local api = vim.api
local M = {} local M = {}
local function get_color_from_hl(hl_name, fallback) local function get_color_from_hl(hl_name, fallback)
@ -93,12 +91,12 @@ function M.setup()
local gui = d.gui and " gui=" .. d.gui or "" local gui = d.gui and " gui=" .. d.gui or ""
local fg = d.fg and " guifg=" .. d.fg or "" local fg = d.fg and " guifg=" .. d.fg or ""
local bg = d.bg and " guibg=" .. d.bg or "" local bg = d.bg and " guibg=" .. d.bg or ""
api.nvim_command("hi def NvimTree" .. k .. gui .. fg .. bg) vim.api.nvim_command("hi def NvimTree" .. k .. gui .. fg .. bg)
end end
local links = get_links() local links = get_links()
for k, d in pairs(links) do for k, d in pairs(links) do
api.nvim_command("hi def link NvimTree" .. k .. " " .. d) vim.api.nvim_command("hi def link NvimTree" .. k .. " " .. d)
end end
end end

View File

@ -1,4 +1,3 @@
local a = vim.api
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local core = require "nvim-tree.core" local core = require "nvim-tree.core"
@ -18,7 +17,7 @@ local sign_names = {
local function add_sign(linenr, severity) local function add_sign(linenr, severity)
local buf = view.get_bufnr() local buf = view.get_bufnr()
if not a.nvim_buf_is_valid(buf) or not a.nvim_buf_is_loaded(buf) then if not vim.api.nvim_buf_is_valid(buf) or not vim.api.nvim_buf_is_loaded(buf) then
return return
end end
local sign_name = sign_names[severity][1] local sign_name = sign_names[severity][1]
@ -30,8 +29,8 @@ local function from_nvim_lsp()
for _, diagnostic in ipairs(vim.diagnostic.get()) do for _, diagnostic in ipairs(vim.diagnostic.get()) do
local buf = diagnostic.bufnr local buf = diagnostic.bufnr
if a.nvim_buf_is_valid(buf) then if vim.api.nvim_buf_is_valid(buf) then
local bufname = a.nvim_buf_get_name(buf) local bufname = vim.api.nvim_buf_get_name(buf)
local lowest_severity = buffer_severity[bufname] local lowest_severity = buffer_severity[bufname]
if not lowest_severity or diagnostic.severity < lowest_severity then if not lowest_severity or diagnostic.severity < lowest_severity then
buffer_severity[bufname] = diagnostic.severity buffer_severity[bufname] = diagnostic.severity

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local M = {} local M = {}
local function get_dir_git_status(parent_ignored, status, absolute_path) local function get_dir_git_status(parent_ignored, status, absolute_path)
@ -22,7 +20,7 @@ local function get_git_status(parent_ignored, status, absolute_path)
end end
function M.has_one_child_folder(node) function M.has_one_child_folder(node)
return #node.nodes == 1 and node.nodes[1].nodes and uv.fs_access(node.nodes[1].absolute_path, "R") return #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R")
end end
function M.update_git_status(node, parent_ignored, status) function M.update_git_status(node, parent_ignored, status)

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local builders = require "nvim-tree.explorer.node-builders" local builders = require "nvim-tree.explorer.node-builders"
local common = require "nvim-tree.explorer.common" local common = require "nvim-tree.explorer.common"
@ -11,14 +9,14 @@ local notify = require "nvim-tree.notify"
local M = {} local M = {}
local function get_type_from(type_, cwd) local function get_type_from(type_, cwd)
return type_ or (uv.fs_stat(cwd) or {}).type return type_ or (vim.loop.fs_stat(cwd) or {}).type
end end
local function populate_children(handle, cwd, node, status) local function populate_children(handle, cwd, node, status)
local node_ignored = node.git_status == "!!" local node_ignored = node.git_status == "!!"
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path") local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
while true do while true do
local name, t = uv.fs_scandir_next(handle) local name, t = vim.loop.fs_scandir_next(handle)
if not name then if not name then
break break
end end
@ -31,7 +29,7 @@ local function populate_children(handle, cwd, node, status)
and not nodes_by_path[abs] and not nodes_by_path[abs]
then then
local child = nil local child = nil
if t == "directory" and uv.fs_access(abs, "R") then if t == "directory" and vim.loop.fs_access(abs, "R") then
child = builders.folder(node, abs, name) child = builders.folder(node, abs, name)
elseif t == "file" then elseif t == "file" then
child = builders.file(node, abs, name) child = builders.file(node, abs, name)
@ -51,7 +49,7 @@ local function populate_children(handle, cwd, node, status)
end end
local function get_dir_handle(cwd) local function get_dir_handle(cwd)
local handle = uv.fs_scandir(cwd) local handle = vim.loop.fs_scandir(cwd)
if type(handle) == "string" then if type(handle) == "string" then
notify.error(handle) notify.error(handle)
return return

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local git = require "nvim-tree.git" local git = require "nvim-tree.git"
local watch = require "nvim-tree.explorer.watch" local watch = require "nvim-tree.explorer.watch"
local common = require "nvim-tree.explorer.common" local common = require "nvim-tree.explorer.common"
@ -13,7 +11,7 @@ local Explorer = {}
Explorer.__index = Explorer Explorer.__index = Explorer
function Explorer.new(cwd) function Explorer.new(cwd)
cwd = uv.fs_realpath(cwd or uv.cwd()) cwd = vim.loop.fs_realpath(cwd or vim.loop.cwd())
local explorer = setmetatable({ local explorer = setmetatable({
absolute_path = cwd, absolute_path = cwd,
nodes = {}, nodes = {},

View File

@ -1,4 +1,3 @@
local uv = vim.loop
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local watch = require "nvim-tree.explorer.watch" local watch = require "nvim-tree.explorer.watch"
@ -8,13 +7,13 @@ local M = {
} }
function M.folder(parent, absolute_path, name) function M.folder(parent, absolute_path, name)
local handle = uv.fs_scandir(absolute_path) local handle = vim.loop.fs_scandir(absolute_path)
local has_children = handle and uv.fs_scandir_next(handle) ~= nil local has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
return { return {
type = "directory", type = "directory",
absolute_path = absolute_path, absolute_path = absolute_path,
fs_stat = uv.fs_stat(absolute_path), fs_stat = vim.loop.fs_stat(absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node group_next = nil, -- If node is grouped, this points to the next child dir/link node
has_children = has_children, has_children = has_children,
name = name, name = name,
@ -39,7 +38,7 @@ function M.is_executable(parent, absolute_path, ext)
return utils.is_wsl_windows_fs_exe(ext) return utils.is_wsl_windows_fs_exe(ext)
end end
end end
return uv.fs_access(absolute_path, "X") return vim.loop.fs_access(absolute_path, "X")
end end
function M.file(parent, absolute_path, name) function M.file(parent, absolute_path, name)
@ -50,7 +49,7 @@ function M.file(parent, absolute_path, name)
absolute_path = absolute_path, absolute_path = absolute_path,
executable = M.is_executable(parent, absolute_path, ext), executable = M.is_executable(parent, absolute_path, ext),
extension = ext, extension = ext,
fs_stat = uv.fs_stat(absolute_path), fs_stat = vim.loop.fs_stat(absolute_path),
name = name, name = name,
parent = parent, parent = parent,
} }
@ -63,11 +62,11 @@ end
-- So we need to check for link_to ~= nil when adding new links to the main tree -- So we need to check for link_to ~= nil when adding new links to the main tree
function M.link(parent, absolute_path, name) function M.link(parent, absolute_path, name)
--- I dont know if this is needed, because in my understanding, there isn't hard links in windows, but just to be sure i changed it. --- I dont know if this is needed, because in my understanding, there isn't hard links in windows, but just to be sure i changed it.
local link_to = uv.fs_realpath(absolute_path) local link_to = vim.loop.fs_realpath(absolute_path)
local open, nodes, has_children, watcher local open, nodes, has_children, watcher
if (link_to ~= nil) and uv.fs_stat(link_to).type == "directory" then if (link_to ~= nil) and vim.loop.fs_stat(link_to).type == "directory" then
local handle = uv.fs_scandir(link_to) local handle = vim.loop.fs_scandir(link_to)
has_children = handle and uv.fs_scandir_next(handle) ~= nil has_children = handle and vim.loop.fs_scandir_next(handle) ~= nil
open = false open = false
nodes = {} nodes = {}
watcher = watch.create_watcher(link_to) watcher = watch.create_watcher(link_to)
@ -76,7 +75,7 @@ function M.link(parent, absolute_path, name)
return { return {
type = "link", type = "link",
absolute_path = absolute_path, absolute_path = absolute_path,
fs_stat = uv.fs_stat(absolute_path), fs_stat = vim.loop.fs_stat(absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node group_next = nil, -- If node is grouped, this points to the next child dir/link node
has_children = has_children, has_children = has_children,
link_to = link_to, link_to = link_to,

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local builders = require "nvim-tree.explorer.node-builders" local builders = require "nvim-tree.explorer.node-builders"
local common = require "nvim-tree.explorer.common" local common = require "nvim-tree.explorer.common"
@ -21,7 +19,7 @@ end
function M.reload(node, status) function M.reload(node, status)
local cwd = node.link_to or node.absolute_path local cwd = node.link_to or node.absolute_path
local handle = uv.fs_scandir(cwd) local handle = vim.loop.fs_scandir(cwd)
if type(handle) == "string" then if type(handle) == "string" then
notify.error(handle) notify.error(handle)
return return
@ -37,7 +35,7 @@ function M.reload(node, status)
local node_ignored = node.git_status == "!!" local node_ignored = node.git_status == "!!"
local nodes_by_path = utils.key_by(node.nodes, "absolute_path") local nodes_by_path = utils.key_by(node.nodes, "absolute_path")
while true do while true do
local ok, name, t = pcall(uv.fs_scandir_next, handle) local ok, name, t = pcall(vim.loop.fs_scandir_next, handle)
if not ok or not name then if not ok or not name then
break break
end end
@ -48,7 +46,7 @@ function M.reload(node, status)
return stat return stat
end end
stat = uv.fs_stat(path) stat = vim.loop.fs_stat(path)
return stat return stat
end end
@ -69,7 +67,7 @@ function M.reload(node, status)
end end
if not nodes_by_path[abs] then if not nodes_by_path[abs] then
if t == "directory" and uv.fs_access(abs, "R") then if t == "directory" and vim.loop.fs_access(abs, "R") then
local folder = builders.folder(node, abs, name) local folder = builders.folder(node, abs, name)
nodes_by_path[abs] = folder nodes_by_path[abs] = folder
table.insert(node.nodes, folder) table.insert(node.nodes, folder)

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local git_utils = require "nvim-tree.git.utils" local git_utils = require "nvim-tree.git.utils"
@ -74,7 +72,7 @@ function M.get_project_root(cwd)
return nil return nil
end end
local stat, _ = uv.fs_stat(cwd) local stat, _ = vim.loop.fs_stat(cwd)
if not stat or stat.type ~= "directory" then if not stat or stat.type ~= "directory" then
return nil return nil
end end

View File

@ -1,4 +1,3 @@
local uv = vim.loop
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
@ -60,9 +59,9 @@ end
function Runner:_run_git_job() function Runner:_run_git_job()
local handle, pid local handle, pid
local stdout = uv.new_pipe(false) local stdout = vim.loop.new_pipe(false)
local stderr = uv.new_pipe(false) local stderr = vim.loop.new_pipe(false)
local timer = uv.new_timer() local timer = vim.loop.new_timer()
local function on_finish(rc) local function on_finish(rc)
self.rc = rc or 0 self.rc = rc or 0
@ -79,14 +78,14 @@ function Runner:_run_git_job()
handle:close() handle:close()
end end
pcall(uv.kill, pid) pcall(vim.loop.kill, pid)
end end
local opts = self:_getopts(stdout, stderr) local opts = self:_getopts(stdout, stderr)
log.line("git", "running job with timeout %dms", self.timeout) log.line("git", "running job with timeout %dms", self.timeout)
log.line("git", "git %s", table.concat(utils.array_remove_nils(opts.args), " ")) log.line("git", "git %s", table.concat(utils.array_remove_nils(opts.args), " "))
handle, pid = uv.spawn( handle, pid = vim.loop.spawn(
"git", "git",
opts, opts,
vim.schedule_wrap(function(rc) vim.schedule_wrap(function(rc)
@ -115,8 +114,8 @@ function Runner:_run_git_job()
self:_log_raw_output(data) self:_log_raw_output(data)
end end
uv.read_start(stdout, vim.schedule_wrap(manage_stdout)) vim.loop.read_start(stdout, vim.schedule_wrap(manage_stdout))
uv.read_start(stderr, vim.schedule_wrap(manage_stderr)) vim.loop.read_start(stderr, vim.schedule_wrap(manage_stderr))
end end
function Runner:_wait() function Runner:_wait()

View File

@ -1,5 +1,3 @@
local api = vim.api
local renderer = require "nvim-tree.renderer" local renderer = require "nvim-tree.renderer"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local core = require "nvim-tree.core" local core = require "nvim-tree.core"
@ -19,7 +17,7 @@ function M.get_node_at_cursor()
return return
end end
local cursor = api.nvim_win_get_cursor(view.get_winnr()) local cursor = vim.api.nvim_win_get_cursor(view.get_winnr())
local line = cursor[1] local line = cursor[1]
if view.is_help_ui() then if view.is_help_ui() then
local help_lines = require("nvim-tree.renderer.help").compute_lines() local help_lines = require("nvim-tree.renderer.help").compute_lines()
@ -57,7 +55,7 @@ function M.expand_or_collapse(node)
end end
function M.set_target_win() function M.set_target_win()
local id = api.nvim_get_current_win() local id = vim.api.nvim_get_current_win()
local tree_id = view.get_winnr() local tree_id = view.get_winnr()
if tree_id and id == tree_id then if tree_id and id == tree_id then
M.target_winid = 0 M.target_winid = 0
@ -81,10 +79,10 @@ local function open_view_and_draw()
end end
local function should_hijack_current_buf() local function should_hijack_current_buf()
local bufnr = api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(bufnr) local bufname = vim.api.nvim_buf_get_name(bufnr)
local bufmodified = api.nvim_buf_get_option(bufnr, "modified") local bufmodified = vim.api.nvim_buf_get_option(bufnr, "modified")
local ft = api.nvim_buf_get_option(bufnr, "ft") local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
local should_hijack_unnamed = M.hijack_unnamed_buffer_when_opening and bufname == "" and not bufmodified and ft == "" local should_hijack_unnamed = M.hijack_unnamed_buffer_when_opening and bufname == "" and not bufmodified and ft == ""
local should_hijack_dir = bufname ~= "" and vim.fn.isdirectory(bufname) == 1 and M.hijack_directories.enable local should_hijack_dir = bufname ~= "" and vim.fn.isdirectory(bufname) == 1 and M.hijack_directories.enable

View File

@ -1,5 +1,3 @@
local a = vim.api
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator" local Iterator = require "nvim-tree.iterators.node-iterator"
@ -28,9 +26,9 @@ local overlay_winnr = nil
local function remove_overlay() local function remove_overlay()
if view.View.float.enable and view.View.float.quit_on_focus_loss then if view.View.float.enable and view.View.float.quit_on_focus_loss then
-- return to normal nvim-tree float behaviour when filter window is closed -- return to normal nvim-tree float behaviour when filter window is closed
a.nvim_create_autocmd("WinLeave", { vim.api.nvim_create_autocmd("WinLeave", {
pattern = "NvimTree_*", pattern = "NvimTree_*",
group = a.nvim_create_augroup("NvimTree", { clear = false }), group = vim.api.nvim_create_augroup("NvimTree", { clear = false }),
callback = function() callback = function()
if utils.is_nvim_tree_buf(0) then if utils.is_nvim_tree_buf(0) then
view.close() view.close()
@ -39,7 +37,7 @@ local function remove_overlay()
}) })
end end
a.nvim_win_close(overlay_winnr, { force = true }) vim.api.nvim_win_close(overlay_winnr, { force = true })
overlay_bufnr = nil overlay_bufnr = nil
overlay_winnr = nil overlay_winnr = nil
@ -85,54 +83,54 @@ end
local function record_char() local function record_char()
vim.schedule(function() vim.schedule(function()
M.filter = a.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1] M.filter = vim.api.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1]
M.apply_filter() M.apply_filter()
redraw() redraw()
end) end)
end end
local function configure_buffer_overlay() local function configure_buffer_overlay()
overlay_bufnr = a.nvim_create_buf(false, true) overlay_bufnr = vim.api.nvim_create_buf(false, true)
a.nvim_buf_attach(overlay_bufnr, true, { vim.api.nvim_buf_attach(overlay_bufnr, true, {
on_lines = record_char, on_lines = record_char,
}) })
a.nvim_create_autocmd("InsertLeave", { vim.api.nvim_create_autocmd("InsertLeave", {
callback = remove_overlay, callback = remove_overlay,
once = true, once = true,
}) })
a.nvim_buf_set_keymap(overlay_bufnr, "i", "<CR>", "<cmd>stopinsert<CR>", {}) vim.api.nvim_buf_set_keymap(overlay_bufnr, "i", "<CR>", "<cmd>stopinsert<CR>", {})
end end
local function create_overlay() local function create_overlay()
local min_width = 20 local min_width = 20
if view.View.float.enable then if view.View.float.enable then
-- don't close nvim-tree float when focus is changed to filter window -- don't close nvim-tree float when focus is changed to filter window
a.nvim_clear_autocmds { vim.api.nvim_clear_autocmds {
event = "WinLeave", event = "WinLeave",
pattern = "NvimTree_*", pattern = "NvimTree_*",
group = a.nvim_create_augroup("NvimTree", { clear = false }), group = vim.api.nvim_create_augroup("NvimTree", { clear = false }),
} }
min_width = min_width - 2 min_width = min_width - 2
end end
configure_buffer_overlay() configure_buffer_overlay()
overlay_winnr = a.nvim_open_win(overlay_bufnr, true, { overlay_winnr = vim.api.nvim_open_win(overlay_bufnr, true, {
col = 1, col = 1,
row = 0, row = 0,
relative = "cursor", relative = "cursor",
width = math.max(min_width, a.nvim_win_get_width(view.get_winnr()) - #M.prefix - 2), width = math.max(min_width, vim.api.nvim_win_get_width(view.get_winnr()) - #M.prefix - 2),
height = 1, height = 1,
border = "none", border = "none",
style = "minimal", style = "minimal",
}) })
a.nvim_buf_set_option(overlay_bufnr, "modifiable", true) vim.api.nvim_buf_set_option(overlay_bufnr, "modifiable", true)
a.nvim_buf_set_lines(overlay_bufnr, 0, -1, false, { M.filter }) vim.api.nvim_buf_set_lines(overlay_bufnr, 0, -1, false, { M.filter })
vim.cmd "startinsert" vim.cmd "startinsert"
a.nvim_win_set_cursor(overlay_winnr, { 1, #M.filter + 1 }) vim.api.nvim_win_set_cursor(overlay_winnr, { 1, #M.filter + 1 })
end end
function M.start_filtering() function M.start_filtering()

View File

@ -1,5 +1,3 @@
local uv = vim.loop
local M = { local M = {
config = nil, config = nil,
path = nil, path = nil,
@ -29,7 +27,7 @@ function M.profile_start(fmt, ...)
return return
end end
M.line("profile", "START " .. (fmt or "???"), ...) M.line("profile", "START " .. (fmt or "???"), ...)
return uv.hrtime() return vim.loop.hrtime()
end end
--- Write to log file via M.line --- Write to log file via M.line
@ -39,7 +37,7 @@ function M.profile_end(start, fmt, ...)
if not M.path or not M.config.types.profile and not M.config.types.all then if not M.path or not M.config.types.profile and not M.config.types.all then
return return
end end
local millis = start and math.modf((uv.hrtime() - start) / 1000000) or -1 local millis = start and math.modf((vim.loop.hrtime() - start) / 1000000) or -1
M.line("profile", "END " .. (fmt or "???") .. " " .. millis .. "ms", ...) M.line("profile", "END " .. (fmt or "???") .. " " .. millis .. "ms", ...)
end end

View File

@ -1,30 +1,28 @@
local M = {} local M = {}
local api = vim.api
local fn = vim.fn
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local function hide(win) local function hide(win)
if win then if win then
if api.nvim_win_is_valid(win) then if vim.api.nvim_win_is_valid(win) then
api.nvim_win_close(win, true) vim.api.nvim_win_close(win, true)
end end
end end
end end
-- reduce signcolumn/foldcolumn from window width -- reduce signcolumn/foldcolumn from window width
local function effective_win_width() local function effective_win_width()
local win_width = fn.winwidth(0) local win_width = vim.fn.winwidth(0)
-- return zero if the window cannot be found -- return zero if the window cannot be found
local win_id = fn.win_getid() local win_id = vim.fn.win_getid()
if win_id == 0 then if win_id == 0 then
return win_width return win_width
end end
-- if the window does not exist the result is an empty list -- if the window does not exist the result is an empty list
local win_info = fn.getwininfo(win_id) local win_info = vim.fn.getwininfo(win_id)
-- check if result table is empty -- check if result table is empty
if next(win_info) == nil then if next(win_info) == nil then
@ -35,7 +33,7 @@ local function effective_win_width()
end end
local function show() local function show()
local line_nr = api.nvim_win_get_cursor(0)[1] local line_nr = vim.api.nvim_win_get_cursor(0)[1]
if line_nr == 1 and require("nvim-tree.view").is_root_folder_visible() then if line_nr == 1 and require("nvim-tree.view").is_root_folder_visible() then
return return
end end
@ -47,36 +45,36 @@ local function show()
return return
end end
local line = fn.getline "." local line = vim.fn.getline "."
local leftcol = fn.winsaveview().leftcol local leftcol = vim.fn.winsaveview().leftcol
-- hide full name if left column of node in nvim-tree win is not zero -- hide full name if left column of node in nvim-tree win is not zero
if leftcol ~= 0 then if leftcol ~= 0 then
return return
end end
local text_width = fn.strdisplaywidth(fn.substitute(line, "[^[:print:]]*$", "", "g")) local text_width = vim.fn.strdisplaywidth(vim.fn.substitute(line, "[^[:print:]]*$", "", "g"))
local win_width = effective_win_width() local win_width = effective_win_width()
if text_width < win_width then if text_width < win_width then
return return
end end
M.popup_win = api.nvim_open_win(api.nvim_create_buf(false, false), false, { M.popup_win = vim.api.nvim_open_win(vim.api.nvim_create_buf(false, false), false, {
relative = "win", relative = "win",
bufpos = { fn.line "." - 2, 0 }, bufpos = { vim.fn.line "." - 2, 0 },
width = math.min(text_width, vim.o.columns - 2), width = math.min(text_width, vim.o.columns - 2),
height = 1, height = 1,
noautocmd = true, noautocmd = true,
style = "minimal", style = "minimal",
}) })
local ns_id = api.nvim_get_namespaces()["NvimTreeHighlights"] local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
local extmarks = api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 }) local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 })
api.nvim_win_call(M.popup_win, function() vim.api.nvim_win_call(M.popup_win, function()
fn.setbufline("%", 1, line) vim.fn.setbufline("%", 1, line)
for _, extmark in ipairs(extmarks) do for _, extmark in ipairs(extmarks) do
local hl = extmark[4] local hl = extmark[4]
api.nvim_buf_add_highlight(0, ns_id, hl.hl_group, 0, extmark[3], hl.end_col) vim.api.nvim_buf_add_highlight(0, ns_id, hl.hl_group, 0, extmark[3], hl.end_col)
end end
vim.cmd [[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=hide ]] vim.cmd [[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=hide ]]
end) end)
@ -88,8 +86,8 @@ M.setup = function(opts)
return return
end end
local group = api.nvim_create_augroup("nvim_tree_floating_node", { clear = true }) local group = vim.api.nvim_create_augroup("nvim_tree_floating_node", { clear = true })
api.nvim_create_autocmd({ "BufLeave", "CursorMoved" }, { vim.api.nvim_create_autocmd({ "BufLeave", "CursorMoved" }, {
group = group, group = group,
pattern = { "NvimTree_*" }, pattern = { "NvimTree_*" },
callback = function() callback = function()
@ -99,7 +97,7 @@ M.setup = function(opts)
end, end,
}) })
api.nvim_create_autocmd({ "CursorMoved" }, { vim.api.nvim_create_autocmd({ "CursorMoved" }, {
group = group, group = group,
pattern = { "NvimTree_*" }, pattern = { "NvimTree_*" },
callback = function() callback = function()

View File

@ -12,19 +12,17 @@ local Builder = require "nvim-tree.renderer.builder"
local live_filter = require "nvim-tree.live-filter" local live_filter = require "nvim-tree.live-filter"
local marks = require "nvim-tree.marks" local marks = require "nvim-tree.marks"
local api = vim.api
local M = { local M = {
last_highlights = {}, last_highlights = {},
} }
local namespace_id = api.nvim_create_namespace "NvimTreeHighlights" local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
local function _draw(bufnr, lines, hl, signs) local function _draw(bufnr, lines, hl, signs)
api.nvim_buf_set_option(bufnr, "modifiable", true) vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
M.render_hl(bufnr, hl) M.render_hl(bufnr, hl)
api.nvim_buf_set_option(bufnr, "modifiable", false) vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
vim.fn.sign_unplace(git.SIGN_GROUP) vim.fn.sign_unplace(git.SIGN_GROUP)
for _, sign in pairs(signs) do for _, sign in pairs(signs) do
vim.fn.sign_place(0, git.SIGN_GROUP, sign.sign, bufnr, { lnum = sign.lnum, priority = 1 }) vim.fn.sign_place(0, git.SIGN_GROUP, sign.sign, bufnr, { lnum = sign.lnum, priority = 1 })
@ -32,12 +30,12 @@ local function _draw(bufnr, lines, hl, signs)
end end
function M.render_hl(bufnr, hl) function M.render_hl(bufnr, hl)
if not bufnr or not api.nvim_buf_is_loaded(bufnr) then if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return return
end end
api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1) vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
for _, data in ipairs(hl or M.last_highlights) do for _, data in ipairs(hl or M.last_highlights) do
api.nvim_buf_add_highlight(bufnr, namespace_id, data[1], data[2], data[3], data[4]) vim.api.nvim_buf_add_highlight(bufnr, namespace_id, data[1], data[2], data[3], data[4])
end end
end end
@ -50,13 +48,13 @@ local picture_map = {
function M.draw() function M.draw()
local bufnr = view.get_bufnr() local bufnr = view.get_bufnr()
if not core.get_explorer() or not bufnr or not api.nvim_buf_is_loaded(bufnr) then if not core.get_explorer() or not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return return
end end
local ps = log.profile_start "draw" local ps = log.profile_start "draw"
local cursor = api.nvim_win_get_cursor(view.get_winnr()) local cursor = vim.api.nvim_win_get_cursor(view.get_winnr())
icon_component.reset_config() icon_component.reset_config()
local lines, hl local lines, hl
@ -84,7 +82,7 @@ function M.draw()
M.last_highlights = hl M.last_highlights = hl
if cursor and #lines >= cursor[1] then if cursor and #lines >= cursor[1] then
api.nvim_win_set_cursor(view.get_winnr(), cursor) vim.api.nvim_win_set_cursor(view.get_winnr(), cursor)
end end
if view.is_help_ui() then if view.is_help_ui() then

View File

@ -1,6 +1,3 @@
local a = vim.api
local uv = vim.loop
local Iterator = require "nvim-tree.iterators.node-iterator" local Iterator = require "nvim-tree.iterators.node-iterator"
local M = { local M = {
@ -19,16 +16,16 @@ function M.str_find(haystack, needle)
end end
function M.read_file(path) function M.read_file(path)
local fd = uv.fs_open(path, "r", 438) local fd = vim.loop.fs_open(path, "r", 438)
if not fd then if not fd then
return "" return ""
end end
local stat = uv.fs_fstat(fd) local stat = vim.loop.fs_fstat(fd)
if not stat then if not stat then
return "" return ""
end end
local data = uv.fs_read(fd, stat.size, 0) local data = vim.loop.fs_read(fd, stat.size, 0)
uv.fs_close(fd) vim.loop.fs_close(fd)
return data or "" return data or ""
end end
@ -211,19 +208,19 @@ function M.is_wsl_windows_fs_exe(ext)
end end
function M.rename_loaded_buffers(old_path, new_path) function M.rename_loaded_buffers(old_path, new_path)
for _, buf in pairs(a.nvim_list_bufs()) do for _, buf in pairs(vim.api.nvim_list_bufs()) do
if a.nvim_buf_is_loaded(buf) then if vim.api.nvim_buf_is_loaded(buf) then
local buf_name = a.nvim_buf_get_name(buf) local buf_name = vim.api.nvim_buf_get_name(buf)
local exact_match = buf_name == old_path local exact_match = buf_name == old_path
local child_match = ( local child_match = (
buf_name:sub(1, #old_path) == old_path and buf_name:sub(#old_path + 1, #old_path + 1) == path_separator buf_name:sub(1, #old_path) == old_path and buf_name:sub(#old_path + 1, #old_path + 1) == path_separator
) )
if exact_match or child_match then if exact_match or child_match then
a.nvim_buf_set_name(buf, new_path .. buf_name:sub(#old_path + 1)) vim.api.nvim_buf_set_name(buf, new_path .. buf_name:sub(#old_path + 1))
-- to avoid the 'overwrite existing file' error message on write for -- to avoid the 'overwrite existing file' error message on write for
-- normal files -- normal files
if a.nvim_buf_get_option(buf, "buftype") == "" then if vim.api.nvim_buf_get_option(buf, "buftype") == "" then
a.nvim_buf_call(buf, function() vim.api.nvim_buf_call(buf, function()
vim.cmd "silent! write!" vim.cmd "silent! write!"
vim.cmd "edit" vim.cmd "edit"
end) end)
@ -369,7 +366,7 @@ function M.debounce(context, timeout, callback)
timer_stop_close(debouncer.timer) timer_stop_close(debouncer.timer)
end end
local timer = uv.new_timer() local timer = vim.loop.new_timer()
debouncer.timer = timer debouncer.timer = timer
timer:start(timeout, 0, function() timer:start(timeout, 0, function()
timer_stop_close(timer) timer_stop_close(timer)
@ -457,7 +454,7 @@ function M.is_nvim_tree_buf(bufnr)
bufnr = 0 bufnr = 0
end end
if vim.fn.bufexists(bufnr) then if vim.fn.bufexists(bufnr) then
local bufname = a.nvim_buf_get_name(bufnr) local bufname = vim.api.nvim_buf_get_name(bufnr)
if vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" then if vim.fn.fnamemodify(bufname, ":t"):match "^NvimTree_[0-9]+$" then
if vim.bo[bufnr].filetype == "NvimTree" then if vim.bo[bufnr].filetype == "NvimTree" then
return true return true

View File

@ -1,5 +1,3 @@
local a = vim.api
local M = {} local M = {}
local events = require "nvim-tree.events" local events = require "nvim-tree.events"
@ -77,9 +75,9 @@ local function matches_bufnr(bufnr)
end end
local function wipe_rogue_buffer() local function wipe_rogue_buffer()
for _, bufnr in ipairs(a.nvim_list_bufs()) do for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if not matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then if not matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then
pcall(a.nvim_buf_delete, bufnr, { force = true }) pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
end end
end end
end end
@ -87,9 +85,9 @@ end
local function create_buffer(bufnr) local function create_buffer(bufnr)
wipe_rogue_buffer() wipe_rogue_buffer()
local tab = a.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
BUFNR_PER_TAB[tab] = bufnr or a.nvim_create_buf(false, false) BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
a.nvim_buf_set_name(M.get_bufnr(), "NvimTree_" .. tab) vim.api.nvim_buf_set_name(M.get_bufnr(), "NvimTree_" .. tab)
for option, value in pairs(BUFFER_OPTIONS) do for option, value in pairs(BUFFER_OPTIONS) do
vim.bo[M.get_bufnr()][option] = value vim.bo[M.get_bufnr()][option] = value
@ -122,7 +120,7 @@ local move_tbl = {
-- setup_tabpage sets up the initial state of a tab -- setup_tabpage sets up the initial state of a tab
local function setup_tabpage(tabpage) local function setup_tabpage(tabpage)
local winnr = a.nvim_get_current_win() local winnr = vim.api.nvim_get_current_win()
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr }) M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr })
end end
@ -143,17 +141,17 @@ end
local function open_window() local function open_window()
if M.View.float.enable then if M.View.float.enable then
a.nvim_open_win(0, true, open_win_config()) vim.api.nvim_open_win(0, true, open_win_config())
else else
a.nvim_command "vsp" vim.api.nvim_command "vsp"
M.reposition_window() M.reposition_window()
end end
setup_tabpage(a.nvim_get_current_tabpage()) setup_tabpage(vim.api.nvim_get_current_tabpage())
set_window_options_and_buffer() set_window_options_and_buffer()
end end
local function is_buf_displayed(buf) local function is_buf_displayed(buf)
return a.nvim_buf_is_valid(buf) and vim.fn.buflisted(buf) == 1 return vim.api.nvim_buf_is_valid(buf) and vim.fn.buflisted(buf) == 1
end end
local function get_alt_or_next_buf() local function get_alt_or_next_buf()
@ -162,7 +160,7 @@ local function get_alt_or_next_buf()
return alt_buf return alt_buf
end end
for _, buf in ipairs(a.nvim_list_bufs()) do for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if is_buf_displayed(buf) then if is_buf_displayed(buf) then
return buf return buf
end end
@ -170,7 +168,7 @@ local function get_alt_or_next_buf()
end end
local function switch_buf_if_last_buf() local function switch_buf_if_last_buf()
if #a.nvim_list_wins() == 1 then if #vim.api.nvim_list_wins() == 1 then
local buf = get_alt_or_next_buf() local buf = get_alt_or_next_buf()
if buf then if buf then
vim.cmd("sb" .. buf) vim.cmd("sb" .. buf)
@ -182,8 +180,8 @@ end
-- save_tab_state saves any state that should be preserved across redraws. -- save_tab_state saves any state that should be preserved across redraws.
local function save_tab_state() local function save_tab_state()
local tabpage = a.nvim_get_current_tabpage() local tabpage = vim.api.nvim_get_current_tabpage()
M.View.cursors[tabpage] = a.nvim_win_get_cursor(M.get_winnr()) M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr())
end end
function M.close() function M.close()
@ -193,15 +191,15 @@ function M.close()
save_tab_state() save_tab_state()
switch_buf_if_last_buf() switch_buf_if_last_buf()
local tree_win = M.get_winnr() local tree_win = M.get_winnr()
local current_win = a.nvim_get_current_win() local current_win = vim.api.nvim_get_current_win()
for _, win in pairs(a.nvim_list_wins()) do for _, win in pairs(vim.api.nvim_list_wins()) do
if tree_win ~= win and a.nvim_win_get_config(win).relative == "" then if tree_win ~= win and vim.api.nvim_win_get_config(win).relative == "" then
local prev_win = vim.fn.winnr "#" -- this tab only local prev_win = vim.fn.winnr "#" -- this tab only
if tree_win == current_win and prev_win > 0 then if tree_win == current_win and prev_win > 0 then
a.nvim_set_current_win(vim.fn.win_getid(prev_win)) vim.api.nvim_set_current_win(vim.fn.win_getid(prev_win))
end end
if a.nvim_win_is_valid(tree_win) then if vim.api.nvim_win_is_valid(tree_win) then
a.nvim_win_close(tree_win, true) vim.api.nvim_win_close(tree_win, true)
end end
events._dispatch_on_tree_close() events._dispatch_on_tree_close()
return return
@ -275,7 +273,7 @@ function M.resize(size)
end end
local new_size = get_size() local new_size = get_size()
a.nvim_win_set_width(M.get_winnr(), new_size) vim.api.nvim_win_set_width(M.get_winnr(), new_size)
events._dispatch_on_tree_resize(new_size) events._dispatch_on_tree_resize(new_size)
@ -286,19 +284,19 @@ end
function M.reposition_window() function M.reposition_window()
local move_to = move_tbl[M.View.side] local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd " .. move_to) vim.api.nvim_command("wincmd " .. move_to)
M.resize() M.resize()
end end
local function set_current_win() local function set_current_win()
local current_tab = a.nvim_get_current_tabpage() local current_tab = vim.api.nvim_get_current_tabpage()
M.View.tabpages[current_tab].winnr = a.nvim_get_current_win() M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
end end
function M.open_in_current_win(opts) function M.open_in_current_win(opts)
opts = opts or { hijack_current_buf = true, resize = true } opts = opts or { hijack_current_buf = true, resize = true }
create_buffer(opts.hijack_current_buf and a.nvim_get_current_buf()) create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf())
setup_tabpage(a.nvim_get_current_tabpage()) setup_tabpage(vim.api.nvim_get_current_tabpage())
set_current_win() set_current_win()
set_window_options_and_buffer() set_window_options_and_buffer()
if opts.resize then if opts.resize then
@ -308,7 +306,7 @@ function M.open_in_current_win(opts)
end end
function M.abandon_current_window() function M.abandon_current_window()
local tab = a.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
BUFNR_PER_TAB[tab] = nil BUFNR_PER_TAB[tab] = nil
M.View.tabpages[tab].winnr = nil M.View.tabpages[tab].winnr = nil
end end
@ -316,26 +314,26 @@ end
function M.is_visible(opts) function M.is_visible(opts)
if opts and opts.any_tabpage then if opts and opts.any_tabpage then
for _, v in pairs(M.View.tabpages) do for _, v in pairs(M.View.tabpages) do
if v.winnr and a.nvim_win_is_valid(v.winnr) then if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
return true return true
end end
end end
return false return false
end end
return M.get_winnr() ~= nil and a.nvim_win_is_valid(M.get_winnr()) return M.get_winnr() ~= nil and vim.api.nvim_win_is_valid(M.get_winnr())
end end
function M.set_cursor(opts) function M.set_cursor(opts)
if M.is_visible() then if M.is_visible() then
pcall(a.nvim_win_set_cursor, M.get_winnr(), opts) pcall(vim.api.nvim_win_set_cursor, M.get_winnr(), opts)
end end
end end
function M.focus(winnr, open_if_closed) function M.focus(winnr, open_if_closed)
local wnr = winnr or M.get_winnr() local wnr = winnr or M.get_winnr()
if a.nvim_win_get_tabpage(wnr or 0) ~= a.nvim_win_get_tabpage(0) then if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then
M.close() M.close()
M.open() M.open()
wnr = M.get_winnr() wnr = M.get_winnr()
@ -343,12 +341,12 @@ function M.focus(winnr, open_if_closed)
M.open() M.open()
end end
a.nvim_set_current_win(wnr) vim.api.nvim_set_current_win(wnr)
end end
--- Restores the state of a NvimTree window if it was initialized before. --- Restores the state of a NvimTree window if it was initialized before.
function M.restore_tab_state() function M.restore_tab_state()
local tabpage = a.nvim_get_current_tabpage() local tabpage = vim.api.nvim_get_current_tabpage()
M.set_cursor(M.View.cursors[tabpage]) M.set_cursor(M.View.cursors[tabpage])
end end
@ -356,7 +354,7 @@ end
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number ---@return number
function M.get_winnr(tabpage) function M.get_winnr(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage() tabpage = tabpage or vim.api.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage] local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then if tabinfo ~= nil then
return tabinfo.winnr return tabinfo.winnr
@ -366,14 +364,14 @@ end
--- Returns the current nvim tree bufnr --- Returns the current nvim tree bufnr
---@return number ---@return number
function M.get_bufnr() function M.get_bufnr()
return BUFNR_PER_TAB[a.nvim_get_current_tabpage()] return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
end end
--- Checks if nvim-tree is displaying the help ui within the tabpage specified --- Checks if nvim-tree is displaying the help ui within the tabpage specified
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number ---@return number
function M.is_help_ui(tabpage) function M.is_help_ui(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage() tabpage = tabpage or vim.api.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage] local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then if tabinfo ~= nil then
return tabinfo.help return tabinfo.help
@ -381,12 +379,12 @@ function M.is_help_ui(tabpage)
end end
function M.toggle_help(tabpage) function M.toggle_help(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage() tabpage = tabpage or vim.api.nvim_get_current_tabpage()
M.View.tabpages[tabpage].help = not M.View.tabpages[tabpage].help M.View.tabpages[tabpage].help = not M.View.tabpages[tabpage].help
end end
function M.is_buf_valid(bufnr) function M.is_buf_valid(bufnr)
return bufnr and a.nvim_buf_is_valid(bufnr) and a.nvim_buf_is_loaded(bufnr) return bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr)
end end
function M._prevent_buffer_override() function M._prevent_buffer_override()
@ -397,10 +395,10 @@ function M._prevent_buffer_override()
-- because this event needs to be run on bufWipeout. -- because this event needs to be run on bufWipeout.
-- Otherwise the curwin/curbuf would match the view buffer and the view window. -- Otherwise the curwin/curbuf would match the view buffer and the view window.
vim.schedule(function() vim.schedule(function()
local curwin = a.nvim_get_current_win() local curwin = vim.api.nvim_get_current_win()
local curwinconfig = a.nvim_win_get_config(curwin) local curwinconfig = vim.api.nvim_win_get_config(curwin)
local curbuf = a.nvim_win_get_buf(curwin) local curbuf = vim.api.nvim_win_get_buf(curwin)
local bufname = a.nvim_buf_get_name(curbuf) local bufname = vim.api.nvim_buf_get_name(curbuf)
if not bufname:match "NvimTree" then if not bufname:match "NvimTree" then
for i, tabpage in ipairs(M.View.tabpages) do for i, tabpage in ipairs(M.View.tabpages) do
@ -420,7 +418,7 @@ function M._prevent_buffer_override()
vim.cmd "setlocal nowinfixheight" vim.cmd "setlocal nowinfixheight"
M.open { focus_tree = false } M.open { focus_tree = false }
require("nvim-tree.renderer").draw() require("nvim-tree.renderer").draw()
pcall(a.nvim_win_close, curwin, { force = true }) pcall(vim.api.nvim_win_close, curwin, { force = true })
-- to handle opening a file using :e when nvim-tree is on floating mode -- to handle opening a file using :e when nvim-tree is on floating mode
-- falling back to the current window instead of creating a new one -- falling back to the current window instead of creating a new one

View File

@ -1,4 +1,3 @@
local uv = vim.loop
local notify = require "nvim-tree.notify" local notify = require "nvim-tree.notify"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
@ -45,7 +44,7 @@ function Event:start()
local rc, _, name local rc, _, name
self._fs_event, _, name = uv.new_fs_event() self._fs_event, _, name = vim.loop.new_fs_event()
if not self._fs_event then if not self._fs_event then
self._fs_event = nil self._fs_event = nil
notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self._path, name)) notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self._path, name))