develop #6

Merged
Fabel merged 35 commits from develop into main 2024-10-09 18:50:26 +00:00
1 changed files with 19 additions and 17 deletions
Showing only changes of commit 0450a222e2 - Show all commits

View File

@ -53,13 +53,10 @@ function createFIMPrompt(prefix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`; return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`;
} }
const previewDecorationType = vscode.window.createTextEditorDecorationType({ const previewDecorationType = vscode.window.createTextEditorDecorationType({
after: {
color: '#888888', // Grayed-out preview text color: '#888888', // Grayed-out preview text
fontStyle: 'italic', fontStyle: 'italic',
}, rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations
isWholeLine: true, // Ensure it handles multiline properly
}); });
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { 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; let completionText = response.data.response;
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim(); 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'); const lines = completionText.split('\n');
// Create a decoration for each line of the response
const previewRanges = lines.map((line: string, idx: number) => { const previewRanges = lines.map((line: string, idx: number) => {
const linePos = new vscode.Position(position.line + idx, 0); 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 { return {
range, range,
renderOptions: { renderOptions: {
after: { before: {
contentText: line, // Show each line properly contentText: line,
color: '#888888',
fontStyle: 'italic',
} }
} }
}; };
}); });
// Set decorations for each line // Apply the decorations for multiline preview
textEditor.setDecorations(previewDecorationType, previewRanges); textEditor.setDecorations(previewDecorationType, previewRanges);
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
@ -135,7 +136,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
// Handle Backspace to decline the preview // Handle Backspace to decline the preview
if (change && change.text === '' && change.rangeLength === 1) { if (change && change.text === '' && change.rangeLength === 1) {
textEditor.setDecorations(previewDecorationType, []); textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
disposable.dispose(); disposable.dispose();
} }
@ -149,20 +150,22 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
); );
if (isCtrlOrCmdPressed) { if (isCtrlOrCmdPressed) {
// Remove the preview decoration before applying the final completion
textEditor.setDecorations(previewDecorationType, []);
const edit = new vscode.WorkspaceEdit(); const edit = new vscode.WorkspaceEdit();
const insertPosition = new vscode.Position(position.line, 0); 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)) { if (!document.getText().includes(completionText)) {
edit.insert(document.uri, insertPosition, '\n' + completionText);
await vscode.workspace.applyEdit(edit); await vscode.workspace.applyEdit(edit);
} }
const newPosition = new vscode.Position(position.line + lines.length, lines[lines.length - 1].length); const newPosition = new vscode.Position(position.line + lines.length, lines[lines.length - 1].length);
textEditor.selection = new vscode.Selection(newPosition, newPosition); textEditor.selection = new vscode.Selection(newPosition, newPosition);
textEditor.setDecorations(previewDecorationType, []); disposable.dispose(); // Clean up the listener after accepting the completion
disposable.dispose();
} }
} }
}); });
@ -179,7 +182,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) { async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
const item = new vscode.CompletionItem("Fabelous autocompletion"); const item = new vscode.CompletionItem("Fabelous autocompletion");
item.insertText = new vscode.SnippetString('${1:}'); item.insertText = new vscode.SnippetString('${1:}');