Skip to content

Commit 79434c2

Browse files
committed
feat(tab_change): introduce new option to filter buffer by bufname or ft
Also fixes changing tab by deferring the call on tab enter. New option `ignore_buf_on_tab_change` to avoid opening for some tabs. Some example could be neogit, vim fugitive, man pages ...
1 parent 1e3c578 commit 79434c2

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

doc/nvim-tree-lua.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Subsequent calls to setup will replace the previous configuration.
167167
open_on_setup = false,
168168
open_on_setup_file = false,
169169
open_on_tab = false,
170+
ignore_buf_on_tab_change = {},
170171
sort_by = "name",
171172
root_dirs = {},
172173
prefer_startup_root = false,
@@ -364,11 +365,15 @@ Will ignore the buffer, when deciding to open the tree on setup.
364365
Type: `boolean`, Default: `false`
365366

366367
*nvim-tree.ignore_ft_on_setup*
367-
List of filetypes that will make `open_on_setup` not open.
368+
List of filetypes that will prevent `open_on_setup` to open.
368369
You can use this option if you don't want the tree to open
369370
in some scenarios (eg using vim startify).
370371
Type: {string}, Default: `{}`
371372

373+
*nvim-tree.ignore_buf_on_tab_change*
374+
List of filetypes or buffer names that will prevent `open_on_tab` to open.
375+
Type: {string}, Default: `{}`
376+
372377
*nvim-tree.auto_reload_on_write*
373378
Reloads the explorer every time a buffer is written to.
374379
Type: `boolean`, Default: `true`

lua/nvim-tree.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ end
122122
function M.tab_change()
123123
if view.is_visible { any_tabpage = true } then
124124
local bufname = api.nvim_buf_get_name(0)
125-
if bufname:match "Neogit" ~= nil or bufname:match "--graph" ~= nil then
126-
return
125+
local ft = api.nvim_buf_get_option(0, "ft")
126+
for _, filter in ipairs(M.config.ignore_buf_on_tab_change) do
127+
if bufname:match(filter) ~= nil or ft:match(filter) ~= nil then
128+
return
129+
end
127130
end
128131
view.open { focus_tree = false }
129132
require("nvim-tree.renderer").draw()
@@ -350,7 +353,7 @@ local function setup_autocommands(opts)
350353
end
351354

352355
if opts.open_on_tab then
353-
create_nvim_tree_autocmd("TabEnter", { callback = M.tab_change })
356+
create_nvim_tree_autocmd("TabEnter", { callback = vim.schedule_wrap(M.tab_change) })
354357
end
355358
if opts.hijack_cursor then
356359
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
@@ -424,6 +427,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
424427
open_on_setup = false,
425428
open_on_setup_file = false,
426429
open_on_tab = false,
430+
ignore_buf_on_tab_change = {},
427431
sort_by = "name",
428432
root_dirs = {},
429433
prefer_startup_root = false,
@@ -665,6 +669,7 @@ function M.setup(conf)
665669
_config.open_on_setup_file = opts.open_on_setup_file
666670
_config.ignore_buffer_on_setup = opts.ignore_buffer_on_setup
667671
_config.ignore_ft_on_setup = opts.ignore_ft_on_setup
672+
_config.ignore_buf_on_tab_change = opts.ignore_buf_on_tab_change
668673
_config.hijack_directories = opts.hijack_directories
669674
_config.hijack_directories.enable = _config.hijack_directories.enable and netrw_disabled
670675

0 commit comments

Comments
 (0)