Open netraular opened 3 weeks ago
To address the feature request of including all files within a specified folder using patterns, we need to update the codebase to support glob patterns. This will allow users to specify patterns like home/*.php
or home/*
to include multiple files at once. The solution involves updating the README.md
to document the new feature and modifying the src/extension.ts
to implement the pattern matching logic.
README.md
We need to update the README.md
file to explain the new functionality. This will help users understand how to use the new feature effectively.
README.md
# multi-file-code-to-ai README
Select multiple files and convert them to a prompt for chatGPT, claude, or deepseek.
## Features
- Manage multiple file lists.
- Swap between different file lists.
- Automatically update prompt with selected files.
- Save the prompts with the workspace in version control.
- **New:** Include all files in a specified folder using patterns.
## How to use
1. Open the prompt explorer.
2. Click on the `+` button to create a new prompt, type a name and instructions.
3. Use the file explorer to copy the relative paths of the files you want to include in the prompt.
4. Paste the paths into the prompt under the `Files` section.
5. Click on the generate button on the prompt in the prompt explorer to generate the prompt.
### Including All Files in a Folder
You can now include all files within a specified folder by using patterns. This feature simplifies the process of including multiple files at once.
#### Examples:
- To include all PHP files in the `home` directory:
```yaml
files:
- home/*.php
home
directory:
files:
- home/*
name: New prompt
instruction: Example instructions
files:
- src/app.ts
- src/server.ts
- src/views/index.ts
- home/*.php # Example of including all PHP files in the home directory
- home/* # Example of including all files in the home directory
src/extension.ts
To implement the feature that allows users to include all files within a specified folder using patterns, we need to modify the src/extension.ts
file.
Update the Prompt
Interface:
Prompt
interface can handle patterns for file inclusion.Modify the addNewPrompt
Function:
Implement Pattern Matching Logic:
Update the generatePrompt
Function:
Prompt
InterfaceEnsure the Prompt
interface can handle patterns for file inclusion:
interface Prompt {
name: string;
instruction: string;
files: string[];
patterns?: string[];
}
addNewPrompt
FunctionAllow users to specify patterns when adding a new prompt:
async function addNewPrompt(provider: PromptsProvider) {
const name = await vscode.window.showInputBox({ prompt: 'Enter a name for the new prompt' });
if (!name) return;
const instruction = await vscode.window.showInputBox({ prompt: 'Enter the instruction for the prompt' });
if (!instruction) return;
const patternsInput = await vscode.window.showInputBox({ prompt: 'Enter file patterns (comma-separated, e.g., "home/*.php, home/*")' });
const patterns = patternsInput ? patternsInput.split(',').map(p => p.trim()) : [];
const newPrompt: Prompt = { name, instruction, files: [], patterns };
prompts.push(newPrompt);
await savePrompt(newPrompt);
provider.refresh();
editPrompt(name, provider);
}
Use glob patterns to match files in the specified directories:
import * as glob from 'glob';
function resolvePatterns(patterns: string[], rootPath: string): string[] {
let matchedFiles: string[] = [];
for (const pattern of patterns) {
const files = glob.sync(pattern, { cwd: rootPath });
matchedFiles = matchedFiles.concat(files);
}
return matchedFiles;
}
generatePrompt
FunctionResolve the patterns to actual file paths before generating the prompt:
async function generatePrompt(item: PromptItem) {
const prompt = prompts.find(p => p.name === item.label);
if (!prompt) return;
const workspaceRoot = vscode.workspace.workspaceFolders?.[0].uri.fsPath || '';
let generatedPrompt = `# ${prompt.name}\n\n## Instructions\n\n${prompt.instruction}\n\n## Files\n\n`;
const files = resolvePatterns(prompt.patterns || [], workspaceRoot);
for (const file of files) {
generatedPrompt += `### ${file}\n\n`;
try {
const absolutePath = path.join(workspaceRoot, file);
const content = fs.readFileSync(absolutePath, 'utf8');
generatedPrompt += `\`\`\`\n${content}\n\`\`\`\n\n`;
} catch (err) {
console.error(`Error reading file ${file}: ${err}`);
generatedPrompt += `Error reading file: ${err}\n\n`;
}
}
generatedPrompt += "---\n\nEnd of prompt";
vscode.env.clipboard.writeText(generatedPrompt);
vscode.window.showInformationMessage('Prompt generated and copied to clipboard!');
}
By following these steps, we have updated the src/extension.ts
file to include the new feature that allows users to specify patterns for including multiple files from a folder. This enhancement simplifies the process of managing and including multiple files at once.
Click here to create a Pull Request with the proposed solution
Files used for this task:
I would like to propose a new feature that allows users to include all files within a specified folder. This functionality would be particularly useful for managing files in a more efficient manner. For instance, users could specify patterns such as:
home/*.php
to include all PHP files in the home directory.home/*
to include all files in the home directory.This enhancement would simplify the process of including multiple files at once, reducing the need for individual file inclusions.
Thank you for considering this request.