Skip to content

Commit 43ee246

Browse files
committed
Sync closing of nvim-tree across tabs
1 parent 65c2ba8 commit 43ee246

File tree

8 files changed

+27
-14
lines changed

8 files changed

+27
-14
lines changed

doc/nvim-tree-lua.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,9 @@ You can easily implement a toggle using this too:
10491049
>
10501050
local function toggle_replace()
10511051
local view = require"nvim-tree.view"
1052+
local api = require"nvim-tree.api"
10521053
if view.is_visible() then
1053-
view.close()
1054+
api.close()
10541055
else
10551056
require"nvim-tree".open_replacing_current_buffer()
10561057
end

lua/nvim-tree.lua

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

7474
function M.toggle(find_file, no_focus, cwd, bang)
7575
if view.is_visible() then
76-
view.close()
76+
M.close()
7777
else
7878
local previous_buf = api.nvim_get_current_buf()
7979
M.open(cwd)
@@ -86,6 +86,11 @@ function M.toggle(find_file, no_focus, cwd, bang)
8686
end
8787
end
8888

89+
function M.close()
90+
local config = M.get_config()
91+
view.close(config.open_on_tab)
92+
end
93+
8994
function M.open(cwd)
9095
cwd = cwd ~= "" and cwd or nil
9196
if view.is_visible() then
@@ -441,7 +446,7 @@ local function setup_autocommands(opts)
441446
pattern = "NvimTree_*",
442447
callback = function()
443448
if utils.is_nvim_tree_buf(0) then
444-
view.close()
449+
M.close()
445450
end
446451
end,
447452
})
@@ -766,11 +771,11 @@ function M.setup(conf)
766771
require("nvim-tree.watcher").purge_watchers()
767772

768773
if not M.setup_called then
769-
setup_vim_commands()
774+
setup_vim_commands(opts)
770775
end
771776

772777
if M.setup_called and view.is_visible() then
773-
view.close()
778+
M.close()
774779
view.abandon_current_window()
775780
end
776781

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
@@ -126,7 +126,7 @@ end
126126

127127
local function open_file_in_tab(filename)
128128
if M.quit_on_open then
129-
view.close()
129+
api.close()
130130
end
131131
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
132132
end
@@ -280,7 +280,7 @@ function M.fn(mode, filename)
280280
end
281281

282282
if M.quit_on_open then
283-
view.close()
283+
api.close()
284284
end
285285
end
286286

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
@@ -119,7 +119,7 @@ function M.open(cwd)
119119
core.init(cwd or vim.loop.cwd())
120120
end
121121
if should_hijack_current_buf() then
122-
view.close()
122+
api.close()
123123
view.open_in_current_win()
124124
renderer.draw()
125125
else

lua/nvim-tree/live-filter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ local function remove_overlay()
3333
group = a.nvim_create_augroup("NvimTree", { clear = false }),
3434
callback = function()
3535
if utils.is_nvim_tree_buf(0) then
36-
view.close()
36+
a.close()
3737
end
3838
end,
3939
})

lua/nvim-tree/view.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ local function save_tab_state()
186186
M.View.cursors[tabpage] = a.nvim_win_get_cursor(M.get_winnr())
187187
end
188188

189-
function M.close()
189+
function M.close(all_tabpages)
190190
if not M.is_visible() then
191191
return
192192
end
@@ -200,8 +200,13 @@ function M.close()
200200
if tree_win == current_win and prev_win > 0 then
201201
a.nvim_set_current_win(vim.fn.win_getid(prev_win))
202202
end
203-
if a.nvim_win_is_valid(tree_win) then
204-
a.nvim_win_close(tree_win, true)
203+
a.nvim_win_close(tree_win, true)
204+
if all_tabpages then
205+
for _, v in pairs(M.View.tabpages) do
206+
if v.winnr and a.nvim_win_is_valid(v.winnr) then
207+
a.nvim_win_close(v.winnr, true)
208+
end
209+
end
205210
end
206211
events._dispatch_on_tree_close()
207212
return

0 commit comments

Comments
 (0)