diff --git a/lua/telescope/_extensions/file_browser/actions.lua b/lua/telescope/_extensions/file_browser/actions.lua index 4e865dc2..d260f612 100644 --- a/lua/telescope/_extensions/file_browser/actions.lua +++ b/lua/telescope/_extensions/file_browser/actions.lua @@ -40,6 +40,7 @@ local transform_mod = require("telescope.actions.mt").transform_mod local Path = require "plenary.path" local popup = require "plenary.popup" +local scan = require "plenary.scandir" local fb_actions = setmetatable({}, { __index = function(_, k) @@ -780,5 +781,53 @@ fb_actions.backspace = function(prompt_bufnr, bypass) end end +fb_actions.path_separator = function(prompt_bufnr) + local current_picker = action_state.get_current_picker(prompt_bufnr) + local dir = Path:new(current_picker.finder.path .. os_sep .. current_picker:_get_prompt() .. os_sep) + + if dir:exists() and dir:is_dir() then + fb_actions.open_dir(prompt_bufnr, dir.filename) + else + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(os_sep, true, false, true), "tn", false) + end +end + +fb_actions.open_dir = function(prompt_bufnr, dir) + local current_picker = action_state.get_current_picker(prompt_bufnr) + local finder = current_picker.finder + local entry = action_state.get_selected_entry() + + if not vim.loop.fs_access(entry.path, "X") then + fb_utils.notify("select", { level = "WARN", msg = "Permission denied" }) + return + end + + local path = vim.loop.fs_realpath(dir:match("^" .. os_sep) and dir or entry.path) + + if finder.files and finder.collapse_dirs then + local upwards = path == Path:new(finder.path):parent():absolute() + while true do + local dirs = scan.scan_dir(path, { add_dirs = true, depth = 1, hidden = true }) + if #dirs == 1 and vim.fn.isdirectory(dirs[1]) then + path = upwards and Path:new(path):parent():absolute() or dirs[1] + -- make sure it's upper bound (#dirs == 1 implicitly reflects lower bound) + if path == Path:new(path):parent():absolute() then + break + end + else + break + end + end + end + + finder.files = true + finder.path = path + fb_utils.redraw_border_title(current_picker) + current_picker:refresh( + finder, + { new_prefix = fb_utils.relative_path_prefix(finder), reset_prompt = true, multi = current_picker._multi } + ) +end + fb_actions = transform_mod(fb_actions) return fb_actions diff --git a/lua/telescope/_extensions/file_browser/config.lua b/lua/telescope/_extensions/file_browser/config.lua index 7c70480a..ef002f20 100644 --- a/lua/telescope/_extensions/file_browser/config.lua +++ b/lua/telescope/_extensions/file_browser/config.lua @@ -27,6 +27,7 @@ _TelescopeFileBrowserConfig = { [""] = fb_actions.toggle_hidden, [""] = fb_actions.toggle_all, [""] = fb_actions.backspace, + [Path.path.sep] = fb_actions.path_separator, }, ["n"] = { ["c"] = fb_actions.create, @@ -44,47 +45,13 @@ _TelescopeFileBrowserConfig = { ["s"] = fb_actions.toggle_all, }, }, - attach_mappings = function(prompt_bufnr, _) + attach_mappings = function() action_set.select:replace_if(function() -- test whether selected entry is directory local entry = action_state.get_selected_entry() return entry and entry.Path:is_dir() - end, function() - local current_picker = action_state.get_current_picker(prompt_bufnr) - local finder = current_picker.finder - local entry = action_state.get_selected_entry() - - if not vim.loop.fs_access(entry.path, "X") then - fb_utils.notify("select", { level = "WARN", msg = "Permission denied" }) - return - end - - local path = vim.loop.fs_realpath(entry.path) - - if finder.files and finder.collapse_dirs then - local upwards = path == Path:new(finder.path):parent():absolute() - while true do - local dirs = scan.scan_dir(path, { add_dirs = true, depth = 1, hidden = true }) - if #dirs == 1 and vim.fn.isdirectory(dirs[1]) then - path = upwards and Path:new(path):parent():absolute() or dirs[1] - -- make sure it's upper bound (#dirs == 1 implicitly reflects lower bound) - if path == Path:new(path):parent():absolute() then - break - end - else - break - end - end - end + end, fb_actions.open_dir) - finder.files = true - finder.path = path - fb_utils.redraw_border_title(current_picker) - current_picker:refresh( - finder, - { new_prefix = fb_utils.relative_path_prefix(finder), reset_prompt = true, multi = current_picker._multi } - ) - end) return true end, } or _TelescopeFileBrowserConfig diff --git a/lua/telescope/_extensions/file_browser/picker.lua b/lua/telescope/_extensions/file_browser/picker.lua index 66092a29..e2912c6e 100644 --- a/lua/telescope/_extensions/file_browser/picker.lua +++ b/lua/telescope/_extensions/file_browser/picker.lua @@ -30,7 +30,8 @@ local fb_picker = {} --- List, create, delete, rename, or move files and folders of your cwd.
--- Notes --- - Default keymaps in insert/normal mode: ---- - ``: opens the currently selected file, or navigates to the currently selected directory +--- - `` : Opens the currently selected file, or navigates to the currently selected directory +--- - `/`, `\` : (OS Path separator) When typing filepath, the path separator will open a directory like ``. --- - `/c`: Create file/folder at current `path` (trailing path separator creates folder) --- - `/r`: Rename multi-selected files/folders --- - `/m`: Move multi-selected files/folders to current `path`