From 0450a222e23b91adb6debd9d5d5f572d3881491b Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Wed, 11 Sep 2024 09:41:59 +0200 Subject: [PATCH] added semi working preview --- src/extension.ts | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 02fd31d..1106a6c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -53,13 +53,10 @@ function createFIMPrompt(prefix: string, language: string): string { return `${prefix}${language}\n`; } - const previewDecorationType = vscode.window.createTextEditorDecorationType({ - after: { - color: '#888888', // Grayed-out preview text - fontStyle: 'italic', - }, - isWholeLine: true, // Ensure it handles multiline properly + color: '#888888', // Grayed-out preview text + fontStyle: 'italic', + rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations }); async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { @@ -111,22 +108,26 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo let completionText = response.data.response; completionText = completionText.replace(/||/g, '').trim(); - // Handle multiline text by splitting it into lines + // Split the completion text by new lines const lines = completionText.split('\n'); + + // Create a decoration for each line of the response const previewRanges = lines.map((line: string, idx: number) => { const linePos = new vscode.Position(position.line + idx, 0); - const range = new vscode.Range(linePos, linePos); + const range = new vscode.Range(linePos, linePos); // Set range at the start of each new line return { range, renderOptions: { - after: { - contentText: line, // Show each line properly + before: { + contentText: line, + color: '#888888', + fontStyle: 'italic', } } }; }); - // Set decorations for each line + // Apply the decorations for multiline preview textEditor.setDecorations(previewDecorationType, previewRanges); const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { @@ -135,7 +136,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo // Handle Backspace to decline the preview if (change && change.text === '' && change.rangeLength === 1) { - textEditor.setDecorations(previewDecorationType, []); + textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations disposable.dispose(); } @@ -149,20 +150,22 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo ); if (isCtrlOrCmdPressed) { + // Remove the preview decoration before applying the final completion + textEditor.setDecorations(previewDecorationType, []); + const edit = new vscode.WorkspaceEdit(); const insertPosition = new vscode.Position(position.line, 0); - edit.insert(document.uri, insertPosition, '\n' + completionText); - // Ensure response is added only once + // Insert the completion only once if (!document.getText().includes(completionText)) { + edit.insert(document.uri, insertPosition, '\n' + completionText); await vscode.workspace.applyEdit(edit); } const newPosition = new vscode.Position(position.line + lines.length, lines[lines.length - 1].length); textEditor.selection = new vscode.Selection(newPosition, newPosition); - textEditor.setDecorations(previewDecorationType, []); - disposable.dispose(); + disposable.dispose(); // Clean up the listener after accepting the completion } } }); @@ -179,7 +182,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo - async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) { const item = new vscode.CompletionItem("Fabelous autocompletion"); item.insertText = new vscode.SnippetString('${1:}');