Skip to content

Commit

Permalink
feat: show multiple selected files in the quickfix list
Browse files Browse the repository at this point in the history
In yazi, it's possible to open one file or multiple files. Now, if a
single file is opened, there is no change in behavior. However, if
multiple files are opened, the quickfix list will show all the files
that are opened.
  • Loading branch information
mikavilpas committed Apr 12, 2024
1 parent 4bd98f1 commit 13aa3e4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Using lazy.nvim:

-- when yazi was successfully closed
yazi_closed_successfully = function(chosen_file) end,

-- when yazi opened multiple files. The default is to send them to the
-- quickfix list, but if you want to change that, you can define it here
yazi_opened_multiple_files = function(chosen_files) end,
},

-- the floating window scaling factor. 1 means 100%, 0.9 means 90%, etc.
Expand All @@ -85,5 +89,6 @@ So far I have done some maintenance work and added a bunch of features:
- feat: health check for yazi
- feat: files that are renamed, moved, deleted, or trashed in yazi are kept in sync with open buffers (this requires a version of yazi that includes [this](https://github.com/sxyazi/yazi/pull/880) change from 2024-04-06)
- feat: allow customizing the method of opening the selected file in neovim
- feat: can send multiple opened files to the quickfix list

If you'd like to collaborate, contact me via GitHub issues.
14 changes: 10 additions & 4 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ function M.yazi(config, path)
if
code == 0 and utils.file_exists(config.chosen_file_path) == true
then
local chosen_file = vim.fn.readfile(config.chosen_file_path)[1]
config.hooks.yazi_closed_successfully(chosen_file)
if chosen_file then
config.open_file_function(chosen_file)
local chosen_files = vim.fn.readfile(config.chosen_file_path)

if #chosen_files > 1 then
config.hooks.yazi_opened_multiple_files(chosen_files)
else
local chosen_file = chosen_files[1]
config.hooks.yazi_closed_successfully(chosen_file)
if chosen_file then
config.open_file_function(chosen_file)
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions lua/yazi/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ function M.default()
yazi_opened = function(_preselected_path) end,
---@diagnostic disable-next-line: unused-local
yazi_closed_successfully = function(_chosen_file) end,
yazi_opened_multiple_files = function(chosen_files)
-- show the items it the quickfix list
vim.fn.setqflist({}, 'r', {
title = 'Yazi',
items = vim.tbl_map(function(file)
return {
filename = file,
lnum = 1,
text = file,
}
end, chosen_files),
})

-- open the quickfix window
vim.cmd('copen')
end,
},

floating_window_scaling_factor = 0.9,
Expand Down
1 change: 1 addition & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
---@class YaziConfigHooks
---@field public yazi_opened? fun(preselected_path: string | nil): nil
---@field public yazi_closed_successfully? fun(chosen_file: string | nil): nil
---@field public yazi_opened_multiple_files? fun(chosen_files: string[]): nil

---@alias YaziEvent YaziRenameEvent | YaziMoveEvent | YaziDeleteEvent | YaziTrashEvent

Expand Down
14 changes: 14 additions & 0 deletions tests/yazi/open_multiple_files_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('the default configuration', function()
it('can display multiple files in the quickfix list', function()
local config = require('yazi.config').default()
local chosen_files = { '/abc/test-file.txt', '/abc/test-file2.txt' }

config.hooks.yazi_opened_multiple_files(chosen_files)

local quickfix_list = vim.fn.getqflist()

assert.equals(2, #quickfix_list)
assert.equals('/abc/test-file.txt', quickfix_list[1].text)
assert.equals('/abc/test-file2.txt', quickfix_list[2].text)
end)
end)
38 changes: 35 additions & 3 deletions tests/yazi/yazi_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ describe('opening a file', function()
end)

it('calls the open_file_function to open the selected file', function()
local spy_hook = spy.new(function(chosen_file)
assert.equals('/abc/test-file.txt', chosen_file)
end)
local spy_hook = spy.new()

vim.api.nvim_command('edit /abc/test-file.txt')

Expand All @@ -108,3 +106,37 @@ describe('opening a file', function()
assert.spy(spy_hook).was_called_with('/abc/test-file.txt')
end)
end)
describe('opening multiple files', function()
local target_file_1 = '/abc/test-file-multiple-1.txt'
local target_file_2 = '/abc/test-file-multiple-2.txt'

before_each(function()
local termopen = spy.on(api_mock, 'termopen')
termopen.callback = function(_, callback)
-- simulate yazi writing to the output file. This is done when a file is
-- chosen in yazi
local exit_code = 0
vim.fn.writefile({
target_file_1,
target_file_2,
}, '/tmp/yazi_filechosen-123')
callback.on_exit('job-id-ignored', exit_code, 'event-ignored')
end
end)

it('can open multiple files', function()
local spy_open_multiple_files = spy.new()
plugin.yazi({
hooks = {
---@diagnostic disable-next-line: assign-type-mismatch
yazi_opened_multiple_files = spy_open_multiple_files,
},
chosen_file_path = '/tmp/yazi_filechosen-123',
})

assert.spy(spy_open_multiple_files).was_called_with({
target_file_1,
target_file_2,
})
end)
end)

0 comments on commit 13aa3e4

Please sign in to comment.