diff --git a/doc/telescope-file-browser.txt b/doc/telescope-file-browser.txt index fc234d1f..3eb3d378 100644 --- a/doc/telescope-file-browser.txt +++ b/doc/telescope-file-browser.txt @@ -99,6 +99,8 @@ fb_picker.file_browser({opts}) *fb_picker.file_browser()* {respect_gitignore} (boolean) induces slow-down w/ plenary finder (default: false, true if `fd` available) + {types} (table) table incl. {"file", "directory"}, + (default: { "directory" }) {browse_files} (function) custom override for the file browser (default: |fb_finders.browse_files|) {browse_folders} (function) custom override for the folder browser diff --git a/lua/telescope/_extensions/file_browser.lua b/lua/telescope/_extensions/file_browser.lua index 66a83a72..00d48df6 100644 --- a/lua/telescope/_extensions/file_browser.lua +++ b/lua/telescope/_extensions/file_browser.lua @@ -94,15 +94,39 @@ local pconf = { 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() + local current_picker = action_state.get_current_picker(prompt_bufnr) + local finder = current_picker.finder + return entry and (entry.Path:is_dir() or finder.files == false) end, function() - local path = vim.loop.fs_realpath(action_state.get_selected_entry().path) + local entry = action_state.get_selected_entry() + local is_dir = entry.Path:is_dir() + local path = is_dir and entry.Path:absolute() or entry.Path:parent():absolute() local current_picker = action_state.get_current_picker(prompt_bufnr) local finder = current_picker.finder + finder.files = true finder.path = path fb_utils.redraw_border_title(current_picker) current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi }) + if not is_dir then + vim.schedule(function() + local index = 1 + for e in current_picker.manager:iter() do + if index > current_picker.max_results then + break + end + if e.value == entry.value then + vim.schedule(function() + vim.schedule(function() + current_picker:set_selection(current_picker:get_row(index)) + end) + end) + break + end + index = index + 1 + end + end) + end end) return true end, diff --git a/lua/telescope/_extensions/file_browser/finders.lua b/lua/telescope/_extensions/file_browser/finders.lua index a9686ee7..639127b2 100644 --- a/lua/telescope/_extensions/file_browser/finders.lua +++ b/lua/telescope/_extensions/file_browser/finders.lua @@ -82,10 +82,14 @@ fb_finders.browse_folders = function(opts) local cwd = opts.cwd_to_path and opts.path or opts.cwd local entry_maker = opts.entry_maker { cwd = cwd } if has_fd then - local args = { "-t", "d", "-a" } + local args = { "-a" } if opts.hidden then table.insert(args, "-H") end + for _, type_ in ipairs(opts.types) do + table.insert(args, "-t") + table.insert(args, type_) + end if opts.respect_gitignore == false then table.insert(args, "--no-ignore-vcs") end @@ -100,7 +104,8 @@ fb_finders.browse_folders = function(opts) else local data = scan.scan_dir(opts.cwd, { hidden = opts.hidden, - only_dirs = true, + add_dirs = vim.tbl_contains(opts.types, "directory"), + only_dirs = not vim.tbl_contains(opts.types, "file"), respect_gitignore = opts.respect_gitignore, }) table.insert(data, 1, opts.cwd) @@ -135,6 +140,7 @@ fb_finders.finder = function(opts) files = vim.F.if_nil(opts.files, true), -- file or folders mode grouped = vim.F.if_nil(opts.grouped, false), -- ensure we forward make_entry opts adequately + types = vim.F.if_nil(opts.types, { "directory" }), entry_maker = vim.F.if_nil(opts.entry_maker, function(local_opts) return fb_make_entry(vim.tbl_extend("force", opts, local_opts)) end), diff --git a/lua/telescope/_extensions/file_browser/picker.lua b/lua/telescope/_extensions/file_browser/picker.lua index 49e610d5..66eba324 100644 --- a/lua/telescope/_extensions/file_browser/picker.lua +++ b/lua/telescope/_extensions/file_browser/picker.lua @@ -52,6 +52,7 @@ local fb_picker = {} ---@field dir_icon string: change the icon for a directory (default: ) ---@field hidden boolean: determines whether to show hidden files or not (default: false) ---@field respect_gitignore boolean: induces slow-down w/ plenary finder (default: false, true if `fd` available) +---@field types table: table incl. {"file", "directory"}, (default: { "directory" }) ---@field browse_files function: custom override for the file browser (default: |fb_finders.browse_files|) ---@field browse_folders function: custom override for the folder browser (default: |fb_finders.browse_folders|) fb_picker.file_browser = function(opts)