From d6da015cd2a6aee19ed798495318e227d8a720c0 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Sun, 18 Aug 2024 10:38:50 +0300 Subject: [PATCH] fix: starting telescope or grug-far when hovering a directory (#388) Issue: Given the following directory structure: - root/ - dir1/ - file1 - file2 When hovering `dir1/` and starting the search or replace operation, the operation would only consider the files in `dir1/`. This is not what the user would expect. The user would expect the operation to consider the files in `root/`. Solution: When hovering a directory, we should consider the directory the hovered directory is in. This is the same behavior as when hovering a file. --- lua/yazi/renameable_buffer.lua | 7 ----- lua/yazi/utils.lua | 13 ++++----- spec/yazi/dir_of_spec.lua | 2 +- spec/yazi/keybinding_helpers_spec.lua | 42 +++++++++++++++------------ 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/lua/yazi/renameable_buffer.lua b/lua/yazi/renameable_buffer.lua index bd1b8f5b..32113aeb 100644 --- a/lua/yazi/renameable_buffer.lua +++ b/lua/yazi/renameable_buffer.lua @@ -61,16 +61,9 @@ function RenameableBuffer:is_sibling_of_hovered(other) other = remove_trailing_slash(other) local utils = require("yazi.utils") - ---@type Path - local other_path = plenary_path:new(other) - local my_dir = utils.dir_of(self.path.filename) local other_dir = utils.dir_of(other) - if other_path:is_dir() then - return my_dir.filename == other_dir:parent().filename - end - return my_dir.filename == other_dir.filename end diff --git a/lua/yazi/utils.lua b/lua/yazi/utils.lua index b824c14d..0ebb7d18 100644 --- a/lua/yazi/utils.lua +++ b/lua/yazi/utils.lua @@ -116,14 +116,13 @@ end function M.dir_of(file_path) ---@type Path local path = plenary_path:new(file_path) - if path:is_dir() then - return path - else - local parent = path:parent() - assert(type(parent) == "table", "parent must be a table") + local parent = path:parent() - return parent - end + -- for some reason, plenary is documented as returning table|unknown[]. we + -- want the table version only + assert(type(parent) == "table", "parent must be a table") + + return parent end -- Returns parsed events from the yazi events file diff --git a/spec/yazi/dir_of_spec.lua b/spec/yazi/dir_of_spec.lua index 202c838e..daf04e31 100644 --- a/spec/yazi/dir_of_spec.lua +++ b/spec/yazi/dir_of_spec.lua @@ -46,7 +46,7 @@ describe("dir_of helper function", function() vim.fn.mkdir(dir) local d = utils.dir_of(dir) - assert.is_equal(dir, d.filename) + assert.is_equal(base_dir, d.filename) end) end) end) diff --git a/spec/yazi/keybinding_helpers_spec.lua b/spec/yazi/keybinding_helpers_spec.lua index dc513bd7..d3d4de9f 100644 --- a/spec/yazi/keybinding_helpers_spec.lua +++ b/spec/yazi/keybinding_helpers_spec.lua @@ -21,7 +21,7 @@ describe("keybinding_helpers", function() keybinding_helpers.grep_in_directory(config, "/tmp") - assert.stub(s).was_called_with("/tmp") + assert.stub(s).was_called_with("/") end) it("should not crash if the integration is disabled", function() @@ -74,22 +74,29 @@ describe("keybinding_helpers", function() end ) - it("when a directory is passed, should replace in the directory", function() - local config = config_module.default() + it( + "when a directory is passed, should replace in the directory the directory is in", + function() + -- when hovering a directory and starting the replace operation, it + -- should not replace in the directory itself. Otherwise starting the + -- replace operation is too confusing + local config = config_module.default() - local stub_replace = stub(config.integrations, "replace_in_directory") + local stub_replace = stub(config.integrations, "replace_in_directory") - keybinding_helpers.replace_in_directory(config, "/tmp") + keybinding_helpers.replace_in_directory(config, "/tmp") - assert.stub(stub_replace).was_called_with(match.is_truthy()) - assert.equals("/tmp", stub_replace.calls[1].vals[1].filename) - end) + assert.stub(stub_replace).was_called_with(match.is_truthy()) + assert.equals("/", stub_replace.calls[1].vals[1].filename) + end + ) end) describe("replace_in_selected_files", function() - it("should call `integrations.replace_in_selected_files`", function() + it("should call the integration if it's available", function() local config = config_module.default() + ---@type Path[] local results = {} config.integrations.replace_in_selected_files = function(paths) results = paths @@ -101,15 +108,14 @@ describe("keybinding_helpers", function() ) assert.equals(2, #results) - assert.are.same( - { "/tmp/file1", "/tmp/file2" }, - vim - .iter(results) - :map(function(a) - return a.filename - end) - :totable() - ) + + local paths = vim + .iter(results) + :map(function(a) + return a.filename + end) + :totable() + assert.same({ "/tmp/file1", "/tmp/file2" }, paths) end) end) end)