From 926c88780b7b9722cd9ca48ab8c7055cb8d2dc2f Mon Sep 17 00:00:00 2001 From: Abhishek Date: Wed, 12 Mar 2025 08:01:18 +0000 Subject: [PATCH] Assistant checkpoint: Refactored prompterTreeProvider.ts into modular components Assistant generated file changes: - src/providers/prompterTreeProvider.ts: Refactor to a more modular structure - src/providers/fileSelectionManager.ts: Create file selection manager class - src/providers/settingsManager.ts: Create settings manager class --- User prompt: Can you help me make this file more modular? @src/providers/prompterTreeProvider.ts Replit-Commit-Author: Assistant Replit-Commit-Session-Id: e2f69f78-99c3-447b-a97a-9a2c4347a1d6 --- src/providers/fileSelectionManager.ts | 103 ++++++++++++++++++ src/providers/prompterTreeProvider.ts | 146 ++++++++------------------ src/providers/settingsManager.ts | 46 ++++++++ 3 files changed, 192 insertions(+), 103 deletions(-) create mode 100644 src/providers/fileSelectionManager.ts create mode 100644 src/providers/settingsManager.ts diff --git a/src/providers/fileSelectionManager.ts b/src/providers/fileSelectionManager.ts new file mode 100644 index 0000000..16c3101 --- /dev/null +++ b/src/providers/fileSelectionManager.ts @@ -0,0 +1,103 @@ + +import * as vscode from 'vscode'; +import * as path from 'path'; + +/** + * Manages file selection state for the Prompter extension + */ +export class FileSelectionManager { + private selectedFiles: Set = new Set(); + + constructor() {} + + /** + * Add a file to the selection + */ + addFile(filePath: string): void { + this.selectedFiles.add(filePath); + console.log(`Added ${filePath} to selection`); + } + + /** + * Add a directory and all its contents to the selection + */ + async addDirectory(dirPath: string): Promise { + 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.addDirectory(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 + */ + removeFile(filePath: string): void { + this.selectedFiles.delete(filePath); + console.log(`Removed ${filePath} from selection`); + } + + /** + * Remove a directory and all its contents from the selection + */ + async removeDirectory(dirPath: string): Promise { + 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.removeDirectory(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); + } + } + + /** + * Check if a file is selected + */ + isSelected(filePath: string): boolean { + return this.selectedFiles.has(filePath); + } + + /** + * Get all selected files + */ + getSelectedFiles(): Set { + console.log('getSelectedFiles called, count:', this.selectedFiles.size); + return this.selectedFiles; + } +} diff --git a/src/providers/prompterTreeProvider.ts b/src/providers/prompterTreeProvider.ts index 955e2d1..a2472f7 100644 --- a/src/providers/prompterTreeProvider.ts +++ b/src/providers/prompterTreeProvider.ts @@ -2,6 +2,8 @@ import * as vscode from 'vscode'; import * as path from 'path'; import { FileTreeItem, SettingTreeItem } from '../models/treeItems'; import { PrompterSettings, DEFAULT_SETTINGS } from '../models/settings'; +import { FileSelectionManager } from './fileSelectionManager'; +import { SettingsManager } from './settingsManager'; /** * Tree provider for the Prompter extension @@ -10,16 +12,19 @@ import { PrompterSettings, DEFAULT_SETTINGS } from '../models/settings'; export class PrompterTreeProvider implements vscode.TreeDataProvider { private _onDidChangeTreeData: vscode.EventEmitter = new vscode.EventEmitter(); - + readonly onDidChangeTreeData: vscode.Event = this._onDidChangeTreeData.event; - - private selectedFiles: Set = new Set(); - private settings: PrompterSettings = { ...DEFAULT_SETTINGS }; + + private fileSelectionManager: FileSelectionManager; + private settingsManager: SettingsManager; private showingSettings: boolean = false; - constructor(private workspaceRoot: string | undefined) {} - + constructor(private workspaceRoot: string | undefined) { + this.fileSelectionManager = new FileSelectionManager(); + this.settingsManager = new SettingsManager(); + } + // Toggle between file view and settings view toggleSettingsView(): void { this.showingSettings = !this.showingSettings; @@ -31,10 +36,10 @@ export class PrompterTreeProvider implements vscode.TreeDataProvider { try { const files = await vscode.workspace.fs.readDirectory(vscode.Uri.file(dirPath)); - + return files.map(([name, type]) => { const filePath = path.join(dirPath, name); const uri = vscode.Uri.file(filePath); - + return new FileTreeItem( uri, type === vscode.FileType.Directory @@ -94,100 +99,35 @@ export class PrompterTreeProvider implements vscode.TreeDataProvider { - 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) { - 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); + if (this.isDirectory(item)) { + this.fileSelectionManager.removeDirectory(item.resourceUri.fsPath); } else { - // It's a file, just remove it - this.selectedFiles.delete(filePath); - console.log(`Removed ${filePath} from selection`); + this.fileSelectionManager.removeFile(item.resourceUri.fsPath); } } } - - // Recursively remove all files in a directory from the selection - private async removeDirectoryFromSelection(dirPath: string): Promise { - 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); - } + + private isDirectory(item: FileTreeItem): boolean { + return item.collapsibleState === vscode.TreeItemCollapsibleState.Collapsed || + item.collapsibleState === vscode.TreeItemCollapsibleState.Expanded; } - + // Toggle a file's selection status toggleSelection(item: FileTreeItem): void { if (item.resourceUri) { const filePath = item.resourceUri.fsPath; - if (this.selectedFiles.has(filePath)) { + if (this.fileSelectionManager.isSelected(filePath)) { this.removeFromSelection(item); } else { this.addToSelection(item); @@ -198,25 +138,25 @@ export class PrompterTreeProvider implements vscode.TreeDataProvider { - console.log('getSelectedFiles called, count:', this.selectedFiles.size); - return this.selectedFiles; + return this.fileSelectionManager.getSelectedFiles(); } - + // Get settings items for the tree view private getSettingsItems(): SettingTreeItem[] { + const settings = this.settingsManager.getSettings(); return [ - new SettingTreeItem('Include Formatting Instructions', 'includeFormattingInstructions', this.settings.includeFormattingInstructions), - new SettingTreeItem('Token Calculation', 'tokenCalculationEnabled', this.settings.tokenCalculationEnabled), - new SettingTreeItem('Include File Map', 'includeFileMap', this.settings.includeFileMap) + new SettingTreeItem('Include Formatting Instructions', 'includeFormattingInstructions', settings.includeFormattingInstructions), + new SettingTreeItem('Token Calculation', 'tokenCalculationEnabled', settings.tokenCalculationEnabled), + new SettingTreeItem('Include File Map', 'includeFileMap', settings.includeFileMap) ]; } - + // Update a setting value updateSetting(key: keyof PrompterSettings, value: boolean): void { - this.settings[key] = value; + this.settingsManager.updateSetting(key, value); this.refresh(); } - + // Check if showing settings view isShowingSettings(): boolean { return this.showingSettings; @@ -224,23 +164,23 @@ export class PrompterTreeProvider implements vscode.TreeDataProvider