Skip to content

Commit

Permalink
feat: create & open file/folder from prompt if results empty (#117)
Browse files Browse the repository at this point in the history
If a prompt results in no file/folder,
create it from prompt and open it as if it were selected
with "Enter", thus, in case of
- file: file opened in current buffer
- folder: folder opened in telescope-file-browser
  • Loading branch information
fdschmidt93 authored Mar 25, 2022
1 parent 46aba2f commit c6f5104
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions lua/telescope/_extensions/file_browser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ local fb_utils = require "telescope._extensions.file_browser.utils"

local action_state = require "telescope.actions.state"
local action_set = require "telescope.actions.set"
local state = require "telescope.state"
local Path = require "plenary.path"

local pconf = {
mappings = {
Expand Down Expand Up @@ -94,15 +96,38 @@ 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()
end, function()
local path = vim.loop.fs_realpath(action_state.get_selected_entry().path)
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 })
return (finder.files and entry == nil) or (entry and entry.Path:is_dir())
end, function()
local entry = action_state.get_selected_entry()
if entry and entry.Path:is_dir() then
local path = vim.loop.fs_realpath(entry.path)
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 })
else
-- Create file from prompt
-- TODO notification about created file once PR lands
local current_picker = action_state.get_current_picker(prompt_bufnr)
local finder = current_picker.finder
if finder.files then
local file = Path:new { finder.path, current_picker:_get_prompt() }
if not fb_utils.is_dir(file.filename) then
file:touch { parents = true }
else
Path:new(file.filename:sub(1, -2)):mkdir { parents = true }
end
local path = file:absolute()
-- pretend new file path is entry
state.set_global_key("selected_entry", { path = path, filename = path, Path = file })
-- select as if were proper entry to support eg changing into created folder
action_set.select(prompt_bufnr, "default")
end
end
end)
return true
end,
Expand Down

0 comments on commit c6f5104

Please sign in to comment.