From f6d87528c54a3d2eaa1e613d6a3b49cb81428d6e Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Thu, 14 Mar 2024 19:33:11 +0200 Subject: [PATCH 1/2] refactor(tests): simplify test code a bit --- tests/yazi/example_spec.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/yazi/example_spec.lua b/tests/yazi/example_spec.lua index 4cd8442..ddc70d1 100644 --- a/tests/yazi/example_spec.lua +++ b/tests/yazi/example_spec.lua @@ -7,7 +7,7 @@ local api_mock = mock(require('yazi.vimfn')) local plugin = require('yazi') -describe('setup with no custom options', function() +describe('opening a file', function() before_each(function() -- add default options that are set in ../../plugin/yazi.vim vim.g.yazi_floating_window_winblend = 0 @@ -19,7 +19,7 @@ describe('setup with no custom options', function() end) it('opens yazi with the current file selected', function() - vim.api.nvim_command('edit ' .. '/tmp/test-file.txt') + vim.api.nvim_command('edit /tmp/test-file.txt') plugin.yazi() assert.stub(api_mock.termopen).was_called_with( @@ -29,7 +29,7 @@ describe('setup with no custom options', function() end) it('opens yazi with the current directory selected', function() - vim.api.nvim_command('edit ' .. '/tmp/') + vim.api.nvim_command('edit /tmp/') plugin.yazi() @@ -44,7 +44,7 @@ describe('setup with no custom options', function() before_each(function() -- have to start editing a valid file, otherwise the plugin will ignore the callback - vim.cmd('edit ' .. '/tmp/a.txt') + vim.cmd('edit /tmp/a.txt') local termopen = spy.on(api_mock, 'termopen') termopen.callback = function(_, callback) From 918f8df821defcb4d56ec6d7e21ed9e81ec3de06 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Wed, 13 Mar 2024 21:14:00 +0200 Subject: [PATCH 2/2] feat: can show yazi when opening a directory --- README.md | 15 +++++++++++++-- lua/yazi.lua | 31 +++++++++++++++++++++++++++++++ lua/yazi/config.lua | 13 +++++++++++++ tests/yazi/no_open_dir_spec.lua | 21 +++++++++++++++++++++ tests/yazi/open_dir_spec.lua | 30 ++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 lua/yazi/config.lua create mode 100644 tests/yazi/no_open_dir_spec.lua create mode 100644 tests/yazi/open_dir_spec.lua diff --git a/README.md b/README.md index 2665cc8..ea41ce5 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,20 @@ Using lazy.nvim: dependencies = { "nvim-lua/plenary.nvim", }, - cmd = "Yazi", + event = "VeryLazy", keys = { - { "-", "Yazi", desc = "Toggle Yazi" }, + { + "-", + function() + require("yazi").yazi() + end, + { desc = "Open the file manager" }, + }, + }, + ---@type YaziConfig + opts = { + -- enable this if you want to open yazi instead of netrw + open_for_directories = false, }, } ``` diff --git a/lua/yazi.lua b/lua/yazi.lua index c893988..7c00c73 100644 --- a/lua/yazi.lua +++ b/lua/yazi.lua @@ -1,6 +1,7 @@ local window = require('yazi.window') local utils = require('yazi.utils') local vimfn = require('yazi.vimfn') +local config = require('yazi.config') local M = {} @@ -63,4 +64,34 @@ function M.yazi(path) vim.cmd('startinsert') end +M.config = config.default() + +---@param opts YaziConfig? +function M.setup(opts) + M.config = vim.tbl_deep_extend('force', M.config, opts or {}) + + if M.config.open_for_directories == true then + local yazi_augroup = vim.api.nvim_create_augroup('yazi', { clear = true }) + + -- disable netrw, the built-in file explorer + vim.cmd('silent! autocmd! FileExplorer *') + + -- executed before starting to edit a new buffer. + vim.api.nvim_create_autocmd('BufAdd', { + pattern = '*', + 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 }) + require('yazi').yazi(file) + end + end, + group = yazi_augroup, + }) + end +end + return M diff --git a/lua/yazi/config.lua b/lua/yazi/config.lua new file mode 100644 index 0000000..7ceb55d --- /dev/null +++ b/lua/yazi/config.lua @@ -0,0 +1,13 @@ +---@class YaziConfig +---@field public open_for_directories boolean + +local M = {} + +---@return YaziConfig +function M.default() + return { + open_for_directories = false, + } +end + +return M diff --git a/tests/yazi/no_open_dir_spec.lua b/tests/yazi/no_open_dir_spec.lua new file mode 100644 index 0000000..8db6847 --- /dev/null +++ b/tests/yazi/no_open_dir_spec.lua @@ -0,0 +1,21 @@ +local assert = require('luassert') +local mock = require('luassert.mock') + +local api_mock = mock(require('yazi.vimfn')) + +local plugin = require('yazi') + +describe('when the user set open_for_directories = false', function() + after_each(function() + mock.clear(api_mock) + end) + + before_each(function() + plugin.setup({ open_for_directories = false }) + end) + + it('does not show yazi when a directory is opened', function() + vim.api.nvim_command('edit /') + assert.stub(api_mock.termopen).was_not_called() + end) +end) diff --git a/tests/yazi/open_dir_spec.lua b/tests/yazi/open_dir_spec.lua new file mode 100644 index 0000000..ecf6fd6 --- /dev/null +++ b/tests/yazi/open_dir_spec.lua @@ -0,0 +1,30 @@ +local assert = require('luassert') +local mock = require('luassert.mock') +local match = require('luassert.match') + +local api_mock = mock(require('yazi.vimfn')) + +local plugin = require('yazi') + +describe('when the user set open_for_directories = true', function() + before_each(function() + -- add default options that are set in ../../plugin/yazi.vim + vim.g.yazi_floating_window_winblend = 0 + vim.g.yazi_floating_window_scaling_factor = 0.9 + + plugin.setup({ open_for_directories = true }) + end) + + after_each(function() + mock.clear(api_mock) + end) + + it('shows yazi when a directory is opened', function() + -- instead of netrw opening, yazi should open + vim.api.nvim_command('edit /') + + assert + .stub(api_mock.termopen) + .was_called_with('yazi "/" --chooser-file "/tmp/yazi_filechosen"', match.is_table()) + end) +end)