Skip to content

Commit

Permalink
refactor(renaming): rename one file at a time, not all at once
Browse files Browse the repository at this point in the history
This is preparation for the next step, which is to process many types of
items from yazi, not just rename events.
  • Loading branch information
mikavilpas committed Apr 10, 2024
1 parent 13e35c2 commit 5a60cbc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 113 deletions.
34 changes: 13 additions & 21 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ local vimfn = require('yazi.vimfn')
local configModule = require('yazi.config')
local renaming = require('yazi.renaming')

local iterators = require('plenary.iterators')

local M = {}

M.yazi_loaded = false
Expand Down Expand Up @@ -72,26 +70,20 @@ function M.yazi(config, path)

-- process events emitted from yazi
local events = utils.read_events_file(config.events_file_path)
local rename_events = iterators
.iter(events)
:filter(function(event)
return event.type == 'rename'
end)
:map(
---@param event YaziRenameEvent
function(event)
return event.data
end
)
:tolist()

local renames =
renaming.get_buffers_that_need_renaming_after_yazi_exited(
rename_events
)

for _, event in ipairs(renames) do
vim.api.nvim_buf_set_name(event.bufnr, event.path.filename)
for _, event in ipairs(events) do
if event.type == 'rename' then
local rename_instructions =
renaming.get_buffers_that_need_renaming_after_yazi_exited(
event.data
)
for _, instruction in ipairs(rename_instructions) do
vim.api.nvim_buf_set_name(
instruction.bufnr,
instruction.path.filename
)
end
end
end
end,
})
Expand Down
21 changes: 10 additions & 11 deletions lua/yazi/renaming.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local RenameableBuffer = require('yazi.renameable_buffer')

local M = {}

---@param rename_events YaziEventDataRename[]
---@param rename_event YaziEventDataRename
---@return RenameableBuffer[] "instructions for renaming the buffers (command pattern)"
function M.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
function M.get_buffers_that_need_renaming_after_yazi_exited(rename_event)
---@type RenameableBuffer[]
local open_buffers = {}
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
Expand All @@ -18,15 +18,14 @@ function M.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
---@type table<integer, RenameableBuffer>
local renamed_buffers = {}

for _, event in ipairs(rename_events) do
for _, buffer in ipairs(open_buffers) do
if buffer:matches_exactly(event.from) then
buffer:rename(event.to)
renamed_buffers[buffer.bufnr] = buffer
elseif buffer:matches_parent(event.from) then
buffer:rename_parent(event.from, event.to)
renamed_buffers[buffer.bufnr] = buffer
end
local event = rename_event
for _, buffer in ipairs(open_buffers) do
if buffer:matches_exactly(event.from) then
buffer:rename(event.to)
renamed_buffers[buffer.bufnr] = buffer
elseif buffer:matches_parent(event.from) then
buffer:rename_parent(event.from, event.to)
renamed_buffers[buffer.bufnr] = buffer
end
end

Expand Down
98 changes: 17 additions & 81 deletions tests/yazi/rename_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,40 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
end)

it('can detect renames to files whose names match exactly', function()
---@type YaziEventDataRename[]
local rename_events = {
{
from = '/my-tmp/file1',
to = '/my-tmp/file2',
},
{
from = '/my-tmp/file_A',
to = '/my-tmp/file_B',
},
---@type YaziEventDataRename
local rename_event = {
from = '/my-tmp/file1',
to = '/my-tmp/file2',
}

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

local rename_instructions =
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_event)

assert.is_equal(vim.tbl_count(rename_instructions), 2)
assert.is_equal(vim.tbl_count(rename_instructions), 1)

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

local result2 = rename_instructions[2]
assert.is_equal('/my-tmp/file_B', result2.path.filename)
assert.is_number(result2.bufnr)
end)

it(
'can detect renames to buffers open in a directory that was renamed',
function()
---@type YaziEventDataRename[]
local rename_events = {
{
from = '/my-tmp/dir1',
to = '/my-tmp/dir2',
},
---@type YaziEventDataRename
local rename_event = {
from = '/my-tmp/dir1',
to = '/my-tmp/dir2',
}

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

local rename_instructions =
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_event)

assert.is_equal(vim.tbl_count(rename_instructions), 1)
local result = rename_instructions[1]
Expand All @@ -64,70 +52,18 @@ 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[]
local rename_events = {
{
from = '/my-tmp/not-opened-file',
to = '/my-tmp/not-opened-file-renamed',
},
---@type YaziEventDataRename
local rename_event = {
from = '/my-tmp/not-opened-file',
to = '/my-tmp/not-opened-file-renamed',
}

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

local rename_instructions =
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_event)

assert.is_equal(vim.tbl_count(rename_instructions), 0)
end)

it('can rename the same file multiple times', function()
---@type YaziEventDataRename[]
local rename_events = {
{
from = '/my-tmp/file1',
to = '/my-tmp/file2',
},
{
from = '/my-tmp/file2',
to = '/my-tmp/file3',
},
}

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

local rename_instructions =
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_events)

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

local result = rename_instructions[1]
assert.is_equal('/my-tmp/file3', result.path.filename)
assert.is_number(result.bufnr)
end)

it('can rename the same directory multiple times', function()
---@type YaziEventDataRename[]
local rename_events = {
{
from = '/my-tmp/dir1',
to = '/my-tmp/dir2',
},
{
from = '/my-tmp/dir2',
to = '/my-tmp/dir3',
},
}

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

local rename_instructions =
renaming.get_buffers_that_need_renaming_after_yazi_exited(rename_events)

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

0 comments on commit 5a60cbc

Please sign in to comment.