Skip to content

chore: remove vim.* "requires" #1701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 41 additions & 44 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
local luv = vim.loop
local api = vim.api

local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log"
local colors = require "nvim-tree.colors"
Expand Down Expand Up @@ -29,7 +26,7 @@ end

function M.change_root(filepath, bufnr)
-- 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
if utils.str_find(filepath, value) or utils.str_find(ft, value) then
return
Expand Down Expand Up @@ -75,7 +72,7 @@ function M.toggle(find_file, no_focus, cwd, bang)
if view.is_visible() then
view.close()
else
local previous_buf = api.nvim_get_current_buf()
local previous_buf = vim.api.nvim_get_current_buf()
M.open(cwd)
if _config.update_focused_file.enable or find_file then
M.find_file(false, previous_buf, bang)
Expand All @@ -101,8 +98,8 @@ function M.open_replacing_current_buffer(cwd)
return
end

local buf = api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(buf)
local buf = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(buf)
if bufname == "" or vim.loop.fs_stat(bufname) == nil then
return
end
Expand All @@ -121,8 +118,8 @@ end

function M.tab_change()
if view.is_visible { any_tabpage = true } then
local bufname = api.nvim_buf_get_name(0)
local ft = api.nvim_buf_get_option(0, "ft")
local bufname = vim.api.nvim_buf_get_name(0)
local ft = vim.api.nvim_buf_get_option(0, "ft")
for _, filter in ipairs(M.config.ignore_buf_on_tab_change) do
if bufname:match(filter) ~= nil or ft:match(filter) ~= nil then
return
Expand All @@ -135,26 +132,26 @@ end

local function find_existing_windows()
return vim.tbl_filter(function(win)
local buf = api.nvim_win_get_buf(win)
return api.nvim_buf_get_name(buf):match "NvimTree" ~= nil
end, api.nvim_list_wins())
local buf = vim.api.nvim_win_get_buf(win)
return vim.api.nvim_buf_get_name(buf):match "NvimTree" ~= nil
end, vim.api.nvim_list_wins())
end

local function is_file_readable(fname)
local stat = luv.fs_stat(fname)
return stat and stat.type == "file" and luv.fs_access(fname, "R")
local stat = vim.loop.fs_stat(fname)
return stat and stat.type == "file" and vim.loop.fs_access(fname, "R")
end

function M.find_file(with_open, bufnr, bang)
if not with_open and not core.get_explorer() then
return
end

bufnr = bufnr or api.nvim_get_current_buf()
if not api.nvim_buf_is_valid(bufnr) then
bufnr = bufnr or vim.api.nvim_get_current_buf()
if not vim.api.nvim_buf_is_valid(bufnr) then
return
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"))
if not is_file_readable(filepath) then
return
Expand All @@ -181,8 +178,8 @@ function M.open_on_directory()
return
end

local buf = api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(buf)
local buf = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(buf)
if vim.fn.isdirectory(bufname) ~= 1 then
return
end
Expand All @@ -198,7 +195,7 @@ end

local prev_line
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
return
end
Expand All @@ -209,22 +206,22 @@ function M.place_cursor_on_node()
return
end

local line = api.nvim_get_current_line()
local cursor = api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line()
local cursor = vim.api.nvim_win_get_cursor(0)
local idx = vim.fn.stridx(line, node.name)

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

function M.on_enter(netrw_disabled)
local bufnr = api.nvim_get_current_buf()
local bufname = api.nvim_buf_get_name(bufnr)
local buftype = api.nvim_buf_get_option(bufnr, "filetype")
local bufnr = vim.api.nvim_get_current_buf()
local bufname = vim.api.nvim_buf_get_name(bufnr)
local buftype = vim.api.nvim_buf_get_option(bufnr, "filetype")
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_file = stats and stats.type == "file"
local cwd
Expand All @@ -234,7 +231,7 @@ function M.on_enter(netrw_disabled)
vim.cmd("noautocmd cd " .. cwd)
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_is_dir = is_dir and netrw_disabled
Expand Down Expand Up @@ -265,7 +262,7 @@ function M.on_enter(netrw_disabled)
-- Session that left a NvimTree Buffer opened, reopen with it
local existing_tree_wins = find_existing_windows()
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

if should_open or should_hijack or existing_tree_wins[1] ~= nil then
Expand Down Expand Up @@ -297,27 +294,27 @@ local function manage_netrw(disable_netrw, hijack_netrw)
end

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)
end, { nargs = "?", complete = "dir" })
api.nvim_create_user_command("NvimTreeClose", view.close, { bar = true })
api.nvim_create_user_command("NvimTreeToggle", function(res)
vim.api.nvim_create_user_command("NvimTreeClose", view.close, { bar = true })
vim.api.nvim_create_user_command("NvimTreeToggle", function(res)
M.toggle(false, false, res.args)
end, { nargs = "?", complete = "dir" })
api.nvim_create_user_command("NvimTreeFocus", M.focus, { bar = true })
api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, { bar = true })
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("NvimTreeFocus", M.focus, { bar = true })
vim.api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, { bar = true })
vim.api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, { bar = true })
vim.api.nvim_create_user_command("NvimTreeFindFile", function(res)
M.find_file(true, nil, res.bang)
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)
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)
end, { nargs = 1, bar = true })
api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, { bar = true })
api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function()
vim.api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, { bar = true })
vim.api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function()
collapse_all.fn(true)
end, { bar = true })
end
Expand All @@ -331,10 +328,10 @@ function M.change_dir(name)
end

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 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

-- reset highlights when colorscheme is changed
Expand Down Expand Up @@ -410,9 +407,9 @@ local function setup_autocommands(opts)
create_nvim_tree_autocmd("BufEnter", {
pattern = "NvimTree_*",
callback = function()
local bufnr = api.nvim_get_current_buf()
local bufnr = vim.api.nvim_get_current_buf()
vim.schedule(function()
api.nvim_buf_call(bufnr, function()
vim.api.nvim_buf_call(bufnr, function()
vim.cmd [[norm! zz]]
end)
end)
Expand Down
3 changes: 1 addition & 2 deletions lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local log = require "nvim-tree.log"
local uv = vim.loop
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local renderer = require "nvim-tree.renderer"
Expand All @@ -18,7 +17,7 @@ function M.fn(fname)
end

-- 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
return
end
Expand Down
23 changes: 10 additions & 13 deletions lua/nvim-tree/actions/finders/search-node.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
local api = vim.api
local uv = vim.loop

local core = require "nvim-tree.core"
local filters = require "nvim-tree.explorer.filters"
local find_file = require("nvim-tree.actions.finders.find-file").fn
Expand All @@ -17,22 +14,22 @@ local function search(search_dir, input_path)
local function iter(dir)
local realpath, path, name, stat, handle, _

handle, _ = uv.fs_scandir(dir)
handle, _ = vim.loop.fs_scandir(dir)
if not handle then
return
end

realpath, _ = uv.fs_realpath(dir)
realpath, _ = vim.loop.fs_realpath(dir)
if not realpath or vim.tbl_contains(realpaths_searched, realpath) then
return
end
table.insert(realpaths_searched, realpath)

name, _ = uv.fs_scandir_next(handle)
name, _ = vim.loop.fs_scandir_next(handle)
while name do
path = dir .. "/" .. name

stat, _ = uv.fs_stat(path)
stat, _ = vim.loop.fs_stat(path)
if not stat then
break
end
Expand All @@ -50,7 +47,7 @@ local function search(search_dir, input_path)
end
end

name, _ = uv.fs_scandir_next(handle)
name, _ = vim.loop.fs_scandir_next(handle)
end
end

Expand All @@ -63,19 +60,19 @@ function M.fn()
end

-- temporarily set &path
local bufnr = api.nvim_get_current_buf()
local path_existed, path_opt = pcall(api.nvim_buf_get_option, bufnr, "path")
api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")
local bufnr = vim.api.nvim_get_current_buf()
local path_existed, path_opt = pcall(vim.api.nvim_buf_get_option, bufnr, "path")
vim.api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")

vim.ui.input({ prompt = "Search: ", completion = "file_in_path" }, function(input_path)
if not input_path or input_path == "" then
return
end
-- reset &path
if path_existed then
api.nvim_buf_set_option(bufnr, "path", path_opt)
vim.api.nvim_buf_set_option(bufnr, "path", path_opt)
else
api.nvim_buf_set_option(bufnr, "path", nil)
vim.api.nvim_buf_set_option(bufnr, "path", nil)
end

-- strip trailing slash
Expand Down
18 changes: 8 additions & 10 deletions lua/nvim-tree/actions/fs/copy-paste.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local uv = vim.loop

local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
Expand All @@ -17,7 +15,7 @@ local function do_copy(source, destination)
local source_stats, handle
local success, errmsg

source_stats, errmsg = uv.fs_stat(source)
source_stats, errmsg = vim.loop.fs_stat(source)
if not source_stats then
log.line("copy_paste", "do_copy fs_stat '%s' failed '%s'", source, errmsg)
return false, errmsg
Expand All @@ -31,29 +29,29 @@ local function do_copy(source, destination)
end

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
log.line("copy_paste", "do_copy fs_copyfile failed '%s'", errmsg)
return false, errmsg
end
return true
elseif source_stats.type == "directory" then
handle, errmsg = uv.fs_scandir(source)
handle, errmsg = vim.loop.fs_scandir(source)
if type(handle) == "string" then
return false, handle
elseif not handle then
log.line("copy_paste", "do_copy fs_scandir '%s' failed '%s'", source, errmsg)
return false, errmsg
end

success, errmsg = uv.fs_mkdir(destination, source_stats.mode)
success, errmsg = vim.loop.fs_mkdir(destination, source_stats.mode)
if not success then
log.line("copy_paste", "do_copy fs_mkdir '%s' failed '%s'", destination, errmsg)
return false, errmsg
end

while true do
local name, _ = uv.fs_scandir_next(handle)
local name, _ = vim.loop.fs_scandir_next(handle)
if not name then
break
end
Expand All @@ -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)

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
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg
Expand Down Expand Up @@ -155,7 +153,7 @@ local function do_paste(node, action_type, action_fn)
end

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
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
Expand Down Expand Up @@ -188,7 +186,7 @@ local function do_cut(source, destination)
return true
end

local success, errmsg = uv.fs_rename(source, destination)
local success, errmsg = vim.loop.fs_rename(source, destination)
if not success then
log.line("copy_paste", "do_cut fs_rename failed '%s'", errmsg)
return false, errmsg
Expand Down
Loading