Skip to content

Commit 521bb42

Browse files
authored
fix(setup): defer hijack until setup or BufEnter (#1667)
1 parent 60c8f77 commit 521bb42

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

Diff for: lua/neo-tree.lua

+24
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,33 @@ M.set_log_level = function(level)
7777
require("neo-tree.log").set_level(level)
7878
end
7979

80+
---Ideally this should only be in plugin/neo-tree.lua but lazy-loading might mean this runs before bufenter
81+
---@param path string? The path to check
82+
---@return boolean hijacked Whether the hijack worked
83+
local function try_netrw_hijack(path)
84+
if not path or #path == 0 then
85+
return false
86+
end
87+
88+
local stats = (vim.uv or vim.loop).fs_stat(path)
89+
if not stats or stats.type ~= "directory" then
90+
return false
91+
end
92+
93+
local netrw = require("neo-tree.setup.netrw")
94+
if netrw.get_hijack_behavior() ~= "disabled" then
95+
vim.cmd("silent! autocmd! FileExplorer *")
96+
return netrw.hijack()
97+
end
98+
return false
99+
end
100+
80101
M.setup = function(config)
81102
-- merging is deferred until ensure_config
82103
new_user_config = config
104+
if vim.v.vim_did_enter == 0 then
105+
try_netrw_hijack(vim.fn.argv(0) --[[@as string]])
106+
end
83107
end
84108

85109
M.show_logs = function()

Diff for: plugin/neo-tree.lua

+16-18
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,28 @@ local function try_netrw_hijack(path)
1717
return false
1818
end
1919

20+
local stats = (vim.uv or vim.loop).fs_stat(path)
21+
if not stats or stats.type ~= "directory" then
22+
return false
23+
end
24+
2025
local netrw = require("neo-tree.setup.netrw")
2126
if netrw.get_hijack_behavior() ~= "disabled" then
2227
vim.cmd("silent! autocmd! FileExplorer *")
23-
local stats = (vim.uv or vim.loop).fs_stat(path)
24-
if stats and stats.type == "directory" then
25-
return netrw.hijack()
26-
end
28+
return netrw.hijack()
2729
end
2830
return false
2931
end
3032

31-
-- currently need to check first arg to not break hijacked on
32-
-- configs that already lazy-load neo-tree (e.g. lazyvim)
33-
local first_arg = vim.fn.argv(0) --[[@as string]]
34-
if not try_netrw_hijack(first_arg) then
35-
local augroup = vim.api.nvim_create_augroup("NeoTree_NetrwDeferred", { clear = true })
36-
vim.api.nvim_create_autocmd("BufEnter", {
37-
group = augroup,
38-
callback = function(args)
39-
if try_netrw_hijack(args.file) then
40-
vim.api.nvim_del_augroup_by_id(augroup)
41-
end
42-
end,
43-
})
44-
end
33+
local augroup = vim.api.nvim_create_augroup("NeoTree_NetrwDeferred", { clear = true })
34+
35+
vim.api.nvim_create_autocmd("BufEnter", {
36+
group = augroup,
37+
callback = function(args)
38+
if try_netrw_hijack(args.file) then
39+
vim.api.nvim_del_augroup_by_id(augroup)
40+
end
41+
end,
42+
})
4543

4644
vim.g.loaded_neo_tree = 1

0 commit comments

Comments
 (0)