From c32b990c84b0e5b55a8f8cbe53f2e5ee6ecd37ef Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Fri, 26 Apr 2024 19:29:59 +0300 Subject: [PATCH] feat: can open a directory from the command line When starting neovim from the command line, yazi would not open. E.g. `nvim .` would not open yazi. This commit adds the ability to open yazi from the command line. This might also be supported in other plugins the user may have. I had to disable the functionality in `neo-tree.nvim` to get this to work. ```lua { "nvim-neo-tree/neo-tree.nvim", opts = { filesystem = { hijack_netrw_behavior = "disabled", }, }, } ``` See: https://github.com/mikavilpas/dotfiles/commit/3b5bc69917ee7e808eef0fc9734693d0141ea474#diff-8be1bcf06549d609352b9b2f97e7ceb027155091fe4067d0e9e70e1dee85ed04R36 --- lua/yazi.lua | 26 ++++++++++++++++++-------- lua/yazi/types.lua | 9 +++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) 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