From 0e7856fd8de18edaaee8fa0c2f2c9432c973aef5 Mon Sep 17 00:00:00 2001 From: kiyan Date: Tue, 1 Mar 2022 20:58:04 +0100 Subject: [PATCH] BREAKING_CHANGE: nvim-tree buffer was renamed You should now use `NvimTree_*` when matching with an autocmd There is now 1 buffer per tabpage. --- doc/nvim-tree-lua.txt | 2 +- lua/nvim-tree.lua | 6 +++--- lua/nvim-tree/view.lua | 28 +++++++++++++++++++--------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 1f5a2b77..a0e2fe3b 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -60,7 +60,7 @@ Print clipboard content for both cut and copy |:NvimTreeResize| *:NvimTreeResize* -Resize the NvimTree window to the given size. Example: `:NvimTreeresize 50` +Resize the NvimTree window to the given size. Example: `:NvimTreeResize 50` resizes the window to the width of 50. ============================================================================== diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 00779b79..283d0c6d 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -305,7 +305,7 @@ local function setup_autocommands(opts) vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()" end if opts.hijack_cursor then - vim.cmd "au CursorMoved NvimTree lua require'nvim-tree'.place_cursor_on_node()" + vim.cmd "au CursorMoved NvimTree_* lua require'nvim-tree'.place_cursor_on_node()" end if opts.update_cwd then vim.cmd "au DirChanged * lua require'nvim-tree'.change_dir(vim.loop.cwd())" @@ -315,9 +315,9 @@ local function setup_autocommands(opts) end if not opts.actions.open_file.quit_on_open then - vim.cmd "au BufWipeout NvimTree lua require'nvim-tree.view'._prevent_buffer_override()" + vim.cmd "au BufWipeout NvimTree_* lua require'nvim-tree.view'._prevent_buffer_override()" else - vim.cmd "au BufWipeout NvimTree lua require'nvim-tree.view'.abandon_current_window()" + vim.cmd "au BufWipeout NvimTree_* lua require'nvim-tree.view'.abandon_current_window()" end if opts.hijack_directories.enable then diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 872e0b89..bd0f20b3 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -33,7 +33,7 @@ M.View = { }, } -local BUFNR = nil +local BUFNR_PER_TAB = {} local LAST_FOCUSED_WIN = nil local BUFFER_OPTIONS = { swapfile = false, @@ -44,24 +44,34 @@ local BUFFER_OPTIONS = { buflisted = false, } +local function matches_bufnr(bufnr) + for _, b in pairs(BUFNR_PER_TAB) do + if b == bufnr then + return true + end + end + return false +end + local function wipe_rogue_buffer() for _, bufnr in ipairs(a.nvim_list_bufs()) do - if bufnr ~= BUFNR and a.nvim_buf_get_name(bufnr):match("NvimTree") ~= nil then + if not matches_bufnr(bufnr) and a.nvim_buf_get_name(bufnr):match("NvimTree") ~= nil then return pcall(a.nvim_buf_delete, bufnr, { force = true }) end end end local function create_buffer(bufnr) - BUFNR = bufnr or a.nvim_create_buf(false, false) + local tab = a.nvim_get_current_tabpage() + BUFNR_PER_TAB[tab] = bufnr or a.nvim_create_buf(false, false) wipe_rogue_buffer() - a.nvim_buf_set_name(BUFNR, 'NvimTree') + a.nvim_buf_set_name(M.get_bufnr(), 'NvimTree_'..tab) for option, value in pairs(BUFFER_OPTIONS) do - vim.bo[BUFNR][option] = value + vim.bo[M.get_bufnr()][option] = value end - require'nvim-tree.actions'.apply_mappings(BUFNR) + require'nvim-tree.actions'.apply_mappings(M.get_bufnr()) end local function get_size() @@ -106,7 +116,7 @@ local function open_window() end local function set_window_options_and_buffer() - pcall(vim.cmd, "buffer "..BUFNR) + pcall(vim.cmd, "buffer "..M.get_bufnr()) for k, v in pairs(M.View.winopts) do set_local(k, v) end @@ -210,7 +220,7 @@ end function M.abandon_current_window() local tab = a.nvim_get_current_tabpage() - BUFNR = nil + BUFNR_PER_TAB[tab] = nil M.View.tabpages[tab] = { winnr = nil } end @@ -267,7 +277,7 @@ end --- Returns the current nvim tree bufnr ---@return number function M.get_bufnr() - return BUFNR + return BUFNR_PER_TAB[a.nvim_get_current_tabpage()] end --- Checks if nvim-tree is displaying the help ui within the tabpage specified