Skip to content
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

Not For Merge - Sync closing of nvim-tree across tabs #1704

Closed
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
3 changes: 2 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lua/nvim-tree/actions/dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions lua/nvim-tree/actions/finders/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 4 additions & 12 deletions lua/nvim-tree/actions/fs/create-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions lua/nvim-tree/actions/reloaders/reloaders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
18 changes: 17 additions & 1 deletion lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down