preview added but badly

This commit is contained in:
Falko Victor Habel 2024-09-10 19:51:03 +02:00
parent d4fc375e01
commit 58c9af0256
1 changed files with 55 additions and 17 deletions

View File

@ -53,13 +53,19 @@ 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, // Support multiline properly
});
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document;
const position = textEditor.selection.active;
const context = getContextLines(document, position);
const fimPrompt = createFIMPrompt(context, document.languageId);
vscode.window.withProgress(
@ -102,27 +108,58 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
progress.report({ message: "Generating..." });
let completionText = response.data.response;
// Remove any FIM tags and leading/trailing whitespace
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
// Remove the context lines
const startLine = Math.max(0, position.line - 1);
const endLine = position.line;
const rangeToReplace = new vscode.Range(
new vscode.Position(startLine, 0),
new vscode.Position(endLine, document.lineAt(endLine).text.length)
);
// Handle multiline text by splitting it into lines
const lines = completionText.split('\n');
const previewRanges = lines.map((line: string, idx: number) => {
const linePos = new vscode.Position(position.line + idx, 0);
const range = new vscode.Range(linePos, linePos);
return {
range,
renderOptions: {
after: {
contentText: line, // Show each line
}
}
};
});
// Apply the edit
const edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, rangeToReplace, completionText);
await vscode.workspace.applyEdit(edit);
textEditor.setDecorations(previewDecorationType, previewRanges);
// Move the cursor to the end of the inserted text
const newPosition = new vscode.Position(startLine + completionText.split('\n').length - 1, completionText.split('\n').pop()!.length);
textEditor.selection = new vscode.Selection(newPosition, newPosition);
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
if (event.document.uri.toString() === document.uri.toString()) {
const change = event.contentChanges[0];
progress.report({ message: "Fabelous completion finished." });
// Handle Backspace to decline the preview
if (change && change.text === '' && change.rangeLength === 1) {
textEditor.setDecorations(previewDecorationType, []);
disposable.dispose();
}
// Handle Ctrl + Enter (or Cmd + Enter on macOS) to accept the preview
const isCtrlOrCmdPressed = event.contentChanges.some(
(change) => {
const isMac = process.platform === 'darwin';
const isCtrlOrCmd = isMac ? change.text.includes('\u0010') : change.text.includes('\n');
return isCtrlOrCmd;
}
);
if (isCtrlOrCmdPressed) {
const edit = new vscode.WorkspaceEdit();
const insertPosition = new vscode.Position(position.line, 0);
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();
}
}
});
} catch (err: any) {
vscode.window.showErrorMessage(
@ -135,6 +172,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:}');