Skip to content

Commit 86e98ef

Browse files
committed
Sync closing of nvim-tree across tabs
1 parent 56b6262 commit 86e98ef

File tree

8 files changed

+28
-14
lines changed

8 files changed

+28
-14
lines changed

doc/nvim-tree-lua.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,9 @@ You can easily implement a toggle using this too:
10521052
>
10531053
local function toggle_replace()
10541054
local view = require"nvim-tree.view"
1055+
local api = require"nvim-tree.api"
10551056
if view.is_visible() then
1056-
view.close()
1057+
api.close()
10571058
else
10581059
require"nvim-tree".open_replacing_current_buffer()
10591060
end

lua/nvim-tree.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ M.on_keypress = require("nvim-tree.actions.dispatch").dispatch
7070

7171
function M.toggle(find_file, no_focus, cwd, bang)
7272
if view.is_visible() then
73-
view.close()
73+
M.close()
7474
else
7575
local previous_buf = vim.api.nvim_get_current_buf()
7676
M.open(cwd)
@@ -83,6 +83,11 @@ function M.toggle(find_file, no_focus, cwd, bang)
8383
end
8484
end
8585

86+
function M.close()
87+
local config = M.get_config()
88+
view.close(config.open_on_tab)
89+
end
90+
8691
function M.open(cwd)
8792
cwd = cwd ~= "" and cwd or nil
8893
if view.is_visible() then
@@ -438,7 +443,7 @@ local function setup_autocommands(opts)
438443
pattern = "NvimTree_*",
439444
callback = function()
440445
if utils.is_nvim_tree_buf(0) then
441-
view.close()
446+
M.close()
442447
end
443448
end,
444449
})
@@ -763,11 +768,11 @@ function M.setup(conf)
763768
require("nvim-tree.watcher").purge_watchers()
764769

765770
if not M.setup_called then
766-
setup_vim_commands()
771+
setup_vim_commands(opts)
767772
end
768773

769774
if M.setup_called and view.is_visible() then
770-
view.close()
775+
M.close()
771776
view.abandon_current_window()
772777
end
773778

lua/nvim-tree/actions/dispatch.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
local api = vim.api
2+
13
local view = require "nvim-tree.view"
24
local lib = require "nvim-tree.lib"
35

46
local M = {}
57

68
local Actions = {
7-
close = view.close,
9+
close = api.close,
810

911
-- Tree modifiers
1012
collapse_all = require("nvim-tree.actions.tree-modifiers.collapse-all").fn,

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ end
132132

133133
local function open_file_in_tab(filename)
134134
if M.quit_on_open then
135-
view.close()
135+
api.close()
136136
end
137137
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
138138
end
@@ -299,7 +299,7 @@ function M.fn(mode, filename)
299299
end
300300

301301
if M.quit_on_open then
302-
view.close()
302+
api.close()
303303
end
304304
end
305305

lua/nvim-tree/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ end
1717

1818
Api.tree.open = require("nvim-tree").open
1919
Api.tree.toggle = require("nvim-tree").toggle
20-
Api.tree.close = require("nvim-tree.view").close
20+
Api.tree.close = require("nvim-tree").close
2121
Api.tree.focus = require("nvim-tree").focus
2222
Api.tree.reload = require("nvim-tree.actions.reloaders.reloaders").reload_explorer
2323
Api.tree.change_root = require("nvim-tree").change_dir

lua/nvim-tree/lib.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function M.open(cwd)
117117
core.init(cwd or vim.loop.cwd())
118118
end
119119
if should_hijack_current_buf() then
120-
view.close()
120+
api.close()
121121
view.open_in_current_win()
122122
renderer.draw()
123123
else

lua/nvim-tree/live-filter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ local function remove_overlay()
3131
group = vim.api.nvim_create_augroup("NvimTree", { clear = false }),
3232
callback = function()
3333
if utils.is_nvim_tree_buf(0) then
34-
view.close()
34+
a.close()
3535
end
3636
end,
3737
})

lua/nvim-tree/view.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ local function matches_bufnr(bufnr)
7575
end
7676

7777
local function wipe_rogue_buffer()
78-
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
79-
if not matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then
78+
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do if not matches_bufnr(bufnr) and utils.is_nvim_tree_buf(bufnr) then
8079
pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
8180
end
8281
end
@@ -184,7 +183,7 @@ local function save_tab_state()
184183
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr())
185184
end
186185

187-
function M.close()
186+
function M.close(all_tabpages)
188187
if not M.is_visible() then
189188
return
190189
end
@@ -201,6 +200,13 @@ function M.close()
201200
if vim.api.nvim_win_is_valid(tree_win) then
202201
vim.api.nvim_win_close(tree_win, true)
203202
end
203+
if all_tabpages then
204+
for _, v in pairs(M.View.tabpages) do
205+
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
206+
vim.api.nvim_win_close(v.winnr, true)
207+
end
208+
end
209+
end
204210
events._dispatch_on_tree_close()
205211
return
206212
end

0 commit comments

Comments
 (0)