From 2a46e061d336a342ec60bff90dc5a716b0575fc1 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Mon, 23 Sep 2024 14:17:23 +0200 Subject: [PATCH] Updated to show now also the used contet in preview (small step progress) --- package.json | 2 +- src/extension.ts | 36 ++++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 6b6d545..635a727 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fabelous-autocoder", - "version": "0.2.49", + "version": "0.2.58", "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 7088f95..81d65e7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -112,14 +112,27 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo // Split the completion text by new lines const lines = completionText.split('\n'); - // Define the preview start line - const startLine = position.line; + // Define the start line and end line based on context (adjusting the preview to the same lines) + let startLine = position.line - (context.split('\n').length - 1); + if (startLine < 0) startLine = 0; // Ensure startLine is not negative + + const endLine = Math.min(startLine + lines.length, document.lineCount); // Ensure endLine doesn't exceed document line count + + // Apply the preview line by line over the context range + const previewRanges: vscode.DecorationOptions[] = lines.map((line: string, index: number): vscode.DecorationOptions | null => { + const currentLine = startLine + index; + + // Ensure the current line is within the bounds of the document + if (currentLine >= document.lineCount) return null; + + const _ = new vscode.Position(currentLine, 0); // Position for each preview line + const endOfLine = document.lineAt(currentLine).text.length; // Full range for the current line + + const range = new vscode.Range( + new vscode.Position(currentLine, 0), + new vscode.Position(currentLine, endOfLine) + ); // Create range for each line - // Apply the preview line by line - const previewRanges = lines.map((line: string, index: number) => { - const linePos = new vscode.Position(startLine + index, 0); // Position each line on a new line - const range = new vscode.Range(linePos, linePos); // Create range for the line - return { range: range, // Range for the line renderOptions: { @@ -130,9 +143,8 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo } } }; - }); - - textEditor.setDecorations(previewDecorationType, previewRanges); // Apply the decorations for the preview + }).filter((range: vscode.DecorationOptions | null): range is vscode.DecorationOptions => range !== null); // Filter out any invalid ranges + textEditor.setDecorations(previewDecorationType, previewRanges as any); // Apply the decorations for the preview let previewInserted = true; @@ -155,11 +167,10 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo // Replace the context with the actual completion const acceptedText = completionText; - const endLine = startLine + lines.length; const rangeToReplace = new vscode.Range( new vscode.Position(startLine, 0), - new vscode.Position(endLine, 0) + new vscode.Position(endLine, document.lineAt(endLine - 1).text.length) ); edit.replace(document.uri, rangeToReplace, acceptedText); @@ -184,6 +195,7 @@ 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:}');