Merge pull request #3 from Iheuzio/fix/store-variables

fix(selected-files): User can now select individual files to pass into the context using the checkboxes
This commit is contained in:
Iheuzio 2023-06-20 19:45:49 -04:00 committed by GitHub
commit 24e1208329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -80,7 +80,17 @@ const openGPTContextPanelCommand = vscode.commands.registerCommand('extension.op
panel.webview.onDidReceiveMessage(message => { panel.webview.onDidReceiveMessage(message => {
if (message.command === 'submitQuestion') { if (message.command === 'submitQuestion') {
const question = message.text; const question = message.text;
const selectedUris = message.selectedUris;
// Update the selectedFiles array based on the selectedUris
selectedFiles.forEach(file => {
file.selected = selectedUris.includes(file.uri.fsPath);
});
fileDataProvider.refresh();
const fileContents = selectedFiles const fileContents = selectedFiles
.filter(file => file.selected)
.map(file => { .map(file => {
const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath);
if (document) { if (document) {
@ -133,28 +143,29 @@ const refreshFilesCommand = vscode.commands.registerCommand('extension.refreshFi
// Helper function to generate the HTML content for the webview panel // Helper function to generate the HTML content for the webview panel
function getWebviewContent(fileContents, question) { function getWebviewContent(fileContents, question) {
const fileList = selectedFiles const fileList = selectedFiles
.map( .map(
file => file =>
`<div><input type="checkbox" ${ `<div><input type="checkbox" data-uri="${file.uri.fsPath}" ${
file.selected ? 'checked' : '' file.selected ? 'checked' : ''
} onchange="toggleFileSelection('${file.uri.fsPath}')" /> ${file.uri.fsPath}</div>` } onchange="toggleFileSelection('${file.uri.fsPath}')" /> ${file.uri.fsPath}</div>`
) )
.join(''); .join('');
const formattedContents = selectedFiles const formattedContents = selectedFiles
.map(file => { .filter(file => file.selected)
const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath); .map(file => {
if (document) { const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath);
const lines = document.getText().split('\n'); if (document) {
const formattedLines = lines.map(line => `\t${line}`).join('\n'); const lines = document.getText().split('\n');
return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``; const formattedLines = lines.map(line => `\t${line}`).join('\n');
} return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``;
return ''; }
}) return '';
.join('\n\n'); })
.join('\n\n');
return ` return `
<html> <html>
<body> <body>
<h1>GPT Context</h1> <h1>GPT Context</h1>
@ -202,9 +213,18 @@ function getWebviewContent(fileContents, question) {
form.addEventListener('submit', event => { form.addEventListener('submit', event => {
event.preventDefault(); event.preventDefault();
const question = document.getElementById('question').value; const question = document.getElementById('question').value;
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
const selectedUris = [];
checkboxes.forEach(checkbox => {
if (checkbox.checked) {
const uri = checkbox.getAttribute('data-uri');
selectedUris.push(uri);
}
});
vscode.postMessage({ vscode.postMessage({
command: 'submitQuestion', command: 'submitQuestion',
text: question text: question,
selectedUris: selectedUris
}); });
}); });
</script> </script>
@ -216,27 +236,12 @@ function getWebviewContent(fileContents, question) {
// Activates the extension // Activates the extension
function activate(context) { function activate(context) {
// Register the file data provider
vscode.window.registerTreeDataProvider('gpt-contextfiles', fileDataProvider);
// Register the commands
context.subscriptions.push(addFilesCommand); context.subscriptions.push(addFilesCommand);
context.subscriptions.push(openGPTContextPanelCommand); context.subscriptions.push(openGPTContextPanelCommand);
context.subscriptions.push(refreshSelectedFilesCommand); context.subscriptions.push(refreshSelectedFilesCommand);
context.subscriptions.push(clearSelectedFilesCommand); context.subscriptions.push(clearSelectedFilesCommand);
context.subscriptions.push(refreshFilesCommand); context.subscriptions.push(refreshFilesCommand);
vscode.window.registerTreeDataProvider('selectedFiles', fileDataProvider);
// Refresh the file data provider when a file is added or removed from the workspace
vscode.workspace.onDidChangeWorkspaceFolders(() => {
fileDataProvider.refresh();
});
// Refresh the file data provider when a file is created, deleted, or renamed within the workspace
vscode.workspace.onDidChangeTextDocument(() => {
fileDataProvider.refresh();
});
} }
module.exports = { exports.activate = activate;
activate
};