From a5c31e35c4a5522a55ff518d5c7c3f55234f0883 Mon Sep 17 00:00:00 2001 From: Fabian David Schmidt <fabian.david.schmidt@gmail.com> Date: Mon, 17 Jan 2022 15:18:50 +0100 Subject: [PATCH 1/3] feat: enable customization of folder browser --- README.md | 26 +++++++++-------- lua/telescope/_extensions/file_browser.lua | 28 +++++++++++++++++-- .../_extensions/file_browser/finders.lua | 12 ++++++-- .../_extensions/file_browser/picker.lua | 1 + 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2f8eb82d..8312a138 100644 --- a/README.md +++ b/README.md @@ -30,21 +30,25 @@ Plug 'nvim-telescope/telescope-file-browser.nvim' You can configure the `telescope-file-browser` like any other `telescope.nvim` picker. Please see `:h telescope-file-browser.picker` for the full set of options dedicated to the picker. For instance, you of course can map `theme` and [mappings](#remappings) as you are used to from `telescope.nvim`. +The setup below shows the defaults. You do not need to set any of the `file_browser` configuration. `require("telescope").load_extension "file_browser"` suffices for the default setup. + ```lua --- You don't need to set any of these options. --- IMPORTANT!: this is only a showcase of how you can set default options! require("telescope").setup { extensions = { file_browser = { - theme = "ivy", - mappings = { - ["i"] = { - -- your custom insert mode mappings - }, - ["n"] = { - -- your custom normal mode mappings - }, - }, + theme = "ivy", + mappings = {} -- See mappings section + files = true, -- Start in file_browser mode + hidden = false, + respect_gitignore = false, -- expensive with plenary, true if `fd` found +---@field files boolean: start in file (true) or folder (false) browser (default: true) +---@field add_dirs boolean: whether the file browser shows folders (default: true) +---@field depth number: file tree depth to display, `false` for unlimited depth (default: 1) +---@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 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|) }, }, } 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..d79b7744 100644 --- a/lua/telescope/_extensions/file_browser/finders.lua +++ b/lua/telescope/_extensions/file_browser/finders.lua @@ -81,11 +81,15 @@ fb_finders.browse_folders = function(opts) -- returns copy with properly set cwd for entry maker 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" } + if has_fd == 0 then + 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..cf35f510 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) +---@filed 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) From f42c3de458a51bab6f0549a45ce88938dfe556a0 Mon Sep 17 00:00:00 2001 From: Fabian David Schmidt <fabian.david.schmidt@gmail.com> Date: Mon, 17 Jan 2022 16:11:53 +0100 Subject: [PATCH 2/3] fix: clean up diff --- README.md | 26 ++++++++----------- .../_extensions/file_browser/finders.lua | 2 +- .../_extensions/file_browser/picker.lua | 2 +- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8312a138..2f8eb82d 100644 --- a/README.md +++ b/README.md @@ -30,25 +30,21 @@ Plug 'nvim-telescope/telescope-file-browser.nvim' You can configure the `telescope-file-browser` like any other `telescope.nvim` picker. Please see `:h telescope-file-browser.picker` for the full set of options dedicated to the picker. For instance, you of course can map `theme` and [mappings](#remappings) as you are used to from `telescope.nvim`. -The setup below shows the defaults. You do not need to set any of the `file_browser` configuration. `require("telescope").load_extension "file_browser"` suffices for the default setup. - ```lua +-- You don't need to set any of these options. +-- IMPORTANT!: this is only a showcase of how you can set default options! require("telescope").setup { extensions = { file_browser = { - theme = "ivy", - mappings = {} -- See mappings section - files = true, -- Start in file_browser mode - hidden = false, - respect_gitignore = false, -- expensive with plenary, true if `fd` found ----@field files boolean: start in file (true) or folder (false) browser (default: true) ----@field add_dirs boolean: whether the file browser shows folders (default: true) ----@field depth number: file tree depth to display, `false` for unlimited depth (default: 1) ----@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 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|) + theme = "ivy", + mappings = { + ["i"] = { + -- your custom insert mode mappings + }, + ["n"] = { + -- your custom normal mode mappings + }, + }, }, }, } diff --git a/lua/telescope/_extensions/file_browser/finders.lua b/lua/telescope/_extensions/file_browser/finders.lua index d79b7744..639127b2 100644 --- a/lua/telescope/_extensions/file_browser/finders.lua +++ b/lua/telescope/_extensions/file_browser/finders.lua @@ -81,7 +81,7 @@ fb_finders.browse_folders = function(opts) -- returns copy with properly set cwd for entry maker local cwd = opts.cwd_to_path and opts.path or opts.cwd local entry_maker = opts.entry_maker { cwd = cwd } - if has_fd == 0 then + if has_fd then local args = { "-a" } if opts.hidden then table.insert(args, "-H") diff --git a/lua/telescope/_extensions/file_browser/picker.lua b/lua/telescope/_extensions/file_browser/picker.lua index cf35f510..66eba324 100644 --- a/lua/telescope/_extensions/file_browser/picker.lua +++ b/lua/telescope/_extensions/file_browser/picker.lua @@ -52,7 +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) ----@filed types table: table incl. {"file", "directory"}, (default: { "directory" }) +---@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) From 42310aa9aca0e3ced3d999376264521fe8c9abc4 Mon Sep 17 00:00:00 2001 From: Github Actions <actions@github> Date: Mon, 17 Jan 2022 15:12:29 +0000 Subject: [PATCH 3/3] [docgen] Update doc/telescope-file-browser.txt skip-checks: true --- doc/telescope-file-browser.txt | 2 ++ 1 file changed, 2 insertions(+) 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