Skip to content

Sync closing of nvim-tree across tabs #1698

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

Merged
merged 17 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1049,8 +1049,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
13 changes: 9 additions & 4 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ 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()
M.open(cwd)
Expand All @@ -86,6 +86,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 Down Expand Up @@ -441,7 +446,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 +771,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
4 changes: 3 additions & 1 deletion lua/nvim-tree/actions/dispatch.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local api = vim.api

local view = require "nvim-tree.view"
local lib = require "nvim-tree.lib"

local M = {}

local Actions = {
close = view.close,
close = api.close,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're referencing the vim api instead of nvim-tree


-- Tree modifiers
collapse_all = require("nvim-tree.actions.tree-modifiers.collapse-all").fn,
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ end

local function open_file_in_tab(filename)
if M.quit_on_open then
view.close()
api.close()
end
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
end
Expand Down Expand Up @@ -280,7 +280,7 @@ function M.fn(mode, filename)
end

if M.quit_on_open then
view.close()
api.close()
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 = require("nvim-tree").close
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
2 changes: 1 addition & 1 deletion lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function M.open(cwd)
core.init(cwd or vim.loop.cwd())
end
if should_hijack_current_buf() then
view.close()
api.close()
view.open_in_current_win()
renderer.draw()
else
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ local function remove_overlay()
group = a.nvim_create_augroup("NvimTree", { clear = false }),
callback = function()
if utils.is_nvim_tree_buf(0) then
view.close()
a.close()
end
end,
})
Expand Down
11 changes: 8 additions & 3 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ local function save_tab_state()
M.View.cursors[tabpage] = a.nvim_win_get_cursor(M.get_winnr())
end

function M.close()
function M.close(all_tabpages)
if not M.is_visible() then
return
end
Expand All @@ -200,8 +200,13 @@ function M.close()
if tree_win == current_win and prev_win > 0 then
a.nvim_set_current_win(vim.fn.win_getid(prev_win))
end
if a.nvim_win_is_valid(tree_win) then
a.nvim_win_close(tree_win, true)
a.nvim_win_close(tree_win, true)
if all_tabpages then
for _, v in pairs(M.View.tabpages) do
if v.winnr and a.nvim_win_is_valid(v.winnr) then
a.nvim_win_close(v.winnr, true)
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is failing when the tree is not open in the current tab:

    tab = {
      sync = {
        open = false,
        close = false,
        ignore = {},
      },
    },

Fail:

:NvimTreeOpen
:tabnew
:lua require"nvim-tree.api".tree.close_in_all_tabs()

Pass:

:NvimTreeOpen
:tabnew
:NvimTreeOpen
:lua require"nvim-tree.api".tree.close_in_all_tabs()

It looks like we will need to move the all-tabs-closing bit out of the nvim_list_wins loop.

events._dispatch_on_tree_close()
return
Expand Down