BREAKING_CHANGE: nvim-tree buffer was renamed

You should now use `NvimTree_*` when matching with an autocmd
There is now 1 buffer per tabpage.
This commit is contained in:
kiyan 2022-03-01 20:58:04 +01:00
parent f37c3ffa59
commit 0e7856fd8d
3 changed files with 23 additions and 13 deletions

View File

@ -60,7 +60,7 @@ Print clipboard content for both cut and copy
|:NvimTreeResize| *:NvimTreeResize* |: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. resizes the window to the width of 50.
============================================================================== ==============================================================================

View File

@ -305,7 +305,7 @@ local function setup_autocommands(opts)
vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()" vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()"
end end
if opts.hijack_cursor then 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 end
if opts.update_cwd then if opts.update_cwd then
vim.cmd "au DirChanged * lua require'nvim-tree'.change_dir(vim.loop.cwd())" vim.cmd "au DirChanged * lua require'nvim-tree'.change_dir(vim.loop.cwd())"
@ -315,9 +315,9 @@ local function setup_autocommands(opts)
end end
if not opts.actions.open_file.quit_on_open then 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 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 end
if opts.hijack_directories.enable then if opts.hijack_directories.enable then

View File

@ -33,7 +33,7 @@ M.View = {
}, },
} }
local BUFNR = nil local BUFNR_PER_TAB = {}
local LAST_FOCUSED_WIN = nil local LAST_FOCUSED_WIN = nil
local BUFFER_OPTIONS = { local BUFFER_OPTIONS = {
swapfile = false, swapfile = false,
@ -44,24 +44,34 @@ local BUFFER_OPTIONS = {
buflisted = false, 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() local function wipe_rogue_buffer()
for _, bufnr in ipairs(a.nvim_list_bufs()) do 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 }) return pcall(a.nvim_buf_delete, bufnr, { force = true })
end end
end end
end end
local function create_buffer(bufnr) 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() 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 for option, value in pairs(BUFFER_OPTIONS) do
vim.bo[BUFNR][option] = value vim.bo[M.get_bufnr()][option] = value
end end
require'nvim-tree.actions'.apply_mappings(BUFNR) require'nvim-tree.actions'.apply_mappings(M.get_bufnr())
end end
local function get_size() local function get_size()
@ -106,7 +116,7 @@ local function open_window()
end end
local function set_window_options_and_buffer() 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 for k, v in pairs(M.View.winopts) do
set_local(k, v) set_local(k, v)
end end
@ -210,7 +220,7 @@ end
function M.abandon_current_window() function M.abandon_current_window()
local tab = a.nvim_get_current_tabpage() local tab = a.nvim_get_current_tabpage()
BUFNR = nil BUFNR_PER_TAB[tab] = nil
M.View.tabpages[tab] = { winnr = nil } M.View.tabpages[tab] = { winnr = nil }
end end
@ -267,7 +277,7 @@ end
--- Returns the current nvim tree bufnr --- Returns the current nvim tree bufnr
---@return number ---@return number
function M.get_bufnr() function M.get_bufnr()
return BUFNR return BUFNR_PER_TAB[a.nvim_get_current_tabpage()]
end end
--- Checks if nvim-tree is displaying the help ui within the tabpage specified --- Checks if nvim-tree is displaying the help ui within the tabpage specified