-
-
Notifications
You must be signed in to change notification settings - Fork 612
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
Sync closing of nvim-tree across tabs #1698
Sync closing of nvim-tree across tabs #1698
Conversation
Continuation of #1662, which had the following review comments:
|
I think I'm missing something fundamental here. Pressing nvim -nu nvt-dev.lua It looks like |
lua/nvim-tree/actions/dispatch.lua
Outdated
local view = require "nvim-tree.view" | ||
local lib = require "nvim-tree.lib" | ||
|
||
local M = {} | ||
|
||
local Actions = { | ||
close = view.close, | ||
close = api.close, |
There was a problem hiding this comment.
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
First of all, thanks for looking at this!
This is something i broke accidentally, and i think i need some advice on how not to do that. I'll explain my goal with this PR in more detail. Nvim-tree implements the In order to only trigger the new behaviour if This also means I replaced all other instances of |
It looks like we have some inconsistency around :; ag --nogroup "local a .*vim.api"
lua/nvim-tree/actions/fs/remove-file.lua:1:local a = vim.api
lua/nvim-tree/actions/init.lua:3:local a = vim.api
lua/nvim-tree/actions/node/file-popup.lua:2:local a = vim.api
lua/nvim-tree/actions/fs/trash.lua:1:local a = vim.api
lua/nvim-tree/actions/root/change-dir.lua:1:local a = vim.api
lua/nvim-tree/diagnostics.lua:1:local a = vim.api
lua/nvim-tree/live-filter.lua:1:local a = vim.api
lua/nvim-tree/utils.lua:3:local a = vim.api
lua/nvim-tree/view.lua:1:local a = vim.api
:; ag --nogroup "local api.*vim.api"
lua/nvim-tree/actions/finders/search-node.lua:1:local api = vim.api
lua/nvim-tree/actions/node/open-file.lua:2:local api = vim.api
lua/nvim-tree/lib.lua:1:local api = vim.api
lua/nvim-tree/renderer/components/full-name.lua:3:local api = vim.api
lua/nvim-tree/renderer/init.lua:15:local api = vim.api
lua/nvim-tree.lua:2:local api = vim.api
lua/nvim-tree/actions/dispatch.lua:1:local api = vim.api
lua/nvim-tree/colors.lua:1:local api = vim.api
Edit: let's just explicitly invoke |
PR to remove the
I'm going to soak that for a few days of regular usage before merging it. That should give you a good base to work from. You could rebase to that branch, merge it or create a new branch from it. |
That is the right course of action. It is implicitly consistent with |
Will this be an issue when not using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- use
chore-remove-vim-quote-requires-quote
branch - test behaviour unchanged when not
open_on_tab
- add to
open_on_tab
help (I'll make that change later)
|
Im in doubt about what to do with the following usaged of the old
|
Created dummy PR #1704 to aid in reviewing against the unmerged branch. |
I see. That is a problem. Summary, please correct me:
|
Idea:
Refactor view to something like: local function close(all_tabpages)
---
end
function M.close_this_tab_only()
close(false)
end
function M.close()
close(M.View.open_on_tab)
end API remains unchanged. |
Let's build it and experiment. We can change these to call |
I Implemented this. I also did some experimentation and found the following regarding the usages of close:
|
I also think |
That is a good idea. I'll make/document that change as a silent refactor. |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is working nicely.
- add
close_all_tabs
- fix lua/nvim-tree/actions/dispatch.lua:7 to use function pointer, test
q
- remove TODOs
- rename or split option
- silent option migration
- doc
I'll make my changes after yours.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
if view.is_visible { any_tabpage = true } then | ||
local bufname = vim.api.nvim_buf_get_name(0) | ||
local ft = vim.api.nvim_buf_get_option(0, "ft") | ||
for _, filter in ipairs(M.config.ignore_buf_on_tab_change) do | ||
for _, filter in ipairs(M.config.tab.sync.ignore) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work very well, however that is for another day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added doc and new silently migrated options:
tab = {
sync = {
open = false,
close = false,
ignore = {},
},
},
Please:
- test migration of
open_on_tab
totab.sync.open
- test migration of
ignore_buf_on_tab_change
totab.sync.ignore
- test all of the changes from the table (thanks for putting that together, it was very useful)
- fix
close_in_all_tabs
failure when no sync
lua/nvim-tree/view.lua
Outdated
vim.api.nvim_win_close(v.winnr, true) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
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.
We could also enhance the |
This falls in the same category as local function tab_win_closed(winnr)
local tabnr = vim.api.nvim_win_get_tabpage(winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr)
local buf_info = vim.fn.getbufinfo(bufnr)[1]
local tab_wins = vim.tbl_filter(function(w) return w~=winnr end, vim.api.nvim_tabpage_list_wins(tabnr))
local tab_bufs = vim.tbl_map(vim.api.nvim_win_get_buf, tab_wins)
if buf_info.name:match(".*NvimTree_%d*$") then -- close buffer was nvim tree
-- Close all nvim tree on :q
if not vim.tbl_isempty(tab_bufs) then -- and was not the last window (not closed automatically by code below)
api.tree.close()
end
else -- else closed buffer was normal buffer
if #tab_bufs == 1 then -- if there is only 1 buffer left in the tab
local last_buf_info = vim.fn.getbufinfo(tab_bufs[1])[1]
if last_buf_info.name:match(".*NvimTree_%d*$") then -- and that buffer is nvim tree
vim.schedule(function ()
if #vim.api.nvim_list_wins() == 1 then -- if its the last buffer in vim
vim.cmd "quit" -- then close all of vim
else -- else there are more tabs open
vim.api.nvim_win_close(tab_wins[1], true) -- then close only the tab
end
end)
end
end
end
end
vim.api.nvim_create_autocmd("WinClosed", {
callback = function ()
local winnr = tonumber(vim.fn.expand("<amatch>"))
vim.schedule_wrap(tab_win_closed(winnr))
end,
nested = true
}) Perhaps it could go in the tips and tricks section. |
I cant figure out how to properly test the last 2. I did notice that when
I looked into the way i integrated close all tabs into |
Yes. There's a reference to #1115 in there but that's not very explicit. There are several solutions people have used for this.
Edit: wiki page I'd be grateful if you could add your solution. |
This is for the case of the user invoking setup a second time and should remove the tree views/windows. This is actually currently broken, as only the view/window in the current tab is removed. Added Please test this one. You can do it using the minimal config template and Edit: see wiki development |
This one is simply the close action Working nicely for me when |
I'm happy to let that one go, however if you'd like to put together a fix I would be most grateful. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested OK
- test this for a week's regular usage
- review and test Sync closing of nvim-tree across tabs #1698 (comment) ded6a85
- optionally fix
hijack_unnamed_buffer_when_opening
- optionally add auto close solution to https://github.com/nvim-tree/nvim-tree.lua/wiki/Auto-Close
Will do.
It works (with and without sync). minimal configvim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvt-min/site]]
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
require("packer").startup {
{
"wbthomason/packer.nvim",
{"rwblokzijl/nvim-tree.lua", branch="sync-close-through-tabs"},
"nvim-tree/nvim-web-devicons",
-- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
},
config = {
package_root = package_root,
compile_path = install_path .. "/plugin/packer_compiled.lua",
display = { non_interactive = true },
},
}
end
if vim.fn.isdirectory(install_path) == 0 then
print "Installing nvim-tree and dependencies."
vim.fn.system { "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }
end
load_plugins()
require("packer").sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]]
vim.opt.termguicolors = true
vim.opt.cursorline = true
-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
require("nvim-tree").setup {
tab = {
sync = {
open = true,
close = true,
ignore = {},
}
},
}
end
I'll skip this for now. I might open a PR for this at some point.
|
Many thanks for all your work @rwblokzijl |
Many thanks from me too! @rwblokzijl So far this works great when I've tried it |
Resolves #1493
This PR syncs nvim tree across tabs if
open_on_tab
is set.Previously:
Now:
:quit
) this is also synced across tabs