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 => {
if (message.command === 'submitQuestion') {
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
.filter(file => file.selected)
.map(file => {
const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath);
if (document) {
@ -136,13 +146,14 @@ function getWebviewContent(fileContents, question) {
const fileList = selectedFiles
.map(
file =>
`<div><input type="checkbox" ${
`<div><input type="checkbox" data-uri="${file.uri.fsPath}" ${
file.selected ? 'checked' : ''
} onchange="toggleFileSelection('${file.uri.fsPath}')" /> ${file.uri.fsPath}</div>`
)
.join('');
const formattedContents = selectedFiles
.filter(file => file.selected)
.map(file => {
const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath);
if (document) {
@ -202,9 +213,18 @@ function getWebviewContent(fileContents, question) {
form.addEventListener('submit', event => {
event.preventDefault();
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({
command: 'submitQuestion',
text: question
text: question,
selectedUris: selectedUris
});
});
</script>
@ -216,27 +236,12 @@ function getWebviewContent(fileContents, question) {
// Activates the extension
function activate(context) {
// Register the file data provider
vscode.window.registerTreeDataProvider('gpt-contextfiles', fileDataProvider);
// Register the commands
context.subscriptions.push(addFilesCommand);
context.subscriptions.push(openGPTContextPanelCommand);
context.subscriptions.push(refreshSelectedFilesCommand);
context.subscriptions.push(clearSelectedFilesCommand);
context.subscriptions.push(refreshFilesCommand);
// 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();
});
vscode.window.registerTreeDataProvider('selectedFiles', fileDataProvider);
}
module.exports = {
activate
};
exports.activate = activate;