From 77e328fceef0b9f056502067576293ab6fc75efb Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Thu, 3 Oct 2024 12:20:38 +0300 Subject: [PATCH] near final --- src/extension.ts | 170 +++++++++++++++++++++++++---------------------- 1 file changed, 91 insertions(+), 79 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index d233ffa..0e417f5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -61,6 +61,7 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({ textDecoration: 'none; display: none;', // Hide the original text }); + async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { const document = textEditor.document; const position = textEditor.selection.active; @@ -113,90 +114,101 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo let completionText = response.data.response; completionText = completionText.replace(/||/g, '').trim(); - // Split the completion text by new lines - const newLines = completionText.split('\n'); + let previewInserted = false; + let originalContent: string; + let previewStartLine: number; - // Calculate the number of new lines in the completion, plus 2 extra lines - const totalNewLines = newLines.length + 2; + // Store the original content and insert preview +const storeAndInsertPreview = async () => { + previewStartLine = startLine; + const endLine = document.lineCount - 1; + const endCharacter = document.lineAt(endLine).text.length; + const fullRange = new vscode.Range(startLine, 0, endLine, endCharacter); + originalContent = document.getText(fullRange); - // Create preview decorations and insert new lines - const previewRanges: vscode.DecorationOptions[] = []; - const edit = new vscode.WorkspaceEdit(); - const document = textEditor.document; + const previewContent = completionText + '\n'.repeat(1); + const edit = new vscode.WorkspaceEdit(); + // Overwrite the context by replacing instead of inserting + edit.replace(document.uri, fullRange, previewContent); + await vscode.workspace.applyEdit(edit); - for (let i = 0; i < totalNewLines; i++) { - const lineContent = i < newLines.length ? newLines[i] : ''; - const position = new vscode.Position(startLine + i, 0); - - // Insert a new line - edit.insert(document.uri, position, '\n'); - - // Create a range for the newly inserted line - const range = new vscode.Range(position, position.translate(1, 0)); - - previewRanges.push({ - range, - renderOptions: { - after: { - contentText: lineContent, - } - } - }); + // Set decorations on the newly inserted lines + const previewRanges: vscode.DecorationOptions[] = []; + const previewLines = previewContent.split('\n'); + for (let i = 0; i < previewLines.length; i++) { + const range = new vscode.Range(startLine + i, 0, startLine + i + 1, 0); + previewRanges.push({ + range, + renderOptions: { + after: { + contentText: previewLines[i], } - - // Apply the edit to insert new lines - await vscode.workspace.applyEdit(edit); - - // Set decorations on the newly inserted lines - textEditor.setDecorations(previewDecorationType, previewRanges); - - let previewInserted = true; - - // Handle preview acceptance or dismissal - const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { - if (event.document.uri.toString() === document.uri.toString()) { - const change = event.contentChanges[0]; - - // Dismiss preview with Backspace - if (change && change.text === '' && change.rangeLength === 1) { - textEditor.setDecorations(previewDecorationType, []); - disposable.dispose(); - previewInserted = false; - } - - // Accept preview with Tab key - if (change && change.text === '\t' && previewInserted) { - textEditor.setDecorations(previewDecorationType, []); - const edit = new vscode.WorkspaceEdit(); - - // Insert the completion text - const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines - - // Replace the entire range from the start of the context to the current position - const replaceRange = new vscode.Range(startLine, 0, position.line, position.character); - edit.replace(document.uri, replaceRange, insertText); - - await vscode.workspace.applyEdit(edit); - await document.save(); // Optionally save after inserting - - // Move the cursor to the end of the inserted completion - const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0); - textEditor.selection = new vscode.Selection(newPosition, newPosition); - - disposable.dispose(); - previewInserted = false; - } - } - }); - - } catch (err: any) { - vscode.window.showErrorMessage( - "Fabelous Autocoder encountered an error: " + err.message - ); - console.log(err); - } + } + }); } -); + textEditor.setDecorations(previewDecorationType, previewRanges); + previewInserted = true; +}; + + + // Handle preview acceptance or dismissal + const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { + if (event.document.uri.toString() === document.uri.toString() && previewInserted) { + const change = event.contentChanges[0]; + + // Dismiss preview with Backspace + if (change && change.text === '' && change.rangeLength === 1) { + await restoreOriginalContent(); + } + + // Accept preview with Tab key + if (change && change.text === '\t') { + await acceptPreview(); + } + } + }); + + const restoreOriginalContent = async () => { + const edit = new vscode.WorkspaceEdit(); + const endLine = document.lineCount - 1; + const endCharacter = document.lineAt(endLine).text.length; + const fullRange = new vscode.Range(previewStartLine, 0, endLine, endCharacter); + edit.replace(document.uri, fullRange, originalContent); + await vscode.workspace.applyEdit(edit); + textEditor.setDecorations(previewDecorationType, []); + previewInserted = false; + disposable.dispose(); + }; + + const acceptPreview = async () => { + textEditor.setDecorations(previewDecorationType, []); + const edit = new vscode.WorkspaceEdit(); + const endLine = document.lineCount - 1; + const endCharacter = document.lineAt(endLine).text.length; + const fullRange = new vscode.Range(previewStartLine, 0, endLine, endCharacter); + edit.replace(document.uri, fullRange, completionText); + await vscode.workspace.applyEdit(edit); + await document.save(); + + // Move the cursor to the end of the inserted completion + const newPosition = new vscode.Position(previewStartLine + completionText.split('\n').length - 1, 0); + textEditor.selection = new vscode.Selection(newPosition, newPosition); + + previewInserted = false; + disposable.dispose(); + }; + + // Call this function to initiate the preview + await storeAndInsertPreview(); + + } catch (err: any) { + vscode.window.showErrorMessage( + "Fabelous Autocoder encountered an error: " + err.message + ); + console.log(err); + } + } + ); }