Skip to content

Commit

Permalink
fix: complex character file name resolution for multiple files
Browse files Browse the repository at this point in the history
This also fixes an issue where the currently open file would not get
preselected when yazi was opened when the file had complex characters
such as '$' in it.
  • Loading branch information
mikavilpas committed Apr 29, 2024
1 parent bdec3b6 commit 051bfce
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function M.yazi(config, path)
os.remove(config.chosen_file_path)
local cmd = string.format(
'yazi "%s" --local-events "rename,delete,trash,move" --chooser-file "%s" > %s',
path,
vim.fn.fnameescape(path),
config.chosen_file_path,
config.events_file_path
)
Expand Down
8 changes: 4 additions & 4 deletions lua/yazi/openers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ local M = {}

---@param chosen_file string
function M.open_file(chosen_file)
vim.cmd(string.format('edit %s', chosen_file))
vim.cmd(string.format('edit %s', vim.fn.fnameescape(chosen_file)))
end

---@param chosen_file string
function M.open_file_in_vertical_split(chosen_file)
vim.cmd(string.format('vsplit %s', chosen_file))
vim.cmd(string.format('vsplit %s', vim.fn.fnameescape(chosen_file)))
end

---@param chosen_file string
function M.open_file_in_horizontal_split(chosen_file)
vim.cmd(string.format('split %s', chosen_file))
vim.cmd(string.format('split %s', vim.fn.fnameescape(chosen_file)))
end

---@param chosen_file string
function M.open_file_in_tab(chosen_file)
vim.cmd(string.format('tabedit %s', chosen_file))
vim.cmd(string.format('tabedit %s', vim.fn.fnameescape(chosen_file)))
end

---@param chosen_files string[]
Expand Down
3 changes: 1 addition & 2 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ function M.on_yazi_exited(prev_win, window, config)
local chosen_file = chosen_files[1]
config.hooks.yazi_closed_successfully(chosen_file, config)
if chosen_file then
local path = vim.fn.fnameescape(chosen_file)
config.open_file_function(path, config)
config.open_file_function(chosen_file, config)
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions tests/yazi/open_multiple_files_spec.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
describe('the default configuration', function()
before_each(function()
-- clear all buffers
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
vim.api.nvim_buf_delete(buf, { force = true })
end
end)

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' }
-- include problematic characters in the file names to preserve their behaviour
local chosen_files = { '/abc/[email protected]', '/abc/test-file2.txt' }

config.hooks.yazi_opened_multiple_files(chosen_files, config)

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-$@file.txt', quickfix_list[1].text)
assert.equals('/abc/test-file2.txt', quickfix_list[2].text)
end)
end)
4 changes: 2 additions & 2 deletions tests/yazi/yazi_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ describe('opening a file', function()
end

it('opens yazi with the current file selected', function()
vim.api.nvim_command('edit /abc/test-file-1.txt')
vim.api.nvim_command('edit ' .. vim.fn.fnameescape('/abc/test-file-$1.txt'))
plugin.yazi()

assert.stub(api_mock.termopen).was_called_with(
'yazi "/abc/test-file-1.txt" --local-events "rename,delete,trash,move" --chooser-file "/tmp/yazi_filechosen" > /tmp/yazi.nvim.events.txt',
'yazi "/abc/test-file-\\$1.txt" --local-events "rename,delete,trash,move" --chooser-file "/tmp/yazi_filechosen" > /tmp/yazi.nvim.events.txt',
match.is_table()
)
end)
Expand Down

0 comments on commit 051bfce

Please sign in to comment.