Skip to content

Commit 02ce839

Browse files
authored
refactor: only create BufAdd autocmd if it's necessary (#720)
Co-authored-by: Eman Resu <78693624+quatquatt@users.noreply.github.com>
1 parent 48fc83a commit 02ce839

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

lua/oil/init.lua

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,6 @@ local function maybe_hijack_directory_buffer(bufnr)
813813
local config = require("oil.config")
814814
local fs = require("oil.fs")
815815
local util = require("oil.util")
816-
if not config.default_file_explorer then
817-
return false
818-
end
819816
local bufname = vim.api.nvim_buf_get_name(bufnr)
820817
if bufname == "" then
821818
return false
@@ -1397,15 +1394,7 @@ M.setup = function(opts)
13971394
vim.w.oil_original_alternate = vim.w[parent_win].oil_original_alternate
13981395
end,
13991396
})
1400-
vim.api.nvim_create_autocmd("BufAdd", {
1401-
desc = "Detect directory buffer and open oil file browser",
1402-
group = aug,
1403-
pattern = "*",
1404-
nested = true,
1405-
callback = function(params)
1406-
maybe_hijack_directory_buffer(params.buf)
1407-
end,
1408-
})
1397+
14091398
-- mksession doesn't save oil buffers in a useful way. We have to manually load them after a
14101399
-- session finishes loading. See https://github.com/stevearc/oil.nvim/issues/29
14111400
vim.api.nvim_create_autocmd("SessionLoadPost", {
@@ -1424,11 +1413,23 @@ M.setup = function(opts)
14241413
end,
14251414
})
14261415

1427-
local bufnr = vim.api.nvim_get_current_buf()
1428-
if maybe_hijack_directory_buffer(bufnr) and vim.v.vim_did_enter == 1 then
1429-
-- manually call load on a hijacked directory buffer if vim has already entered
1430-
-- (the BufReadCmd will not trigger)
1431-
M.load_oil_buffer(bufnr)
1416+
if config.default_file_explorer then
1417+
vim.api.nvim_create_autocmd("BufAdd", {
1418+
desc = "Detect directory buffer and open oil file browser",
1419+
group = aug,
1420+
pattern = "*",
1421+
nested = true,
1422+
callback = function(params)
1423+
maybe_hijack_directory_buffer(params.buf)
1424+
end,
1425+
})
1426+
1427+
local bufnr = vim.api.nvim_get_current_buf()
1428+
if maybe_hijack_directory_buffer(bufnr) and vim.v.vim_did_enter == 1 then
1429+
-- manually call load on a hijacked directory buffer if vim has already entered
1430+
-- (the BufReadCmd will not trigger)
1431+
M.load_oil_buffer(bufnr)
1432+
end
14321433
end
14331434
end
14341435

0 commit comments

Comments
 (0)