Skip to content

Commit

Permalink
fix: starting telescope or grug-far when hovering a directory (#388)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikavilpas authored Aug 18, 2024
1 parent bdc33d0 commit d6da015
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
7 changes: 0 additions & 7 deletions lua/yazi/renameable_buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 6 additions & 7 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/yazi/dir_of_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 24 additions & 18 deletions spec/yazi/keybinding_helpers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)

0 comments on commit d6da015

Please sign in to comment.