the preview is now working in one row, without the context

This commit is contained in:
Falko Victor Habel 2024-09-23 15:17:23 +02:00
parent 2a46e061d3
commit 8e72d08d53
2 changed files with 30 additions and 41 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "fabelous-autocoder", "name": "fabelous-autocoder",
"version": "0.2.58", "version": "0.2.6",
"displayName": "Fabelous Autocoder", "displayName": "Fabelous Autocoder",
"description": "A simple to use Ollama autocompletion Plugin", "description": "A simple to use Ollama autocompletion Plugin",
"icon": "icon.png", "icon": "icon.png",

View File

@ -53,11 +53,14 @@ function createFIMPrompt(prefix: string, language: string): string {
} }
const previewDecorationType = vscode.window.createTextEditorDecorationType({ const previewDecorationType = vscode.window.createTextEditorDecorationType({
color: '#888888', // Grayed-out preview text after: {
fontStyle: 'italic', color: '#888888', // Grayed-out preview text
rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations fontStyle: 'italic',
},
textDecoration: 'none; display: none;', // Hide the original text
}); });
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document; const document = textEditor.document;
const position = textEditor.selection.active; const position = textEditor.selection.active;
@ -112,73 +115,59 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
// Split the completion text by new lines // Split the completion text by new lines
const lines = completionText.split('\n'); const lines = completionText.split('\n');
// Define the start line and end line based on context (adjusting the preview to the same lines) // Define the start line based on the current position
let startLine = position.line - (context.split('\n').length - 1); const startLine = position.line;
if (startLine < 0) startLine = 0; // Ensure startLine is not negative const endLine = Math.min(startLine + lines.length, document.lineCount);
const endLine = Math.min(startLine + lines.length, document.lineCount); // Ensure endLine doesn't exceed document line count // Apply the preview, replacing existing content
const previewRanges: vscode.DecorationOptions[] = [];
for (let i = 0; i < lines.length; i++) {
const currentLine = startLine + i;
if (currentLine >= document.lineCount) break;
// Apply the preview line by line over the context range const range = document.lineAt(currentLine).range;
const previewRanges: vscode.DecorationOptions[] = lines.map((line: string, index: number): vscode.DecorationOptions | null => { previewRanges.push({
const currentLine = startLine + index; range,
// 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
renderOptions: { renderOptions: {
after: { after: {
contentText: line, contentText: lines[i],
color: '#888888', // Grayed-out text
fontStyle: 'italic', // Italic text for 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
textEditor.setDecorations(previewDecorationType, previewRanges);
let previewInserted = true; let previewInserted = true;
// Handle preview acceptance or dismissal with Tab and Backspace // Handle preview acceptance or dismissal
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
if (event.document.uri.toString() === document.uri.toString()) { if (event.document.uri.toString() === document.uri.toString()) {
const change = event.contentChanges[0]; const change = event.contentChanges[0];
// Dismiss preview with Backspace // Dismiss preview with Backspace
if (change && change.text === '' && change.rangeLength === 1) { if (change && change.text === '' && change.rangeLength === 1) {
textEditor.setDecorations(previewDecorationType, []); // Clear preview textEditor.setDecorations(previewDecorationType, []);
disposable.dispose(); // Clean up event listener disposable.dispose();
previewInserted = false; previewInserted = false;
} }
// Accept preview with Tab key // Accept preview with Tab key
if (change && change.text === '\t' && previewInserted) { if (change && change.text === '\t' && previewInserted) {
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations textEditor.setDecorations(previewDecorationType, []);
const edit = new vscode.WorkspaceEdit(); const edit = new vscode.WorkspaceEdit();
// Replace the context with the actual completion
const acceptedText = completionText;
const rangeToReplace = new vscode.Range( const rangeToReplace = new vscode.Range(
new vscode.Position(startLine, 0), new vscode.Position(startLine, 0),
new vscode.Position(endLine, document.lineAt(endLine - 1).text.length) new vscode.Position(endLine, document.lineAt(endLine - 1).range.end.character)
); );
edit.replace(document.uri, rangeToReplace, acceptedText); edit.replace(document.uri, rangeToReplace, completionText);
await vscode.workspace.applyEdit(edit); await vscode.workspace.applyEdit(edit);
await document.save(); // Optionally save after replacing await document.save(); // Optionally save after replacing
disposable.dispose(); // Clean up event listener disposable.dispose();
previewInserted = false; previewInserted = false;
} }
} }