Skip to content

Commit a4633c3

Browse files
committed
feat(files): hijack netrw working, partial work on #23
1 parent 4787b63 commit a4633c3

File tree

3 files changed

+92
-58
lines changed

3 files changed

+92
-58
lines changed

lua/neo-tree.lua

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ M.focus = function(source_name, close_others, toggle_if_open)
143143
manager.focus(source_name)
144144
end
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+
146159
M.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)
202215
end
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+
204283
M.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

lua/neo-tree/ui/renderer.lua

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -357,26 +357,10 @@ local enable_auto_close_floats = function()
357357
end
358358

359359
create_window = function(state)
360-
local winhl = string.format(
361-
"Normal:%s,NormalNC:%s,CursorLine:%s",
362-
highlights.NORMAL,
363-
highlights.NORMALNC,
364-
highlights.CURSOR_LINE
365-
)
366-
367360
local win_options = {
368361
size = utils.resolve_config_option(state, "window.width", "40"),
369362
position = utils.resolve_config_option(state, "window.position", "left"),
370363
relative = "editor",
371-
win_options = {
372-
cursorline = true,
373-
number = false,
374-
relativenumber = false,
375-
wrap = false,
376-
winhighlight = winhl,
377-
list = false,
378-
spell = false,
379-
},
380364
buf_options = {
381365
buftype = "nowrite",
382366
modifiable = false,
@@ -391,6 +375,8 @@ create_window = function(state)
391375
-- First get the default options for floating windows.
392376
local sourceTitle = state.name:gsub("^%l", string.upper)
393377
win_options = popups.popup_options("Neo-tree " .. sourceTitle, 40, win_options)
378+
win_options.win_options = nil
379+
print(vim.inspect(win_options))
394380
win_options.zindex = 40
395381
local size = { width = 60, height = "80%" }
396382

plugin/neo-tree.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ command! NeoTreeRevealToggle lua require("neo-tree").reveal_current_file("filesy
1717
command! NeoTreePasteConfig lua require("neo-tree").paste_default_config()
1818
command! -nargs=? NeoTreeSetLogLevel lua require("neo-tree").set_log_level("<args>")
1919
command! NeoTreeLogs lua require("neo-tree").show_logs()
20+
21+
augroup NeoTreePluginLoad
22+
autocmd!
23+
silent! autocmd! FileExplorer *
24+
augroup END
25+
26+
let g:loaded_netrw = 1
27+
let g:loaded_netrwPlugin = 1
28+
let g:loaded_netrwSettings = 1
29+
let g:loaded_netrwFileHandlers = 1

0 commit comments

Comments
 (0)