Skip to content

Commit

Permalink
feat: when files are moved in yazi, they stay in sync in nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 11, 2024
1 parent 77ed04d commit 0904cdd
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 11 deletions.
16 changes: 15 additions & 1 deletion lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function M.yazi(config, path)

os.remove(config.chosen_file_path)
local cmd = string.format(
'yazi "%s" --local-events "rename,delete,trash" --chooser-file "%s" > %s',
'yazi "%s" --local-events "rename,delete,trash,move" --chooser-file "%s" > %s',
path,
config.chosen_file_path,
config.events_file_path
Expand Down Expand Up @@ -84,6 +84,20 @@ function M.yazi(config, path)
instruction.path.filename
)
end
elseif event.type == 'move' then
---@cast event YaziMoveEvent
for _, item in ipairs(event.data.items) do
local rename_instructions =
event_handling.get_buffers_that_need_renaming_after_yazi_exited(
item
)
for _, instruction in ipairs(rename_instructions) do
vim.api.nvim_buf_set_name(
instruction.bufnr,
instruction.path.filename
)
end
end
elseif event.type == 'delete' then
---@cast event YaziDeleteEvent
event_handling.process_delete_event(event)
Expand Down
2 changes: 1 addition & 1 deletion lua/yazi/event_handling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function M.process_delete_event(event)
end
end

---@param rename_event YaziEventDataRename
---@param rename_event YaziEventDataRenameOrMove
---@return RenameableBuffer[] "instructions for renaming the buffers (command pattern)"
function M.get_buffers_that_need_renaming_after_yazi_exited(rename_event)
local open_buffers = utils.get_open_buffers()
Expand Down
12 changes: 9 additions & 3 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
---@field public yazi_opened? fun(preselected_path: string | nil): nil
---@field public yazi_closed_successfully? fun(chosen_file: string | nil): nil

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

---@class YaziRenameEvent
---@field public type "rename"
---@field public timestamp string
---@field public id string
---@field public data YaziEventDataRename
---@field public data YaziEventDataRenameOrMove

---@class YaziEventDataRename
---@class YaziMoveEvent
---@field public type "move"
---@field public timestamp string
---@field public id string
---@field public data {items: YaziEventDataRenameOrMove[]}

---@class YaziEventDataRenameOrMove
---@field public from string
---@field public to string

Expand Down
15 changes: 15 additions & 0 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ function M.parse_events(events_file_lines)
data = vim.fn.json_decode(data_string),
}
table.insert(events, event)
elseif type == 'move' then
-- example of a move event:
-- move,1712854829131439,1712854829131439,{"items":[{"from":"/tmp/test/test","to":"/tmp/test"}]}
local timestamp = parts[2]
local id = parts[3]
local data_string = table.concat(parts, ',', 4, #parts)

---@type YaziMoveEvent
local event = {
type = type,
timestamp = timestamp,
id = id,
data = vim.fn.json_decode(data_string),
}
table.insert(events, event)
elseif type == 'delete' then
-- example of a delete event:
-- delete,1712766606832135,1712766606832135,{"urls":["/tmp/test-directory/test_2"]}
Expand Down
75 changes: 75 additions & 0 deletions tests/yazi/move_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local assert = require('luassert')
local event_handling = require('yazi.event_handling')

describe('get_buffers_that_need_renaming_after_yazi_exited', 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 detect moves to files whose names match exactly', function()
---@type YaziEventDataRenameOrMove
local move_event = {
from = '/my-tmp/file1',
to = '/my-tmp/file2',
}

-- simulate buffers being opened
vim.fn.bufadd('/my-tmp/file1')
vim.fn.bufadd('/my-tmp/file_A')

local instructions =
event_handling.get_buffers_that_need_renaming_after_yazi_exited(
move_event
)

assert.is_equal(vim.tbl_count(instructions), 1)

local result1 = instructions[1]
assert.is_equal('/my-tmp/file2', result1.path.filename)
assert.is_number(result1.bufnr)
end)

it(
'can detect moves to buffers open in a directory that was moved',
function()
---@type YaziEventDataRenameOrMove
local move_event = {
from = '/my-tmp/dir1',
to = '/my-tmp/dir2',
}

-- simulate the buffer being opened
vim.fn.bufadd('/my-tmp/dir1/file')

local instructions =
event_handling.get_buffers_that_need_renaming_after_yazi_exited(
move_event
)

assert.is_equal(vim.tbl_count(instructions), 1)
local result = instructions[1]
assert.is_equal('/my-tmp/dir2/file', result.path.filename)
end
)

it("doesn't move a buffer that was not moved in yazi", function()
---@type YaziEventDataRenameOrMove
local move_event = {
from = '/my-tmp/not-opened-file',
to = '/my-tmp/not-opened-file-moved',
}

-- simulate the buffer being opened
vim.fn.bufadd('/my-tmp/dir1/file')

local instructions =
event_handling.get_buffers_that_need_renaming_after_yazi_exited(
move_event
)

assert.is_equal(vim.tbl_count(instructions), 0)
end)
end)
2 changes: 1 addition & 1 deletion tests/yazi/open_dir_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('when the user set open_for_directories = true', function()
vim.api.nvim_command('edit /')

assert.stub(api_mock.termopen).was_called_with(
'yazi "/" --local-events "rename,delete,trash" --chooser-file "/tmp/yazi_filechosen" > /tmp/yazi.nvim.events.txt',
'yazi "/" --local-events "rename,delete,trash,move" --chooser-file "/tmp/yazi_filechosen" > /tmp/yazi.nvim.events.txt',
match.is_table()
)
end)
Expand Down
6 changes: 3 additions & 3 deletions tests/yazi/rename_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
end)

it('can detect renames to files whose names match exactly', function()
---@type YaziEventDataRename
---@type YaziEventDataRenameOrMove
local rename_event = {
from = '/my-tmp/file1',
to = '/my-tmp/file2',
Expand All @@ -35,7 +35,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
it(
'can detect renames to buffers open in a directory that was renamed',
function()
---@type YaziEventDataRename
---@type YaziEventDataRenameOrMove
local rename_event = {
from = '/my-tmp/dir1',
to = '/my-tmp/dir2',
Expand All @@ -56,7 +56,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
)

it("doesn't rename a buffer that was not renamed in yazi", function()
---@type YaziEventDataRename
---@type YaziEventDataRenameOrMove
local rename_event = {
from = '/my-tmp/not-opened-file',
to = '/my-tmp/not-opened-file-renamed',
Expand Down
4 changes: 2 additions & 2 deletions tests/yazi/yazi_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('opening a file', function()
plugin.yazi()

assert.stub(api_mock.termopen).was_called_with(
'yazi "/abc/test-file.txt" --local-events "rename,delete,trash" --chooser-file "/tmp/yazi_filechosen" > /tmp/yazi.nvim.events.txt',
'yazi "/abc/test-file.txt" --local-events "rename,delete,trash,move" --chooser-file "/tmp/yazi_filechosen" > /tmp/yazi.nvim.events.txt',
match.is_table()
)
end)
Expand All @@ -28,7 +28,7 @@ describe('opening a file', function()
plugin.yazi()

assert.stub(api_mock.termopen).was_called_with(
'yazi "/tmp/" --local-events "rename,delete,trash" --chooser-file "/tmp/yazi_filechosen" > /tmp/yazi.nvim.events.txt',
'yazi "/tmp/" --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 0904cdd

Please sign in to comment.