add support for recursive selection and deselection of files in the tree view

This commit is contained in:
2025-03-12 07:25:26 +00:00
parent 65bf168e1f
commit 2d28f71e9b
5 changed files with 473 additions and 39 deletions

View File

@@ -94,16 +94,92 @@ export class PrompterTreeProvider implements vscode.TreeDataProvider<FileTreeIte
// Add a file to the selection
addToSelection(item: FileTreeItem): void {
if (item.resourceUri) {
this.selectedFiles.add(item.resourceUri.fsPath);
console.log(`Added ${item.resourceUri.fsPath} to selection`);
const filePath = item.resourceUri.fsPath;
// Check if it's a directory
if (item.collapsibleState === vscode.TreeItemCollapsibleState.Collapsed ||
item.collapsibleState === vscode.TreeItemCollapsibleState.Expanded) {
// It's a directory, recursively add all files
this.addDirectoryToSelection(filePath);
} else {
// It's a file, just add it
this.selectedFiles.add(filePath);
console.log(`Added ${filePath} to selection`);
}
}
}
// Recursively add all files in a directory to the selection
private async addDirectoryToSelection(dirPath: string): Promise<void> {
try {
// Add the directory itself
this.selectedFiles.add(dirPath);
console.log(`Added directory ${dirPath} to selection`);
// Read directory contents
const files = await vscode.workspace.fs.readDirectory(vscode.Uri.file(dirPath));
// Process each item
for (const [name, type] of files) {
const filePath = path.join(dirPath, name);
if (type === vscode.FileType.Directory) {
// Recursively process subdirectories
await this.addDirectoryToSelection(filePath);
} else {
// Add files
this.selectedFiles.add(filePath);
console.log(`Added ${filePath} to selection (from directory)`);
}
}
} catch (error) {
console.error(`Error adding directory to selection: ${dirPath}`, error);
}
}
// Remove a file from the selection
removeFromSelection(item: FileTreeItem): void {
if (item.resourceUri) {
this.selectedFiles.delete(item.resourceUri.fsPath);
console.log(`Removed ${item.resourceUri.fsPath} from selection`);
const filePath = item.resourceUri.fsPath;
// Check if it's a directory
if (item.collapsibleState === vscode.TreeItemCollapsibleState.Collapsed ||
item.collapsibleState === vscode.TreeItemCollapsibleState.Expanded) {
// It's a directory, recursively remove all files
this.removeDirectoryFromSelection(filePath);
} else {
// It's a file, just remove it
this.selectedFiles.delete(filePath);
console.log(`Removed ${filePath} from selection`);
}
}
}
// Recursively remove all files in a directory from the selection
private async removeDirectoryFromSelection(dirPath: string): Promise<void> {
try {
// Remove the directory itself
this.selectedFiles.delete(dirPath);
console.log(`Removed directory ${dirPath} from selection`);
// Read directory contents
const files = await vscode.workspace.fs.readDirectory(vscode.Uri.file(dirPath));
// Process each item
for (const [name, type] of files) {
const filePath = path.join(dirPath, name);
if (type === vscode.FileType.Directory) {
// Recursively process subdirectories
await this.removeDirectoryFromSelection(filePath);
} else {
// Remove files
this.selectedFiles.delete(filePath);
console.log(`Removed ${filePath} from selection (from directory)`);
}
}
} catch (error) {
console.error(`Error removing directory from selection: ${dirPath}`, error);
}
}