From 7a0de28970da3af471d1f8c592b11d2585429497 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sun, 14 Jul 2024 21:25:09 +0300 Subject: [PATCH] fix: symlinked files cannot be highlighted when hovered 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 https://github.com/mikavilpas/yazi.nvim/issues/195 --- lua/yazi/renameable_buffer.lua | 6 ++++-- spec/yazi/renameable_buffer_spec.lua | 30 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lua/yazi/renameable_buffer.lua b/lua/yazi/renameable_buffer.lua index 6723ea95..cfa42c18 100644 --- a/lua/yazi/renameable_buffer.lua +++ b/lua/yazi/renameable_buffer.lua @@ -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 diff --git a/spec/yazi/renameable_buffer_spec.lua b/spec/yazi/renameable_buffer_spec.lua index 2558ea08..6016ab52 100644 --- a/spec/yazi/renameable_buffer_spec.lua +++ b/spec/yazi/renameable_buffer_spec.lua @@ -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')