From 93be5cbce3263c5565d051483db37ff20d29d14c Mon Sep 17 00:00:00 2001 From: Christopher Date: Sun, 18 Jun 2023 16:30:57 -0400 Subject: [PATCH] fix(gpt-context): Fixes the layout and the options with the selected files --- extension.js | 221 +++++++++++++++++++++++---------------------------- 1 file changed, 99 insertions(+), 122 deletions(-) diff --git a/extension.js b/extension.js index 11c844e..6c36368 100644 --- a/extension.js +++ b/extension.js @@ -1,11 +1,14 @@ const vscode = require('vscode'); -const path = require('path'); // Represents a file item in the file explorer class FileItem { - constructor(uri, checked) { + constructor(uri, selected = false) { this.uri = uri; - this.checked = checked || false; + this.selected = selected; + } + + toggleSelected() { + this.selected = !this.selected; } } @@ -14,79 +17,53 @@ const selectedFiles = []; // Tree data provider for the selected files class FileDataProvider { - constructor() { - this._onDidChangeTreeData = new vscode.EventEmitter(); - this.onDidChangeTreeData = this._onDidChangeTreeData.event; - this.filterPatterns = ['*.*']; // Default filter pattern - } + constructor() { + this._onDidChangeTreeData = new vscode.EventEmitter(); + this.onDidChangeTreeData = this._onDidChangeTreeData.event; + } - refresh() { - this._onDidChangeTreeData.fire(); - } + refresh() { + this._onDidChangeTreeData.fire(); + } - getTreeItem(element) { - return { - label: element.uri.fsPath, - collapsibleState: vscode.TreeItemCollapsibleState.None, - checked: element.checked - }; - } + getTreeItem(element) { + return { + label: element.uri.fsPath, + collapsibleState: vscode.TreeItemCollapsibleState.None + }; + } - getChildren(element) { - if (element) { - return []; - } - return selectedFiles; - } + getChildren(element) { + if (element) { + return []; + } - setFilter(filter) { - this.filterPatterns = filter.split(',').map(pattern => pattern.trim()); - this.refresh(); - } - - filterFiles(files) { - return files.filter(file => { - const extension = path.extname(file.uri.fsPath); - return this.filterPatterns.some(pattern => { - const regex = new RegExp(pattern.replace(/\./g, '\\.').replace(/\*/g, '.*')); - return regex.test(extension); - }); - }); - } + // Return only the selected files + return selectedFiles.filter(file => file.selected); + } } + + // Command for adding files to gpt-contextfiles const addFilesCommand = vscode.commands.registerCommand('extension.addFilesToGPTContext', () => { - const workspaceFolders = vscode.workspace.workspaceFolders; - if (workspaceFolders && workspaceFolders.length > 0) { - const workspacePath = workspaceFolders[0].uri.fsPath; - vscode.workspace.findFiles('**/*', '', 1000).then(files => { - const fileItems = files.map(file => new FileItem(file)); - selectedFiles.splice(0, selectedFiles.length, ...fileItems); - fileDataProvider.refresh(); - }); - } + const editor = vscode.window.activeTextEditor; + if (editor) { + const uri = editor.document.uri; + const existingFileIndex = selectedFiles.findIndex(file => file.uri.fsPath === uri.fsPath); + + if (existingFileIndex !== -1) { + // File already exists, remove it from the list + selectedFiles.splice(existingFileIndex, 1); + } else { + // Add the file to the list with selected state + selectedFiles.push(new FileItem(uri, true)); + } + + fileDataProvider.refresh(); + } }); -// Refresh the file list when workspace changes (file creation, deletion, renaming) -vscode.workspace.onDidChangeWorkspaceFolders(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - -vscode.workspace.onDidCreateFiles(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - -vscode.workspace.onDidDeleteFiles(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - -vscode.workspace.onDidRenameFiles(() => { - vscode.commands.executeCommand('extension.addFilesToGPTContext'); -}); - - - const fileDataProvider = new FileDataProvider(); // Command for displaying the webview panel @@ -105,46 +82,52 @@ const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.op panel.webview.onDidReceiveMessage(message => { if (message.command === 'submitQuestion') { const question = message.text; - const selectedFilePaths = selectedFiles - .filter(file => file.checked) - .map(file => file.uri.fsPath); - - const fileContents = selectedFilePaths - .map(filePath => { - const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === filePath); + const fileContents = selectedFiles + .map(file => { + const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); if (document) { const lines = document.getText().split('\n'); - return `${filePath}\n${lines.join('\n')}`; + return `${file.uri.fsPath}\n${lines.join('\n')}`; } return ''; }) .join('\n\n'); panel.webview.html = getWebviewContent(fileContents, question); - } else if (message.command === 'fileSelectionChanged') { - const { filePath, checked } = message; - const file = selectedFiles.find(file => file.uri.fsPath === filePath); + } else if (message.command === 'toggleFileSelection') { + const uri = message.uri; + const file = selectedFiles.find(file => file.uri.fsPath === uri); if (file) { - file.checked = checked; + file.toggleSelected(); + fileDataProvider.refresh(); } - } else if (message.command === 'filterFiles') { - const { filter } = message; - fileDataProvider.setFilter(filter); + } else if (message.command === 'clearSelectedFiles') { + selectedFiles.forEach(file => { + file.selected = false; + }); + selectedFiles.length = 0; // Clear the array + fileDataProvider.refresh(); + } else if (message.command === 'refreshSelectedFiles') { + fileDataProvider.refresh(); } }); }); +// Command for refreshing the selected files +const refreshSelectedFilesCommand = vscode.commands.registerCommand('extension.refreshSelectedFiles', () => { + fileDataProvider.refresh(); +}); + // Helper function to generate the HTML content for the webview panel function getWebviewContent(fileContents, question) { - const fileItems = fileDataProvider - .filterFiles(selectedFiles) - .map(file => ` -
- - -
- `) - .join('\n'); + const fileList = selectedFiles + .map( + file => + `
${file.uri.fsPath}
` + ) + .join(''); return ` @@ -154,23 +137,39 @@ function getWebviewContent(fileContents, question) { + + -
-

Select Files:

-
- - - -
- ${fileItems} -
${ fileContents ? `
${fileContents}
` : '' } +
+

Selected Files:

+ ${fileList} +
${question ? question : ''}
@@ -217,6 +193,7 @@ function activate(context) { // Register the commands context.subscriptions.push(addFilesCommand); context.subscriptions.push(openGPTContextPanelCommand); + context.subscriptions.push(refreshSelectedFilesCommand); // Refresh the file data provider when a file is added or removed from the workspace vscode.workspace.onDidChangeWorkspaceFolders(() => {