Skip to content

Commit c59fc31

Browse files
committed
perf(#3253): short-circuit open_on_directory au
1 parent d38839d commit c59fc31

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

lua/nvim-tree.lua

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ local function setup_autocommands()
4545
})
4646
end
4747

48-
-- TODO this fires on setup
49-
-- if config.g.hijack_directories.enable and (config.g.disable_netrw or config.g.hijack_netrw) then
50-
-- vim.api.nvim_create_autocmd({ "BufEnter", "BufNewFile" }, {
51-
-- group = augroup_id,
52-
-- callback = function()
53-
-- require("nvim-tree.actions.tree.open").open_on_directory()
54-
-- end,
55-
-- nested = true
56-
-- })
57-
-- end
48+
if config.g.hijack_directories.enable and (config.g.disable_netrw or config.g.hijack_netrw) then
49+
vim.api.nvim_create_autocmd({ "BufEnter", "BufNewFile" }, {
50+
group = augroup_id,
51+
nested = true,
52+
callback = function(data)
53+
local bufname = vim.api.nvim_buf_get_name(data.buf)
54+
if vim.fn.isdirectory(bufname) == 1 then
55+
require("nvim-tree.actions.tree.open").open_on_directory(bufname)
56+
end
57+
end,
58+
})
59+
end
5860

5961
if config.g.view.centralize_selection then
6062
vim.api.nvim_create_autocmd("BufEnter", {

lua/nvim-tree/actions/tree/open.lua

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,21 @@ function M.fn(opts)
5151
end
5252
end
5353

54-
function M.open_on_directory()
54+
---@param dirname string absolute directory path
55+
function M.open_on_directory(dirname)
5556
local should_proceed = config.g.hijack_directories.auto_open or view.is_visible()
5657
if not should_proceed then
5758
return
5859
end
5960

60-
local buf = vim.api.nvim_get_current_buf()
61-
local bufname = vim.api.nvim_buf_get_name(buf)
62-
if vim.fn.isdirectory(bufname) ~= 1 then
63-
return
64-
end
65-
6661
-- instantiate an explorer if there is not one
6762
if not core.get_explorer() then
68-
core.init(bufname)
63+
core.init(dirname)
6964
end
7065

7166
local explorer = core.get_explorer()
7267
if explorer then
73-
explorer:force_dirchange(bufname, true, false)
68+
explorer:force_dirchange(dirname, true, false)
7469
end
7570
end
7671

0 commit comments

Comments
 (0)