diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 79ee7177150..08ac9649c6a 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1074,8 +1074,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 diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 46ddcbd5a2d..d9aaea42b5c 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -70,7 +70,8 @@ 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() + view.close() -- TODO Choose one + -- view.close_this_tab_only() -- TODO Choose one else local previous_buf = vim.api.nvim_get_current_buf() M.open(cwd) @@ -438,7 +439,8 @@ local function setup_autocommands(opts) pattern = "NvimTree_*", callback = function() if utils.is_nvim_tree_buf(0) then - view.close() + view.close() -- TODO Choose one + -- view.close_this_tab_only() -- TODO Choose one end end, }) @@ -773,7 +775,8 @@ function M.setup(conf) end if M.setup_called and view.is_visible() then - view.close() + view.close() -- TODO Choose one + -- view.close_this_tab_only() -- TODO Choose one view.abandon_current_window() end diff --git a/lua/nvim-tree/actions/dispatch.lua b/lua/nvim-tree/actions/dispatch.lua index 44eff9834b0..25959356eba 100644 --- a/lua/nvim-tree/actions/dispatch.lua +++ b/lua/nvim-tree/actions/dispatch.lua @@ -4,7 +4,8 @@ local lib = require "nvim-tree.lib" local M = {} local Actions = { - close = view.close, + -- close = view.close(), -- TODO Choose one + close = view.close_this_tab_only(), -- TODO Choose one -- Tree modifiers collapse_all = require("nvim-tree.actions.tree-modifiers.collapse-all").fn, diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 8fec0cb5ea6..58dbb9b217d 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -15,20 +15,23 @@ function M.fn(fname) if running[fname] or not core.get_explorer() then return end - running[fname] = true - local ps = log.profile_start("find file %s", fname) -- always match against the real path local fname_real = vim.loop.fs_realpath(fname) if not fname_real then return end - local line = core.get_nodes_starting_line() + running[fname] = true + + local ps = log.profile_start("find file %s", fname) + + -- first line is the root node + local line = core.get_nodes_starting_line() - 1 local absolute_paths_searched = {} - local found = Iterator.builder(core.get_explorer().nodes) + local found = Iterator.builder({ core.get_explorer() }) :matcher(function(node) return node.absolute_path == fname_real or node.link_to == fname_real end) @@ -45,9 +48,7 @@ function M.fn(fname) if abs_match or link_match then node.open = true - if #node.nodes == 0 then - core.get_explorer():expand(node) - end + core.get_explorer():expand(node) end end) :recursor(function(node) diff --git a/lua/nvim-tree/actions/fs/create-file.lua b/lua/nvim-tree/actions/fs/create-file.lua index f19e58041ef..80bf1e15406 100644 --- a/lua/nvim-tree/actions/fs/create-file.lua +++ b/lua/nvim-tree/actions/fs/create-file.lua @@ -2,7 +2,6 @@ local utils = require "nvim-tree.utils" local events = require "nvim-tree.events" local lib = require "nvim-tree.lib" local core = require "nvim-tree.core" -local watch = require "nvim-tree.explorer.watch" local notify = require "nvim-tree.notify" local M = {} @@ -99,22 +98,15 @@ function M.fn(node) is_error = true break end + events._dispatch_folder_created(path_to_create) end end if not is_error then notify.info(new_file_path .. " was properly created") end - events._dispatch_folder_created(new_file_path) - if M.enable_reload then - require("nvim-tree.actions.reloaders.reloaders").reload_explorer() - else - -- synchronous call required so that we may focus the file now - node = node.nodes ~= nil and node or node.parent - if node then - watch.refresh_path(node.absolute_path) - end - end - utils.focus_file(utils.path_remove_trailing(new_file_path)) + + -- implicitly refreshes contents + require("nvim-tree.actions.finders.find-file").fn(new_file_path) end) end diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index 1a88f62e29c..cd9506ea36c 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -143,7 +143,8 @@ end local function open_file_in_tab(filename) if M.quit_on_open then - view.close() + view.close() -- TODO Choose one + -- view.close_this_tab_only() -- TODO Choose one end vim.cmd("tabe " .. vim.fn.fnameescape(filename)) end @@ -306,7 +307,8 @@ function M.fn(mode, filename) end if M.quit_on_open then - view.close() + view.close() -- TODO Choose one + -- view.close_this_tab_only() -- TODO Choose one end end diff --git a/lua/nvim-tree/actions/reloaders/reloaders.lua b/lua/nvim-tree/actions/reloaders/reloaders.lua index c2b403a6954..8aac6f62246 100644 --- a/lua/nvim-tree/actions/reloaders/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders/reloaders.lua @@ -3,6 +3,7 @@ local view = require "nvim-tree.view" local renderer = require "nvim-tree.renderer" local explorer_module = require "nvim-tree.explorer" local core = require "nvim-tree.core" +local log = require "nvim-tree.log" local M = {} @@ -39,11 +40,16 @@ function M.reload_explorer() end event_running = true + local ps = log.profile_start "reload_explorer" + local projects = git.reload() refresh_nodes(core.get_explorer(), projects) if view.is_visible() then renderer.draw() end + + log.profile_end(ps, "reload_explorer") + event_running = false end @@ -53,9 +59,14 @@ function M.reload_git() end event_running = true + local ps = log.profile_start "reload_git" + local projects = git.reload() M.reload_node_status(core.get_explorer(), projects) renderer.draw() + + log.profile_end(ps, "reload_git") + event_running = false end diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index a2a3cc7cf8f..7c539b813e6 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -18,6 +18,7 @@ end Api.tree.open = require("nvim-tree").open Api.tree.toggle = require("nvim-tree").toggle Api.tree.close = require("nvim-tree.view").close +Api.tree.close_this_tab = require("nvim-tree.view").close_this_tab_only Api.tree.focus = require("nvim-tree").focus Api.tree.reload = require("nvim-tree.actions.reloaders.reloaders").reload_explorer Api.tree.change_root = require("nvim-tree").change_dir diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index bb66df44b38..e1f1420cad5 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -117,7 +117,8 @@ function M.open(cwd) core.init(cwd or vim.loop.cwd()) end if should_hijack_current_buf() then - view.close() + -- view.close() -- TODO Choose one + view.close_this_tab_only() -- TODO Choose one view.open_in_current_win() renderer.draw() else diff --git a/lua/nvim-tree/live-filter.lua b/lua/nvim-tree/live-filter.lua index df28911c45a..2c1e5985d07 100644 --- a/lua/nvim-tree/live-filter.lua +++ b/lua/nvim-tree/live-filter.lua @@ -31,7 +31,8 @@ local function remove_overlay() group = vim.api.nvim_create_augroup("NvimTree", { clear = false }), callback = function() if utils.is_nvim_tree_buf(0) then - view.close() + view.close() -- TODO Choose one + -- view.close_this_tab_only() -- TODO Choose one end end, }) diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 00e6f498c91..afda15fb1f0 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -184,7 +184,7 @@ local function save_tab_state() M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr()) end -function M.close() +local function close(all_tabpages) if not M.is_visible() then return end @@ -201,12 +201,27 @@ function M.close() if vim.api.nvim_win_is_valid(tree_win) then vim.api.nvim_win_close(tree_win, true) end + if all_tabpages then + for _, v in pairs(M.View.tabpages) do + if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then + vim.api.nvim_win_close(v.winnr, true) + end + end + end events._dispatch_on_tree_close() return end end end +function M.close_this_tab_only() + close(false) +end + +function M.close() + close(M.View.open_on_tab) +end + function M.open(options) if M.is_visible() then return @@ -450,6 +465,7 @@ function M.setup(opts) M.View.height = options.height M.View.initial_width = get_size() M.View.hide_root_folder = options.hide_root_folder + M.View.open_on_tab = opts.open_on_tab M.View.preserve_window_proportions = options.preserve_window_proportions M.View.winopts.number = options.number M.View.winopts.relativenumber = options.relativenumber diff --git a/lua/nvim-tree/watcher.lua b/lua/nvim-tree/watcher.lua index b9ff5523fb6..972147dfbd1 100644 --- a/lua/nvim-tree/watcher.lua +++ b/lua/nvim-tree/watcher.lua @@ -88,7 +88,7 @@ function Event:destroy(message) if self._fs_event then if message then - utils.notify.warn(message) + notify.warn(message) end local rc, _, name = self._fs_event:stop()