diff --git a/lua/yazi.lua b/lua/yazi.lua index 9f31a86e..4d14afc2 100644 --- a/lua/yazi.lua +++ b/lua/yazi.lua @@ -71,6 +71,17 @@ function M.setup(opts) vim.tbl_deep_extend('force', configModule.default(), M.config, opts or {}) if M.config.open_for_directories == true then + ---@param file string + ---@param bufnr number + local function open_yazi_in_directory(file, bufnr) + if vim.fn.isdirectory(file) == 1 then + -- A buffer was opened for a directory. + -- Remove the buffer as we want to show yazi instead + vim.api.nvim_buf_delete(bufnr, { force = true }) + M.yazi(M.config, file) + end + end + local yazi_augroup = vim.api.nvim_create_augroup('yazi', { clear = true }) -- disable netrw, the built-in file explorer @@ -79,18 +90,17 @@ function M.setup(opts) -- executed before starting to edit a new buffer. vim.api.nvim_create_autocmd('BufAdd', { pattern = '*', + ---@param ev yazi.AutoCmdEvent callback = function(ev) - local file = ev.file - if vim.fn.isdirectory(file) == 1 then - local bufnr = ev.buf - -- A buffer was opened for a directory. - -- Remove the buffer as we want to show yazi instead - vim.api.nvim_buf_delete(bufnr, { force = true }) - M.yazi(M.config, file) - end + open_yazi_in_directory(ev.file, ev.buf) end, group = yazi_augroup, }) + + -- When opening neovim with "nvim ." or "nvim ", the current + -- buffer is already open at this point. If we have already opened a + -- directory, display yazi instead. + open_yazi_in_directory(vim.fn.expand('%:p'), vim.api.nvim_get_current_buf()) end end diff --git a/lua/yazi/types.lua b/lua/yazi/types.lua index c3098bce..c07b0e11 100644 --- a/lua/yazi/types.lua +++ b/lua/yazi/types.lua @@ -44,3 +44,12 @@ ---@field public timestamp string ---@field public id string ---@field public data {urls: string[]} + +---@class yazi.AutoCmdEvent # the nvim_create_autocmd() event object copied from the nvim help docs +---@field public id number +---@field public event string +---@field public group number | nil +---@field public match string +---@field public buf number +---@field public file string +---@field public data any