22local lib = require (" nvim-tree.lib" )
33local notify = require (" nvim-tree.notify" )
44local utils = require (" nvim-tree.utils" )
5- local view = require (" nvim-tree.view " )
5+ local core = require (" nvim-tree.core " )
66
77local M = {}
88
1919--- Get all windows in the current tabpage that aren't NvimTree.
2020--- @return table with valid win_ids
2121local function usable_win_ids ()
22+ local explorer = core .get_explorer ()
2223 local tabpage = vim .api .nvim_get_current_tabpage ()
2324 local win_ids = vim .api .nvim_tabpage_list_wins (tabpage )
24- local tree_winid = view . View :get_winnr (tabpage )
25+ local tree_winid = explorer and explorer . view :get_winnr (tabpage )
2526
2627 return vim .tbl_filter (function (id )
2728 local bufid = vim .api .nvim_win_get_buf (id )
187188
188189local function open_file_in_tab (filename )
189190 if M .quit_on_open then
190- view .View :close ()
191+ local explorer = core .get_explorer ()
192+ if explorer then
193+ explorer .view :close ()
194+ end
191195 end
192196 if M .relative_path then
193197 filename = utils .path_relative (filename , vim .fn .getcwd ())
197201
198202local function drop (filename )
199203 if M .quit_on_open then
200- view .View :close ()
204+ local explorer = core .get_explorer ()
205+ if explorer then
206+ explorer .view :close ()
207+ end
201208 end
202209 if M .relative_path then
203210 filename = utils .path_relative (filename , vim .fn .getcwd ())
207214
208215local function tab_drop (filename )
209216 if M .quit_on_open then
210- view .View :close ()
217+ local explorer = core .get_explorer ()
218+ if explorer then
219+ explorer .view :close ()
220+ end
211221 end
212222 if M .relative_path then
213223 filename = utils .path_relative (filename , vim .fn .getcwd ())
@@ -228,7 +238,10 @@ local function on_preview(buf_loaded)
228238 once = true ,
229239 })
230240 end
231- view .View :focus ()
241+ local explorer = core .get_explorer ()
242+ if explorer then
243+ explorer .view :focus ()
244+ end
232245end
233246
234247local function get_target_winid (mode )
@@ -273,6 +286,8 @@ local function set_current_win_no_autocmd(winid, autocmd)
273286end
274287
275288local function open_in_new_window (filename , mode )
289+ local explorer = core .get_explorer ()
290+
276291 if type (mode ) ~= " string" then
277292 mode = " "
278293 end
@@ -295,7 +310,11 @@ local function open_in_new_window(filename, mode)
295310 end , vim .api .nvim_list_wins ())
296311
297312 local create_new_window = # win_ids == 1 -- This implies that the nvim-tree window is the only one
298- local new_window_side = (view .View .side == " right" ) and " aboveleft" or " belowright"
313+
314+ local new_window_side = " belowright"
315+ if explorer and (explorer .view .side == " right" ) then
316+ new_window_side = " aboveleft"
317+ end
299318
300319 -- Target is invalid: create new window
301320 if not vim .tbl_contains (win_ids , target_winid ) then
@@ -327,7 +346,7 @@ local function open_in_new_window(filename, mode)
327346 end
328347 end
329348
330- if (mode == " preview" or mode == " preview_no_picker" ) and view . View .float .enable then
349+ if (mode == " preview" or mode == " preview_no_picker" ) and explorer and explorer . view .float .enable then
331350 -- ignore "WinLeave" autocmd on preview
332351 -- because the registered "WinLeave"
333352 -- will kill the floating window immediately
@@ -367,7 +386,12 @@ local function is_already_loaded(filename)
367386end
368387
369388local function edit_in_current_buf (filename )
370- require (" nvim-tree.view" ).View :abandon_current_window ()
389+ local explorer = core .get_explorer ()
390+
391+ if explorer then
392+ explorer .view :abandon_current_window ()
393+ end
394+
371395 if M .relative_path then
372396 filename = utils .path_relative (filename , vim .fn .getcwd ())
373397 end
378402--- @param filename string
379403--- @return nil
380404function M .fn (mode , filename )
405+ local explorer = core .get_explorer ()
406+
381407 if type (mode ) ~= " string" then
382408 mode = " "
383409 end
@@ -412,16 +438,16 @@ function M.fn(mode, filename)
412438 vim .bo .bufhidden = " "
413439 end
414440
415- if M .resize_window then
416- view . View :resize ()
441+ if M .resize_window and explorer then
442+ explorer . view :resize ()
417443 end
418444
419445 if mode == " preview" or mode == " preview_no_picker" then
420446 return on_preview (buf_loaded )
421447 end
422448
423- if M .quit_on_open then
424- view . View :close ()
449+ if M .quit_on_open and explorer then
450+ explorer . view :close ()
425451 end
426452end
427453
0 commit comments