Skip to content

Commit

Permalink
feat: add create-from-prompt option
Browse files Browse the repository at this point in the history
Enables/disables the creation of files/folders from the prompt if no
entry is selected.
Defaults to true to preserve existing behavior.
  • Loading branch information
jamestrew committed Apr 23, 2024
1 parent 5ee5002 commit 0399e56
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lua/telescope/_extensions/file_browser/config.lua
Expand Up @@ -51,16 +51,17 @@ _TelescopeFileBrowserConfig = {
return entry and fb_utils.is_dir(entry.Path)
end

local entry_is_nil = function(prompt_bufnr)
local prompt = action_state.get_current_picker(prompt_bufnr):_get_prompt()
local create_from_prompt = function(prompt_bufnr)
local picker = action_state.get_current_picker(prompt_bufnr)
local finder = picker.finder
local prompt = picker:_get_prompt()
local entry = action_state.get_selected_entry()

return entry == nil and #prompt > 0
return entry == nil and #prompt > 0 and finder.create_from_prompt
end

action_set.select:replace_map {
[entry_is_dir] = fb_actions.open_dir,
[entry_is_nil] = fb_actions.create_from_prompt,
[create_from_prompt] = fb_actions.create_from_prompt,
}

return true
Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/_extensions/file_browser/finders.lua
Expand Up @@ -183,6 +183,7 @@ end
---@field dir_icon_hl string: change the highlight group of dir icon (default: "Default")
---@field use_fd boolean: use `fd` if available over `plenary.scandir` (default: true)
---@field git_status boolean: show the git status of files (default: true)
---@field create_from_prompt boolean: Create file/folder from prompt if no entry selected (default: true)
fb_finders.finder = function(opts)
opts = opts or {}
-- cache entries such that multi selections are maintained across {file, folder}_browsers
Expand Down Expand Up @@ -215,6 +216,8 @@ fb_finders.finder = function(opts)
hide_parent_dir = vim.F.if_nil(opts.hide_parent_dir, false),
collapse_dirs = vim.F.if_nil(opts.collapse_dirs, false),
git_status = vim.F.if_nil(opts.git_status, fb_git.find_root(cwd) ~= nil),
create_from_prompt = vim.F.if_nil(opts.create_from_prompt, true),

-- ensure we forward make_entry opts adequately
entry_maker = vim.F.if_nil(opts.entry_maker, function(local_opts)
return fb_make_entry(vim.tbl_extend("force", opts, local_opts))
Expand Down
2 changes: 2 additions & 0 deletions lua/telescope/_extensions/file_browser/picker.lua
Expand Up @@ -81,6 +81,7 @@ local fb_picker = {}
---@field use_fd boolean: use `fd` if available over `plenary.scandir` (default: true)
---@field git_status boolean: show the git status of files (default: true if `git` executable can be found)
---@field prompt_path boolean: Show the current relative path from cwd as the prompt prefix. (default: false)
---@field create_from_prompt boolean: Create file/folder from prompt if no entry selected (default: true)
fb_picker.file_browser = function(opts)
opts = opts or {}

Expand All @@ -99,6 +100,7 @@ fb_picker.file_browser = function(opts)
opts.use_fd = vim.F.if_nil(opts.use_fd, true)
opts.git_status = vim.F.if_nil(opts.git_status, vim.fn.executable "git" == 1)
opts.prompt_path = vim.F.if_nil(opts.prompt_path, false)
opts.create_from_prompt = vim.F.if_nil(opts.create_from_prompt, true)

local select_buffer = opts.select_buffer and opts.files
-- handle case that current buffer is a hidden file
Expand Down

0 comments on commit 0399e56

Please sign in to comment.