@@ -143,6 +143,19 @@ M.focus = function(source_name, close_others, toggle_if_open)
143143 manager .focus (source_name )
144144end
145145
146+ M .hijack_netrw = function ()
147+ local bufname = vim .api .nvim_buf_get_name (0 )
148+ local stats = vim .loop .fs_stat (bufname )
149+ local is_dir = stats and stats .type == " directory"
150+ if is_dir then
151+ vim .cmd (" bwipeout!" )
152+ manager .navigate (" filesystem" , bufname )
153+ return true
154+ else
155+ return false
156+ end
157+ end
158+
146159M .reveal_current_file = function (source_name , toggle_if_open )
147160 source_name = check_source (source_name )
148161 if toggle_if_open then
@@ -201,6 +214,72 @@ M.paste_default_config = function()
201214 end )
202215end
203216
217+ M .buffer_enter_event = function (args )
218+ if utils .is_floating () then
219+ return
220+ end
221+ -- if vim is trying to open a dir, then we hijack it
222+ if M .hijack_netrw () then
223+ return
224+ end
225+ -- if it is a neo-tree window, just set local options
226+ if vim .bo .filetype == " neo-tree" then
227+ vim .cmd ([[
228+ setlocal cursorline
229+ setlocal nowrap
230+ setlocal winhighlight=Normal:NeoTreeNormal,NormalNC:NeoTreeNormalNC,CursorLine:NeoTreeCursorLine,FloatBorder:NeoTreeFloatBorder
231+ setlocal nolist nospell nonumber norelativenumber
232+ ]] )
233+ return
234+ end
235+ if vim .bo .filetype == " neo-tree-popup" then
236+ vim .cmd ([[
237+ setlocal winhighlight=Normal:NeoTreeNormal,FloatBorder:NeoTreeFloatBorder
238+ setlocal nolist nospell nonumber norelativenumber
239+ ]] )
240+ return
241+ end
242+
243+ -- For all others, make sure another buffer is not hijacking our window
244+ local prior_buf = vim .fn .bufnr (" #" )
245+ if prior_buf < 1 then
246+ return
247+ end
248+ local prior_type = vim .api .nvim_buf_get_option (prior_buf , " filetype" )
249+ if prior_type == " neo-tree" then
250+ local current_tabnr = vim .api .nvim_get_current_tabpage ()
251+ local neo_tree_tabnr = vim .api .nvim_buf_get_var (prior_buf , " neo_tree_tabnr" )
252+ if neo_tree_tabnr ~= current_tabnr then
253+ -- This a new tab, so the alternate being neo-tree doesn't matter.
254+ return
255+ end
256+ local neo_tree_winid = vim .api .nvim_buf_get_var (prior_buf , " neo_tree_winid" )
257+ local current_winid = vim .api .nvim_get_current_win ()
258+ if neo_tree_winid ~= current_winid then
259+ -- This is not the neo-tree window, so the alternate being neo-tree doesn't matter.
260+ return
261+ end
262+
263+ local bufname = vim .api .nvim_buf_get_name (0 )
264+ log .debug (" redirecting buffer " .. bufname .. " to new split" )
265+ vim .cmd (" b#" )
266+ -- Using schedule at this point fixes problem with syntax
267+ -- highlighting in the buffer. I also prevents errors with diagnostics
268+ -- trying to work with gthe buffer as it's being closed.
269+ vim .schedule (function ()
270+ -- try to delete the buffer, only because if it was new it would take
271+ -- on options from the neo-tree window that are undesirable.
272+ pcall (vim .cmd , " bdelete " .. bufname )
273+ local fake_state = {
274+ window = {
275+ position = " left" ,
276+ },
277+ }
278+ utils .open_file (fake_state , bufname )
279+ end )
280+ end
281+ end
282+
204283M .win_enter_event = function ()
205284 local win_id = vim .api .nvim_get_current_win ()
206285 if utils .is_floating (win_id ) then
@@ -272,48 +351,7 @@ M.setup = function(config)
272351 -- Prevent accidentally opening another file in the neo-tree window.
273352 events .subscribe ({
274353 event = events .VIM_BUFFER_ENTER ,
275- handler = function (args )
276- if utils .is_floating () then
277- return
278- end
279- local prior_buf = vim .fn .bufnr (" #" )
280- if prior_buf < 1 then
281- return
282- end
283- local prior_type = vim .api .nvim_buf_get_option (prior_buf , " filetype" )
284- if prior_type == " neo-tree" and vim .bo .filetype ~= " neo-tree" then
285- local current_tabnr = vim .api .nvim_get_current_tabpage ()
286- local neo_tree_tabnr = vim .api .nvim_buf_get_var (prior_buf , " neo_tree_tabnr" )
287- if neo_tree_tabnr ~= current_tabnr then
288- -- This a new tab, so the alternate being neo-tree doesn't matter.
289- return
290- end
291- local neo_tree_winid = vim .api .nvim_buf_get_var (prior_buf , " neo_tree_winid" )
292- local current_winid = vim .api .nvim_get_current_win ()
293- if neo_tree_winid ~= current_winid then
294- -- This is not the neo-tree window, so the alternate being neo-tree doesn't matter.
295- return
296- end
297-
298- local bufname = vim .api .nvim_buf_get_name (0 )
299- log .debug (" redirecting buffer " .. bufname .. " to new split" )
300- vim .cmd (" b#" )
301- -- Using schedule at this point fixes problem with syntax
302- -- highlighting in the buffer. I also prevents errors with diagnostics
303- -- trying to work with gthe buffer as it's being closed.
304- vim .schedule (function ()
305- -- try to delete the buffer, only because if it was new it would take
306- -- on options from the neo-tree window that are undesirable.
307- pcall (vim .cmd , " bdelete " .. bufname )
308- local fake_state = {
309- window = {
310- position = " left" ,
311- },
312- }
313- utils .open_file (fake_state , bufname )
314- end )
315- end
316- end ,
354+ handler = M .buffer_enter_event ,
317355 })
318356
319357 if config .event_handlers ~= nil then
0 commit comments