feat(tabs): add tab.sync options (#1698)

* Sync closing of nvim-tree across tabs

* chore: remove vim.* "requires"

* Sync closing of nvim-tree across tabs

* Fix api.close calls

* Fix issue from merge

* Implement changes

* Finish todos and add close_all_tabs

* silently refactor options, add doc

* fix vinegar example

* Refactor close to work with tabid

* Close nvim tree if last buffer

* close and abandon all tabs on subsequent setup calls

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Wessel Blokzijl
2022-11-19 03:57:45 +01:00
committed by GitHub
parent 1837751efb
commit c49499413a
7 changed files with 120 additions and 43 deletions

View File

@@ -18,6 +18,8 @@ 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_in_this_tab = require("nvim-tree.view").close_this_tab_only
Api.tree.close_in_all_tabs = require("nvim-tree.view").close_all_tabs
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

View File

@@ -289,6 +289,11 @@ local function refactored(opts)
-- 2022/06/20
utils.move_missing_val(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root")
utils.move_missing_val(opts, "", "update_cwd", opts, "", "sync_root_with_cwd")
-- 2022/11/07
utils.move_missing_val(opts, "", "open_on_tab", opts, "tab.sync", "open", false)
utils.move_missing_val(opts, "", "open_on_tab", opts, "tab.sync", "close")
utils.move_missing_val(opts, "", "ignore_buf_on_tab_change", opts, "tab.sync", "ignore")
end
local function removed(opts)

View File

@@ -117,7 +117,7 @@ function M.open(cwd)
core.init(cwd or vim.loop.cwd())
end
if should_hijack_current_buf() then
view.close()
view.close_this_tab_only()
view.open_in_current_win()
renderer.draw()
else

View File

@@ -1,4 +1,5 @@
local Iterator = require "nvim-tree.iterators.node-iterator"
local notify = require "nvim-tree.notify"
local M = {
debouncers = {},
@@ -266,14 +267,20 @@ function M.table_create_missing(tbl, path)
return t
end
-- Move a value from src to dst if value is nil on dst
-- @param src to copy from
-- @param src_path dot separated string of sub-tables
-- @param src_pos value pos
-- @param dst to copy to
-- @param dst_path dot separated string of sub-tables, created when missing
-- @param dst_pos value pos
function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos)
--- Move a value from src to dst if value is nil on dst.
--- Remove value from src
--- @param src table to copy from
--- @param src_path string dot separated string of sub-tables
--- @param src_pos string value pos
--- @param dst table to copy to
--- @param dst_path string dot separated string of sub-tables, created when missing
--- @param dst_pos string value pos
--- @param remove boolean default true
function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos, remove)
if remove == nil then
remove = true
end
local ok, err = pcall(vim.validate, {
src = { src, "table" },
src_path = { src_path, "string" },
@@ -281,9 +288,11 @@ function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos)
dst = { dst, "table" },
dst_path = { dst_path, "string" },
dst_pos = { dst_pos, "string" },
remove = { remove, "boolean" },
})
if not ok then
M.notify.warn("move_missing_val: " .. (err or "invalid arguments"))
notify.warn("move_missing_val: " .. (err or "invalid arguments"))
return
end
for pos in string.gmatch(src_path, "([^%.]+)%.*") do
@@ -304,7 +313,9 @@ function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos)
dst[dst_pos] = src_val
end
src[src_pos] = nil
if remove then
src[src_pos] = nil
end
end
function M.format_bytes(bytes)

View File

@@ -179,21 +179,21 @@ local function switch_buf_if_last_buf()
end
-- save_tab_state saves any state that should be preserved across redraws.
local function save_tab_state()
local tabpage = vim.api.nvim_get_current_tabpage()
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr())
local function save_tab_state(tabnr)
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage))
end
function M.close()
if not M.is_visible() then
local function close(tabpage)
if not M.is_visible { tabpage = tabpage } then
return
end
save_tab_state()
save_tab_state(tabpage)
switch_buf_if_last_buf()
local tree_win = M.get_winnr()
local tree_win = M.get_winnr(tabpage)
local current_win = vim.api.nvim_get_current_win()
for _, win in pairs(vim.api.nvim_list_wins()) do
if tree_win ~= win and vim.api.nvim_win_get_config(win).relative == "" then
for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
if vim.api.nvim_win_get_config(win).relative == "" then
local prev_win = vim.fn.winnr "#" -- this tab only
if tree_win == current_win and prev_win > 0 then
vim.api.nvim_set_current_win(vim.fn.win_getid(prev_win))
@@ -207,6 +207,24 @@ function M.close()
end
end
function M.close_this_tab_only()
close(vim.api.nvim_get_current_tabpage())
end
function M.close_all_tabs()
for tabpage, _ in pairs(M.View.tabpages) do
close(tabpage)
end
end
function M.close()
if M.View.tab.sync.close then
M.close_all_tabs()
else
M.close_this_tab_only()
end
end
function M.open(options)
if M.is_visible() then
return
@@ -308,10 +326,29 @@ end
function M.abandon_current_window()
local tab = vim.api.nvim_get_current_tabpage()
BUFNR_PER_TAB[tab] = nil
M.View.tabpages[tab].winnr = nil
if M.View.tabpages[tab] then
M.View.tabpages[tab].winnr = nil
end
end
function M.abandon_all_windows()
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
BUFNR_PER_TAB[tab] = nil
if M.View.tabpages[tab] then
M.View.tabpages[tab].winnr = nil
end
end
end
function M.is_visible(opts)
if opts and opts.tabpage then
if M.View.tabpages[opts.tabpage] == nil then
return false
end
local winnr = M.View.tabpages[opts.tabpage].winnr
return winnr and vim.api.nvim_win_is_valid(winnr)
end
if opts and opts.any_tabpage then
for _, v in pairs(M.View.tabpages) do
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
@@ -450,6 +487,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.tab = opts.tab
M.View.preserve_window_proportions = options.preserve_window_proportions
M.View.winopts.number = options.number
M.View.winopts.relativenumber = options.relativenumber