Skip to content

Commit d17f47b

Browse files
fdschmidt93Github Actions
andauthored
feat(file_browser): collapse dirs (#159)
* feat: collapse_dirs picker option With { collapse_dirs = true } going from (example from stow) `dotfiles/` to `dotfiles/nvim/.config/nvim/` only requires selecting nvim in `file_browser` as intermediate single-folder levels are skipped. This also works in the reverse direction. * [docgen] Update doc/telescope-file-browser.txt skip-checks: true Co-authored-by: Github Actions <actions@github>
1 parent b5502c6 commit d17f47b

5 files changed

Lines changed: 38 additions & 18 deletions

File tree

doc/telescope-file-browser.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ fb_picker.file_browser({opts}) *telescope-file-browser.picker.file_browser()*
131131
|fb_finders.browse_folders|)
132132
{hide_parent_dir} (boolean) hide `../` in the file browser
133133
(default: false)
134+
{collapse_dirs} (boolean) skip dirs w/ only single
135+
(possibly hidden) sub-dir in
136+
file_browser (default: false)
134137
{quiet} (boolean) surpress any notification from
135138
file_brower actions (default:
136139
false)

lua/telescope/_extensions/file_browser/actions.lua

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,8 @@ local sort_by = function(prompt_bufnr, sorter_fn)
642642
table.insert(entries, e)
643643
end
644644
table.sort(entries, sorter_fn)
645-
current_picker.manager = EntryManager:new(
646-
current_picker.max_results,
647-
current_picker.entry_adder,
648-
current_picker.stats
649-
)
645+
current_picker.manager =
646+
EntryManager:new(current_picker.max_results, current_picker.entry_adder, current_picker.stats)
650647
local index = 1
651648
for _, entry in ipairs(entries) do
652649
current_picker.manager:_append_container(current_picker, { entry, 0 }, true)

lua/telescope/_extensions/file_browser/config.lua

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local fb_actions = require "telescope._extensions.file_browser.actions"
22
local fb_utils = require "telescope._extensions.file_browser.utils"
3+
local scan = require "plenary.scandir"
4+
local Path = require "plenary.path"
35

46
local action_state = require "telescope.actions.state"
57
local action_set = require "telescope.actions.set"
@@ -47,10 +49,27 @@ _TelescopeFileBrowserConfig = {
4749
local entry = action_state.get_selected_entry()
4850
return entry and entry.Path:is_dir()
4951
end, function()
50-
local entry = action_state.get_selected_entry()
51-
local path = vim.loop.fs_realpath(entry.path)
5252
local current_picker = action_state.get_current_picker(prompt_bufnr)
5353
local finder = current_picker.finder
54+
local entry = action_state.get_selected_entry()
55+
local path = vim.loop.fs_realpath(entry.path)
56+
57+
if finder.files and finder.collapse_dirs then
58+
local upwards = path == Path:new(finder.path):parent():absolute()
59+
while true do
60+
local dirs = scan.scan_dir(path, { add_dirs = true, depth = 1, hidden = true })
61+
if #dirs == 1 and vim.fn.isdirectory(dirs[1]) then
62+
path = upwards and Path:new(path):parent():absolute() or dirs[1]
63+
-- make sure it's upper bound (#dirs == 1 implicitly reflects lower bound)
64+
if path == Path:new(path):parent():absolute() then
65+
break
66+
end
67+
else
68+
break
69+
end
70+
end
71+
end
72+
5473
finder.files = true
5574
finder.path = path
5675
fb_utils.redraw_border_title(current_picker)
@@ -101,11 +120,8 @@ end
101120

102121
config.setup = function(opts)
103122
-- TODO maybe merge other keys as well from telescope.config
104-
config.values.mappings = vim.tbl_deep_extend(
105-
"force",
106-
config.values.mappings,
107-
require("telescope.config").values.mappings
108-
)
123+
config.values.mappings =
124+
vim.tbl_deep_extend("force", config.values.mappings, require("telescope.config").values.mappings)
109125
config.values = vim.tbl_deep_extend("force", config.values, opts)
110126

111127
if config.values.hijack_netrw then

lua/telescope/_extensions/file_browser/finders.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ fb_finders.finder = function(opts)
141141
quiet = vim.F.if_nil(opts.quiet, false),
142142
select_buffer = vim.F.if_nil(opts.select_buffer, false),
143143
hide_parent_dir = vim.F.if_nil(opts.hide_parent_dir, false),
144+
collapse_dirs = vim.F.if_nil(opts.collapse_dirs, false),
144145
-- ensure we forward make_entry opts adequately
145146
entry_maker = vim.F.if_nil(opts.entry_maker, function(local_opts)
146147
return fb_make_entry(vim.tbl_extend("force", opts, local_opts))

lua/telescope/_extensions/file_browser/picker.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ local fb_picker = {}
6565
---@field browse_files function: custom override for the file browser (default: |fb_finders.browse_files|)
6666
---@field browse_folders function: custom override for the folder browser (default: |fb_finders.browse_folders|)
6767
---@field hide_parent_dir boolean: hide `../` in the file browser (default: false)
68+
---@field collapse_dirs boolean: skip dirs w/ only single (possibly hidden) sub-dir in file_browser (default: false)
6869
---@field quiet boolean: surpress any notification from file_brower actions (default: false)
6970
---@field dir_icon string: change the icon for a directory (default: )
7071
---@field dir_icon_hl string: change the highlight group of dir icon (default: "Default")
@@ -105,12 +106,14 @@ fb_picker.file_browser = function(opts)
105106
-- end)
106107
end
107108

108-
pickers.new(opts, {
109-
prompt_title = opts.files and "File Browser" or "Folder Browser",
110-
results_title = opts.files and Path:new(opts.path):make_relative(cwd) .. os_sep or "Results",
111-
previewer = conf.file_previewer(opts),
112-
sorter = conf.file_sorter(opts),
113-
}):find()
109+
pickers
110+
.new(opts, {
111+
prompt_title = opts.files and "File Browser" or "Folder Browser",
112+
results_title = opts.files and Path:new(opts.path):make_relative(cwd) .. os_sep or "Results",
113+
previewer = conf.file_previewer(opts),
114+
sorter = conf.file_sorter(opts),
115+
})
116+
:find()
114117
end
115118

116119
return fb_picker.file_browser

0 commit comments

Comments
 (0)