Skip to content

Commit 4bd5657

Browse files
authored
fix(action): prevent collapse_dirs from trying to open file (#351)
Previously, opening a directory with one file with `collapse_dirs` set to true would result in the `open_dir` action trying to refresh the picker with that file as the new path. This caused `fd` to error out since a file isn't a valid folder. This was caused by a bad truthy check. Corrected this behavior to open folders regularly with `collapse_dirs` if the selected folder only contains one file. closes #348
1 parent 8e05433 commit 4bd5657

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

lua/telescope/_extensions/file_browser/actions.lua

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -826,42 +826,57 @@ fb_actions.path_separator = function(prompt_bufnr)
826826
local dir = Path:new(current_picker.finder.path .. os_sep .. current_picker:_get_prompt() .. os_sep)
827827

828828
if current_picker.finder.files and dir:exists() and dir:is_dir() then
829-
fb_actions.open_dir(prompt_bufnr, dir.filename)
829+
fb_actions.open_dir(prompt_bufnr, nil, dir.filename)
830830
else
831831
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(os_sep, true, false, true), "tn", false)
832832
end
833833
end
834834

835-
fb_actions.open_dir = function(prompt_bufnr, dir)
836-
local current_picker = action_state.get_current_picker(prompt_bufnr)
837-
local finder = current_picker.finder
838-
local entry = action_state.get_selected_entry()
835+
---get directory path to open based on `collapse_dirs` options
836+
---@param finder any
837+
---@param path string
838+
---@param upward boolean whether to "cd" upwards
839+
---@return string? #path string
840+
local function open_dir_path(finder, path, upward)
841+
path = vim.loop.fs_realpath(path) or ""
842+
if path == "" then
843+
return
844+
end
839845

840-
if not vim.loop.fs_access(entry.path, "X") then
846+
if not vim.loop.fs_access(path, "X") then
841847
fb_utils.notify("select", { level = "WARN", msg = "Permission denied" })
842848
return
843849
end
844850

845-
local path = vim.loop.fs_realpath(dir:match("^" .. os_sep) and dir or entry.path)
851+
if not finder.files or not finder.collapse_dirs then
852+
return path
853+
end
846854

847-
if finder.files and finder.collapse_dirs then
848-
local upwards = path == Path:new(finder.path):parent():absolute()
849-
while true do
850-
local dirs = scan.scan_dir(path, { add_dirs = true, depth = 1, hidden = true })
851-
if #dirs == 1 and vim.fn.isdirectory(dirs[1]) then
852-
path = upwards and Path:new(path):parent():absolute() or dirs[1]
853-
-- make sure it's upper bound (#dirs == 1 implicitly reflects lower bound)
854-
if path == Path:new(path):parent():absolute() then
855-
break
856-
end
857-
else
858-
break
859-
end
855+
while true do
856+
local dirs = scan.scan_dir(path, { add_dirs = true, depth = 1, hidden = true })
857+
if #dirs == 1 and vim.fn.isdirectory(dirs[1]) == 1 then
858+
path = upward and Path:new(path):parent():absolute() or dirs[1]
859+
else
860+
break
860861
end
861862
end
863+
return path
864+
end
865+
866+
---comment open directory and refresh picker
867+
---@param prompt_bufnr integer
868+
---@param _ any select type
869+
---@param dir string? priority dir path
870+
fb_actions.open_dir = function(prompt_bufnr, _, dir)
871+
local current_picker = action_state.get_current_picker(prompt_bufnr)
872+
local finder = current_picker.finder
873+
local entry = action_state.get_selected_entry()
874+
875+
local path = dir or entry.path
876+
local upward = path == Path:new(finder.path):parent():absolute()
862877

863878
finder.files = true
864-
finder.path = path
879+
finder.path = open_dir_path(finder, path, upward)
865880
fb_utils.redraw_border_title(current_picker)
866881
current_picker:refresh(
867882
finder,

lua/telescope/_extensions/file_browser/config.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local fb_actions = require "telescope._extensions.file_browser.actions"
2+
local fb_utils = require "telescope._extensions.file_browser.utils"
23
local Path = require "plenary.path"
34

45
local action_state = require "telescope.actions.state"
@@ -47,7 +48,7 @@ _TelescopeFileBrowserConfig = {
4748
attach_mappings = function(_, _)
4849
local entry_is_dir = function()
4950
local entry = action_state.get_selected_entry()
50-
return entry and entry.Path:is_dir()
51+
return entry and fb_utils.is_dir(entry.Path)
5152
end
5253

5354
local entry_is_nil = function(prompt_bufnr)

0 commit comments

Comments
 (0)