Updated to show now also the used contet in preview (small step progress)

This commit is contained in:
Falko Victor Habel 2024-09-23 14:17:23 +02:00
parent 27571fcad8
commit 2a46e061d3
2 changed files with 25 additions and 13 deletions

View File

@ -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",

View File

@ -112,13 +112,26 @@ 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
// 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
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
return {
range: range, // Range for the line
@ -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:}');