-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: renaming the same file multiple times did not sync to nvim
- Loading branch information
1 parent
07826ef
commit 3a59384
Showing
7 changed files
with
238 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
local plenary_path = require('plenary.path') | ||
local plenary_iterators = require('plenary.iterators') | ||
|
||
local function remove_trailing_slash(path) | ||
if path:sub(-1) == '/' then | ||
return path:sub(1, -2) | ||
end | ||
|
||
return path | ||
end | ||
|
||
---@class RenameableBuffer | ||
---@field bufnr number | ||
---@field original_path string | ||
---@field path Path | ||
local RenameableBuffer = {} | ||
RenameableBuffer.__index = RenameableBuffer | ||
|
||
---@param bufnr number | ||
---@param path string the original path of the buffer | ||
function RenameableBuffer.new(bufnr, path) | ||
local self = setmetatable({}, RenameableBuffer) | ||
|
||
path = remove_trailing_slash(path) | ||
|
||
self.bufnr = bufnr | ||
self.original_path = path | ||
self.path = plenary_path:new(path) | ||
|
||
return self | ||
end | ||
|
||
---@param path string can be a parent directory or an exact file path | ||
---@return boolean | ||
function RenameableBuffer:matches_exactly(path) | ||
path = remove_trailing_slash(path) | ||
return self.path.filename == path | ||
end | ||
|
||
---@param path string | ||
function RenameableBuffer:matches_parent(path) | ||
path = remove_trailing_slash(path) | ||
local found = plenary_iterators | ||
.iter(self.path:parents()) | ||
:find(function(parent_path) | ||
return path == parent_path | ||
end) | ||
|
||
return found ~= nil | ||
end | ||
|
||
---@param path string | ||
---@return nil | ||
function RenameableBuffer:rename(path) | ||
path = remove_trailing_slash(path) | ||
self.path = plenary_path:new(path) | ||
end | ||
|
||
---@param parent_from string the parent directory that was renamed | ||
---@param parent_to string the new parent directory | ||
---@return nil | ||
function RenameableBuffer:rename_parent(parent_from, parent_to) | ||
local common = self.path.filename:sub(1, #parent_from) | ||
local rest = self.path.filename:sub(#common + 1, -1) | ||
|
||
local new_path = parent_to .. rest | ||
|
||
self.path = plenary_path:new(new_path) | ||
end | ||
|
||
return RenameableBuffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
local RenameableBuffer = require('yazi.renameable_buffer') | ||
local assert = require('luassert') | ||
|
||
describe('the RenameableBuffer class', function() | ||
it('matches a parent directory', function() | ||
local rename = RenameableBuffer.new(1, '/my-tmp/file1') | ||
local result = rename:matches_parent('/my-tmp/') | ||
assert.is_true(result) | ||
end) | ||
|
||
it('matches a parent directory with a trailing slash', function() | ||
local rename = RenameableBuffer.new(1, '/my-tmp/file1') | ||
local result = rename:matches_parent('/my-tmp') | ||
assert.is_true(result) | ||
end) | ||
|
||
it('matches an exact file path', function() | ||
local rename = RenameableBuffer.new(1, '/my-tmp/file1') | ||
local result = rename:matches_exactly('/my-tmp/file1') | ||
assert.is_true(result) | ||
end) | ||
|
||
it('does not match a different parent directory', function() | ||
local rename = RenameableBuffer.new(1, '/my-tmp/file1') | ||
assert.is_false(rename:matches_exactly('/my-tmp2')) | ||
end) | ||
|
||
it('does not match a different file path', function() | ||
local rename = RenameableBuffer.new(1, '/my-tmp/file1') | ||
assert.is_false(rename:matches_exactly('/my-tmp/file2')) | ||
end) | ||
|
||
it('renames a file', function() | ||
local rename = RenameableBuffer.new(1, '/my-tmp/file1') | ||
rename:rename('/my-tmp/file2') | ||
assert.are.equal('/my-tmp/file2', rename.path.filename) | ||
|
||
-- does not change the original path | ||
assert.are.equal('/my-tmp/file1', rename.original_path) | ||
end) | ||
|
||
it("renames the buffer's parent directory", function() | ||
local buffer = RenameableBuffer.new(1, '/my-tmp/file1') | ||
buffer:rename_parent('/my-tmp', '/my-tmp2') | ||
assert.are.equal('/my-tmp2/file1', buffer.path.filename) | ||
|
||
-- does not change the original path | ||
assert.are.equal('/my-tmp/file1', buffer.original_path) | ||
end) | ||
end) |