added semi working preview

This commit is contained in:
Falko Victor Habel 2024-09-11 09:41:59 +02:00
parent 916ea8ca4a
commit 0450a222e2
1 changed files with 19 additions and 17 deletions

View File

@ -53,13 +53,10 @@ function createFIMPrompt(prefix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`;
}
const previewDecorationType = vscode.window.createTextEditorDecorationType({
after: {
color: '#888888', // Grayed-out preview text
fontStyle: 'italic',
},
isWholeLine: true, // Ensure it handles multiline properly
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(/<fim_middle>|<fim_suffix>|<fim_prefix>/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:}');