From 66c88a053b40c7c688e8f3cd97c486c42f57fae7 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Mon, 23 Sep 2024 09:13:03 +0200 Subject: [PATCH] having now switech to use tab to complete completion, but the preview itself is still a mess --- package.json | 2 +- src/extension.ts | 54 +++++++++++++++++++++--------------------------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 0f2c205..6b6d545 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fabelous-autocoder", - "version": "0.1.7", + "version": "0.2.49", "displayName": "Fabelous Autocoder", "description": "A simple to use Ollama autocompletion Plugin", "icon": "icon.png", diff --git a/src/extension.ts b/src/extension.ts index 319ee83..0d06f9c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -48,7 +48,6 @@ function getContextLines(document: vscode.TextDocument, position: vscode.Positio return lines.join("\n"); } - function createFIMPrompt(prefix: string, language: string): string { return `${prefix}${language}\n`; } @@ -59,8 +58,6 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({ rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations }); - - async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { const document = textEditor.document; const position = textEditor.selection.active; @@ -78,7 +75,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo async (progress, progressCancellationToken) => { try { progress.report({ message: "Starting model..." }); - + let axiosCancelPost: () => void; const axiosCancelToken = new axios.CancelToken((c) => { axiosCancelPost = () => { @@ -106,24 +103,25 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo 'Authorization': apiAuthentication } }); - + progress.report({ message: "Generating..." }); - + let completionText = response.data.response; completionText = completionText.replace(/||/g, '').trim(); // Split the completion text by new lines const lines = completionText.split('\n'); - - // Remove context lines and insert the preview - const startLine = Math.max(0, position.line - 1); // Start 1 line before the cursor - const endLine = position.line + 1; // End 1 line after the cursor + + // Define the preview start and end lines + const startLine = Math.max(0, position.line); // Use cursor position directly + const endLine = position.line + lines.length; + const rangeToReplace = new vscode.Range( new vscode.Position(startLine, 0), new vscode.Position(endLine, 0) ); - // Apply grayed-out italic styling to the preview + // Apply the preview with multi-line support const previewRanges = lines.map((line: string, index: number) => { const linePos = new vscode.Position(startLine + index, 0); return { @@ -138,39 +136,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo }; }); - const previewDecorationType = vscode.window.createTextEditorDecorationType({ - color: '#888888', // Grayed-out color - fontStyle: 'italic', // Italic style - }); - - // Apply the preview as decoration textEditor.setDecorations(previewDecorationType, previewRanges); - // Flag to ensure we only accept or dismiss once let previewInserted = true; - // Event handler to accept or dismiss the preview + // Handle preview acceptance or dismissal with Tab and Backspace const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { if (event.document.uri.toString() === document.uri.toString()) { const change = event.contentChanges[0]; - // Handle Backspace to dismiss the preview + // Dismiss preview with Backspace if (change && change.text === '' && change.rangeLength === 1) { - // Remove the decoration preview - textEditor.setDecorations(previewDecorationType, []); + textEditor.setDecorations(previewDecorationType, []); // Clear preview disposable.dispose(); // Clean up event listener previewInserted = false; } - // Handle Enter to accept the preview - if (change && change.text === '\n' && previewInserted) { - // Remove the decoration preview and insert actual completion text - textEditor.setDecorations(previewDecorationType, []); // Remove decorations - + // Accept preview with Tab key + if (change && change.text === '\t' && previewInserted) { + textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations const edit = new vscode.WorkspaceEdit(); - const acceptedText = completionText; - edit.replace(document.uri, rangeToReplace, acceptedText); // Insert actual completion + + // Replace the context with the actual completion + const acceptedText = completionText; + edit.replace(document.uri, rangeToReplace, acceptedText); + await vscode.workspace.applyEdit(edit); + await document.save(); // Optionally save after replacing disposable.dispose(); // Clean up event listener previewInserted = false; @@ -192,11 +184,11 @@ 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:}'); - + if (responsePreview) { await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000)); if (cancellationToken.isCancellationRequested) { - return [ item ]; + return [item]; } const context = getContextLines(document, position);