Skip to content

Commit

Permalink
refactor: simplify buffer renaming logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 7, 2024
1 parent da592f1 commit 0b4f72f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 68 deletions.
12 changes: 10 additions & 2 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local utils = require('yazi.utils')
local vimfn = require('yazi.vimfn')
local config = require('yazi.config')

local iterators = require('plenary.iterators')

local M = {}

M.yazi_loaded = false
Expand Down Expand Up @@ -66,9 +68,15 @@ function M.yazi(path)
end

-- process events emitted from yazi
local rename_events = utils.read_events_file(M.config.events_file_path)
local events = utils.read_events_file(M.config.events_file_path)

local renames =
utils.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
utils.get_buffers_that_need_renaming_after_yazi_exited(iterators
.iter(events)
:filter(function(event)
return event.type == 'rename'
end)
:tolist())

for _, event in ipairs(renames) do
vim.api.nvim_buf_set_name(event.bufnr, event.path.filename)
Expand Down
10 changes: 5 additions & 5 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function M.read_events_file(path)
return events
end

---@param rename_events YaziRenameEvent[]
---@param rename_events YaziEventDataRename[]
---@return RenameableBuffer[] "instructions for renaming the buffers (command pattern)"
function M.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
---@type RenameableBuffer[]
Expand All @@ -104,11 +104,11 @@ function M.get_buffers_that_need_renaming_after_yazi_exited(rename_events)

for _, event in ipairs(rename_events) do
for _, buffer in ipairs(open_buffers) do
if buffer:matches_exactly(event.data.from) then
buffer:rename(event.data.to)
if buffer:matches_exactly(event.from) then
buffer:rename(event.to)
renamed_buffers[buffer.bufnr] = buffer
elseif buffer:matches_parent(event.data.from) then
buffer:rename_parent(event.data.from, event.data.to)
elseif buffer:matches_parent(event.from) then
buffer:rename_parent(event.from, event.to)
renamed_buffers[buffer.bufnr] = buffer
end
end
Expand Down
1 change: 1 addition & 0 deletions tests/yazi/no_open_dir_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('when the user set open_for_directories = false', function()
end)

before_each(function()
---@diagnostic disable-next-line: missing-fields
plugin.setup({ open_for_directories = false })
end)

Expand Down
82 changes: 21 additions & 61 deletions tests/yazi/rename_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,15 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
end)

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

Expand All @@ -53,16 +43,11 @@ 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 YaziRenameEvent[]
---@type YaziEventDataRename[]
local rename_events = {
{
type = 'rename',
timestamp = '1712242143209837',
id = '1712242143209837',
data = {
from = '/my-tmp/dir1',
to = '/my-tmp/dir2',
},
from = '/my-tmp/dir1',
to = '/my-tmp/dir2',
},
}

Expand All @@ -79,16 +64,11 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
)

it("doesn't rename a buffer that was not renamed in yazi", function()
---@type YaziRenameEvent[]
---@type YaziEventDataRename[]
local rename_events = {
{
type = 'rename',
timestamp = '1712242143209837',
id = '1712242143209837',
data = {
from = '/my-tmp/not-opened-file',
to = '/my-tmp/not-opened-file-renamed',
},
from = '/my-tmp/not-opened-file',
to = '/my-tmp/not-opened-file-renamed',
},
}

Expand All @@ -102,25 +82,15 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
end)

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

Expand All @@ -138,25 +108,15 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
end)

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

Expand Down

0 comments on commit 0b4f72f

Please sign in to comment.