Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/open directory in yazi #2

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ Using lazy.nvim:
dependencies = {
"nvim-lua/plenary.nvim",
},
cmd = "Yazi",
event = "VeryLazy",
keys = {
{ "<leader>-", "<cmd>Yazi<CR>", desc = "Toggle Yazi" },
{
"<leader>-",
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,
},
}
```
31 changes: 31 additions & 0 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/yazi/example_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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()

Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tests/yazi/no_open_dir_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions tests/yazi/open_dir_spec.lua
Original file line number Diff line number Diff line change
@@ -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)
Loading