Merge pull request #2 from Iheuzio/fix/format-queries

fix(queries): Fixes the queries formatting
This commit is contained in:
Iheuzio 2023-06-18 17:14:15 -04:00 committed by GitHub
commit 43e3b9781e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -133,71 +133,87 @@ 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" ${
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('');
return ` const formattedContents = selectedFiles
<html> .map(file => {
<body> const document = vscode.workspace.textDocuments.find(doc => doc.uri.fsPath === file.uri.fsPath);
<h1>GPT Context</h1> if (document) {
<form id="questionForm"> const lines = document.getText().split('\n');
<label for="question">Enter your question:</label> const formattedLines = lines.map(line => `\t${line}`).join('\n');
<input type="text" id="question" name="question" required> return `${file.uri.fsPath}:\n\`\`\`\n${formattedLines}\n\`\`\``;
<button type="submit">Submit</button> }
<button type="button" onclick="clearSelectedFiles()">Clear</button> return '';
<button type="button" onclick="refreshSelectedFiles()">Refresh</button> })
</form> .join('\n\n');
${
fileContents ? `<div><pre>${fileContents}</pre></div>` : ''
}
<div>
<h2>Selected Files:</h2>
${fileList}
</div>
<div><pre>${question ? question : ''}</pre></div>
<script>
const vscode = acquireVsCodeApi();
function toggleFileSelection(uri) { return `
vscode.postMessage({ <html>
command: 'toggleFileSelection', <body>
uri: uri <h1>GPT Context</h1>
}); <form id="questionForm">
<div>
<label for="question">Enter your question:</label>
<input type="text" id="question" name="question" required>
<button type="submit">Submit</button>
<button type="button" onclick="clearSelectedFiles()">Clear</button>
<button type="button" onclick="refreshSelectedFiles()">Refresh</button>
</div>
<div>
<div><pre>${question ? question : ''}</pre></div>
${
fileContents ? `<div><pre>${formattedContents}</pre></div>` : ''
} }
</div>
<div>
<h2>Selected Files:</h2>
${fileList}
</div>
<script>
const vscode = acquireVsCodeApi();
function clearSelectedFiles() { function toggleFileSelection(uri) {
vscode.postMessage({ vscode.postMessage({
command: 'clearSelectedFiles' command: 'toggleFileSelection',
}); uri: uri
} });
}
function refreshSelectedFiles() { function clearSelectedFiles() {
vscode.postMessage({ vscode.postMessage({
command: 'refreshFiles' command: 'clearSelectedFiles'
}); });
} }
const form = document.getElementById('questionForm'); function refreshSelectedFiles() {
form.addEventListener('submit', event => { vscode.postMessage({
event.preventDefault(); command: 'refreshFiles'
const question = document.getElementById('question').value; });
vscode.postMessage({ }
command: 'submitQuestion',
text: question const form = document.getElementById('questionForm');
}); form.addEventListener('submit', event => {
}); event.preventDefault();
</script> const question = document.getElementById('question').value;
</body> vscode.postMessage({
</html> command: 'submitQuestion',
`; text: question
});
});
</script>
</body>
</html>
`;
} }
// Activates the extension // Activates the extension
function activate(context) { function activate(context) {
// Register the file data provider // Register the file data provider