Skip to content

Commit

Permalink
fix: symlinked files cannot be highlighted when hovered
Browse files Browse the repository at this point in the history
I experienced this issue when hovering over my yazi.nvim lazy.nvim spec.
It's symlinked to the actual file in the lazy.nvim repository.

These are the same file:

```sh
/Users/mikavilpas/dotfiles/.config/nvim/lua/plugins/my-file-manager.lua
/Users/mikavilpas/.config/nvim/lua/plugins/my-file-manager.lua
```

This commit fixes the issue by resolving the real path of the file
before
comparing it to the path of the buffer.

Closes #195
  • Loading branch information
mikavilpas committed Jul 16, 2024
1 parent 377a68a commit 7a0de28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lua/yazi/renameable_buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ 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
local path_a = vim.uv.fs_realpath(path) or path
local path_b = vim.uv.fs_realpath(self.path.filename) or self.path.filename

return path_a == path_b
end

---@param path string
Expand Down
30 changes: 30 additions & 0 deletions spec/yazi/renameable_buffer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ describe('the RenameableBuffer class', function()
assert.is_false(rename:matches_exactly('/my-tmp/file2'))
end)

---@param suffix string
local function create_temp_file(suffix)
local file_path = vim.fn.tempname() .. suffix
local file, err = io.open(file_path, 'w')
assert(file ~= nil, 'Failed to create a temporary file ' .. file_path)
assert(
err == nil,
'Failed to create a temporary file ' .. file_path .. ': ' .. (err or '')
)
local _, write_err = file:write('hello')
assert(
write_err == nil,
'Failed to write to a temporary file ' .. file_path
)
file:close()

return file_path
end

it('#focus matches when the file is a symlink', function()
local file1_path = create_temp_file('_file1')
local file2_path = vim.fn.tempname() .. '_file2'

local success, a, b = vim.uv.fs_symlink(file1_path, file2_path)
assert(success, vim.inspect({ 'Failed to create a symlink', a, b }))

local rename = RenameableBuffer.new(1, file1_path)
assert.is_true(rename:matches_exactly(file2_path))
end)

it('renames a file', function()
local rename = RenameableBuffer.new(1, '/my-tmp/file1')
rename:rename('/my-tmp/file2')
Expand Down

0 comments on commit 7a0de28

Please sign in to comment.