From 0b4f72f98ee347fe8732743e8cac8390d369d0f1 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sun, 7 Apr 2024 18:39:46 +0300 Subject: [PATCH] refactor: simplify buffer renaming logic --- lua/yazi.lua | 12 ++++- lua/yazi/utils.lua | 10 ++-- tests/yazi/no_open_dir_spec.lua | 1 + tests/yazi/rename_spec.lua | 82 +++++++++------------------------ 4 files changed, 37 insertions(+), 68 deletions(-) diff --git a/lua/yazi.lua b/lua/yazi.lua index eabc9f7a..c9c378f9 100644 --- a/lua/yazi.lua +++ b/lua/yazi.lua @@ -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 @@ -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) diff --git a/lua/yazi/utils.lua b/lua/yazi/utils.lua index 1766d226..133f0316 100644 --- a/lua/yazi/utils.lua +++ b/lua/yazi/utils.lua @@ -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[] @@ -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 diff --git a/tests/yazi/no_open_dir_spec.lua b/tests/yazi/no_open_dir_spec.lua index 8db68476..9246357b 100644 --- a/tests/yazi/no_open_dir_spec.lua +++ b/tests/yazi/no_open_dir_spec.lua @@ -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) diff --git a/tests/yazi/rename_spec.lua b/tests/yazi/rename_spec.lua index 05ae094f..5ab0e8df 100644 --- a/tests/yazi/rename_spec.lua +++ b/tests/yazi/rename_spec.lua @@ -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', }, } @@ -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', }, } @@ -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', }, } @@ -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', }, } @@ -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', }, }