Skip to content

Commit

Permalink
Path separator opens directory when dir name is in input (#313)
Browse files Browse the repository at this point in the history
* Path separator acts as "select" when directory is selected

* Add docs for path separator
  • Loading branch information
CKolkey authored Sep 18, 2023
1 parent bec4b0f commit c0e2135
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
49 changes: 49 additions & 0 deletions lua/telescope/_extensions/file_browser/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
39 changes: 3 additions & 36 deletions lua/telescope/_extensions/file_browser/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ _TelescopeFileBrowserConfig = {
["<C-h>"] = fb_actions.toggle_hidden,
["<C-s>"] = fb_actions.toggle_all,
["<bs>"] = fb_actions.backspace,
[Path.path.sep] = fb_actions.path_separator,
},
["n"] = {
["c"] = fb_actions.create,
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lua/telescope/_extensions/file_browser/picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ local fb_picker = {}
--- List, create, delete, rename, or move files and folders of your cwd.<br>
--- Notes
--- - Default keymaps in insert/normal mode:
--- - `<cr>`: opens the currently selected file, or navigates to the currently selected directory
--- - `<cr>` : 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 `<cr>`.
--- - `<A-c>/c`: Create file/folder at current `path` (trailing path separator creates folder)
--- - `<A-r>/r`: Rename multi-selected files/folders
--- - `<A-m>/m`: Move multi-selected files/folders to current `path`
Expand Down

0 comments on commit c0e2135

Please sign in to comment.