Skip to content

Commit

Permalink
feat: added support for creating multiple files using pattern {file1,…
Browse files Browse the repository at this point in the history
…file2,file3}.ext

- Added functionality to create multiple files at once using the pattern {file1,file2,file3}.ext
- Updated both action.create and action.create_from_prompt to use create_items for handling multiple files
- Users can now generate multiple files in one go, e.g., {index,about,contact}.html creates index.html, about.html, and contact.html.
  • Loading branch information
Mirsmog committed Oct 1, 2024
1 parent 3b8a1e1 commit 2089cb6
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions lua/telescope/_extensions/file_browser/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ local create = function(file, finder)
return file
end

local function create_items(input, finder)
if not input then
return
end
local prefix, middle, suffix = input:match "^(.-){(.-)}(.-)$"
local files = {}

if not (prefix and middle and suffix) then
local file = create(input, finder)
if file then
table.insert(files, file)
end
return files
end

local file_names = vim.split(middle, ",", { plain = true })
for _, file_name in ipairs(file_names) do
local resolved_path = prefix .. file_name .. suffix
local file = create(resolved_path, finder)
if file then
table.insert(files, file)
end
end

return files
end

local function newly_created_root(path, cwd)
local idx
local parents = path:parents()
Expand Down Expand Up @@ -149,11 +176,13 @@ fb_actions.create = function(prompt_bufnr)
local base_dir = get_target_dir(finder) .. os_sep
get_input({ prompt = "Create: ", default = base_dir, completion = "file" }, function(input)
vim.cmd [[ redraw ]] -- redraw to clear out vim.ui.prompt to avoid hit-enter prompt
local file = create(input, finder)
if file then
local selection_path = newly_created_root(file, base_dir)
if selection_path then
fb_utils.selection_callback(current_picker, selection_path)
local files = create_items(input, finder)
if files and #files > 0 then
for _, file in pairs(files) do
local selection_path = newly_created_root(file, base_dir)
if selection_path then
fb_utils.selection_callback(current_picker, selection_path)
end
end
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
end
Expand All @@ -168,12 +197,12 @@ fb_actions.create_from_prompt = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
local finder = current_picker.finder
local input = (finder.files and finder.path or finder.cwd) .. os_sep .. current_picker:_get_prompt()
local file = create(input, finder)
if file then
-- pretend new file path is entry
local path = file:absolute()
state.set_global_key("selected_entry", { path, value = path, path = path, Path = file })
-- select as if were proper entry to support eg changing into created folder
local files = create_items(input, finder)
if files and #files > 0 then
for _, file in ipairs(files) do
local path = file:absolute()
state.set_global_key("selected_entry", { path, value = path, path = path, Path = file })
end
action_set.select(prompt_bufnr, "default")
end
end
Expand Down

0 comments on commit 2089cb6

Please sign in to comment.