11local log = require (" nvim-tree.log" )
2+ local view = require (" nvim-tree.view" )
23local utils = require (" nvim-tree.utils" )
34local actions = require (" nvim-tree.actions" )
45local core = require (" nvim-tree.core" )
@@ -73,8 +74,7 @@ function M.change_root(path, bufnr)
7374end
7475
7576function M .tab_enter ()
76- local explorer = core .get_explorer ()
77- if explorer and explorer .view :is_visible ({ any_tabpage = true }) then
77+ if view .is_visible ({ any_tabpage = true }) then
7878 local bufname = vim .api .nvim_buf_get_name (0 )
7979
8080 local ft
@@ -89,15 +89,17 @@ function M.tab_enter()
8989 return
9090 end
9191 end
92- explorer . view : open ({ focus_tree = false })
92+ view . open ({ focus_tree = false })
9393
94- explorer .renderer :draw ()
94+ local explorer = core .get_explorer ()
95+ if explorer then
96+ explorer .renderer :draw ()
97+ end
9598 end
9699end
97100
98101function M .open_on_directory ()
99- local explorer = core .get_explorer ()
100- local should_proceed = _config .hijack_directories .auto_open or explorer and explorer .view :is_visible ()
102+ local should_proceed = _config .hijack_directories .auto_open or view .is_visible ()
101103 if not should_proceed then
102104 return
103105 end
@@ -148,6 +150,21 @@ local function setup_autocommands(opts)
148150 vim .api .nvim_create_autocmd (name , vim .tbl_extend (" force" , default_opts , custom_opts ))
149151 end
150152
153+ -- prevent new opened file from opening in the same window as nvim-tree
154+ create_nvim_tree_autocmd (" BufWipeout" , {
155+ pattern = " NvimTree_*" ,
156+ callback = function ()
157+ if not utils .is_nvim_tree_buf (0 ) then
158+ return
159+ end
160+ if opts .actions .open_file .eject then
161+ view ._prevent_buffer_override ()
162+ else
163+ view .abandon_current_window ()
164+ end
165+ end ,
166+ })
167+
151168 if opts .tab .sync .open then
152169 create_nvim_tree_autocmd (" TabEnter" , { callback = vim .schedule_wrap (M .tab_enter ) })
153170 end
@@ -209,6 +226,17 @@ local function setup_autocommands(opts)
209226 })
210227 end
211228
229+ if opts .view .float .enable and opts .view .float .quit_on_focus_loss then
230+ create_nvim_tree_autocmd (" WinLeave" , {
231+ pattern = " NvimTree_*" ,
232+ callback = function ()
233+ if utils .is_nvim_tree_buf (0 ) then
234+ view .close ()
235+ end
236+ end ,
237+ })
238+ end
239+
212240 -- Handles event dispatch when tree is closed by `:q`
213241 create_nvim_tree_autocmd (" WinClosed" , {
214242 pattern = " *" ,
@@ -486,7 +514,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
486514 },
487515 },
488516 experimental = {
489- multi_instance = false ,
490517 },
491518 log = {
492519 enable = false ,
@@ -666,10 +693,10 @@ local function localise_default_opts()
666693end
667694
668695function M .purge_all_state ()
696+ view .close_all_tabs ()
697+ view .abandon_all_windows ()
669698 local explorer = core .get_explorer ()
670699 if explorer then
671- explorer .view :close_all_tabs ()
672- explorer .view :abandon_all_windows (" purge_all_state" )
673700 require (" nvim-tree.git" ).purge_state ()
674701 explorer :destroy ()
675702 core .reset_explorer ()
@@ -722,12 +749,12 @@ function M.setup(conf)
722749 require (" nvim-tree.explorer.watch" ).setup (opts )
723750 require (" nvim-tree.git" ).setup (opts )
724751 require (" nvim-tree.git.utils" ).setup (opts )
752+ require (" nvim-tree.view" ).setup (opts )
725753 require (" nvim-tree.lib" ).setup (opts )
726754 require (" nvim-tree.renderer.components" ).setup (opts )
727755 require (" nvim-tree.buffers" ).setup (opts )
728756 require (" nvim-tree.help" ).setup (opts )
729757 require (" nvim-tree.watcher" ).setup (opts )
730- require (" nvim-tree.multi-instance-debug" ).setup (opts )
731758
732759 setup_autocommands (opts )
733760
0 commit comments