From 2089cb68ffd6e2585a878bd0b24f66ef1e2817b0 Mon Sep 17 00:00:00 2001 From: zwine Date: Tue, 1 Oct 2024 09:32:45 +0500 Subject: [PATCH] feat: added support for creating multiple files using pattern {file1,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. --- .../_extensions/file_browser/actions.lua | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/lua/telescope/_extensions/file_browser/actions.lua b/lua/telescope/_extensions/file_browser/actions.lua index 929dcec..9411426 100755 --- a/lua/telescope/_extensions/file_browser/actions.lua +++ b/lua/telescope/_extensions/file_browser/actions.lua @@ -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() @@ -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 @@ -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