Skip to content

Commit

Permalink
refactor: move renaming related code to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Apr 9, 2024
1 parent a87a281 commit 29d563e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
5 changes: 4 additions & 1 deletion lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local window = require('yazi.window')
local utils = require('yazi.utils')
local vimfn = require('yazi.vimfn')
local config = require('yazi.config')
local renaming = require('yazi.renaming')

local iterators = require('plenary.iterators')

Expand Down Expand Up @@ -81,7 +82,9 @@ function M.yazi(path)
:tolist()

local renames =
utils.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
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)
Expand Down
36 changes: 36 additions & 0 deletions lua/yazi/renaming.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local RenameableBuffer = require('yazi.renameable_buffer')

local M = {}

---@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[]
local open_buffers = {}
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
local path = vim.api.nvim_buf_get_name(bufnr)
if path ~= '' and path ~= nil then
local renameable_buffer = RenameableBuffer.new(bufnr, path)
open_buffers[#open_buffers + 1] = renameable_buffer
end
end

---@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
end
end

return vim.tbl_values(renamed_buffers)
end

return M
32 changes: 0 additions & 32 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local fn = vim.fn
local RenameableBuffer = require('yazi.renameable_buffer')

local M = {}

Expand Down Expand Up @@ -86,35 +85,4 @@ function M.read_events_file(path)
return events
end

---@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[]
local open_buffers = {}
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
local path = vim.api.nvim_buf_get_name(bufnr)
if path ~= '' and path ~= nil then
local renameable_buffer = RenameableBuffer.new(bufnr, path)
open_buffers[#open_buffers + 1] = renameable_buffer
end
end

---@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
end
end

return vim.tbl_values(renamed_buffers)
end

return M
12 changes: 6 additions & 6 deletions tests/yazi/rename_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local assert = require('luassert')
local utils = require('yazi.utils')
local renaming = require('yazi.renaming')

describe('get_buffers_that_need_renaming_after_yazi_exited', function()
before_each(function()
Expand Down Expand Up @@ -27,7 +27,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
vim.fn.bufadd('/my-tmp/file_A')

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

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

Expand Down Expand Up @@ -55,7 +55,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
vim.fn.bufadd('/my-tmp/dir1/file')

local rename_instructions =
utils.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
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]
Expand All @@ -76,7 +76,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
vim.fn.bufadd('/my-tmp/dir1/file')

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

assert.is_equal(vim.tbl_count(rename_instructions), 0)
end)
Expand All @@ -98,7 +98,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
vim.fn.bufadd('/my-tmp/file1')

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

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

Expand All @@ -124,7 +124,7 @@ describe('get_buffers_that_need_renaming_after_yazi_exited', function()
vim.fn.bufadd('/my-tmp/dir1/file')

local rename_instructions =
utils.get_buffers_that_need_renaming_after_yazi_exited(rename_events)
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]
Expand Down

0 comments on commit 29d563e

Please sign in to comment.