Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,9 @@ You can easily implement a toggle using this too:
>
local function toggle_replace()
local view = require"nvim-tree.view"
local api = require"nvim-tree.api"
if view.is_visible() then
view.close()
api.close()
else
require"nvim-tree".open_replacing_current_buffer()
end
Expand Down
98 changes: 50 additions & 48 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 @@ -73,9 +70,9 @@ M.on_keypress = require("nvim-tree.actions.dispatch").dispatch

function M.toggle(find_file, no_focus, cwd, bang)
if view.is_visible() then
view.close()
M.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 @@ -86,6 +83,11 @@ function M.toggle(find_file, no_focus, cwd, bang)
end
end

function M.close()
local config = M.get_config()
view.close(config.open_on_tab)
end

function M.open(cwd)
cwd = cwd ~= "" and cwd or nil
if view.is_visible() then
Expand All @@ -101,8 +103,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 +123,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 +137,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 +183,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 +200,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 +211,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 +236,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 +267,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 +299,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 +333,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 +412,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 Expand Up @@ -441,7 +443,7 @@ local function setup_autocommands(opts)
pattern = "NvimTree_*",
callback = function()
if utils.is_nvim_tree_buf(0) then
view.close()
M.close()
end
end,
})
Expand Down Expand Up @@ -766,11 +768,11 @@ function M.setup(conf)
require("nvim-tree.watcher").purge_watchers()

if not M.setup_called then
setup_vim_commands()
setup_vim_commands(opts)
end

if M.setup_called and view.is_visible() then
view.close()
M.close()
view.abandon_current_window()
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 @@ -20,7 +19,7 @@ function M.fn(fname)

local ps = log.profile_start("find file %s", fname)
-- 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
Loading