Compare commits
No commits in common. "678c4823b3b20f352d6cde2abdf5e1312e76b204" and "d4fc375e01393d309aa3652ec8dd0aa3e8bc60fd" have entirely different histories.
678c4823b3
...
d4fc375e01
|
@ -53,17 +53,13 @@ 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({
|
|
||||||
color: '#888888', // Grayed-out preview text
|
|
||||||
fontStyle: 'italic',
|
|
||||||
rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations
|
|
||||||
});
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
const context = getContextLines(document, position);
|
const context = getContextLines(document, position);
|
||||||
|
|
||||||
const fimPrompt = createFIMPrompt(context, document.languageId);
|
const fimPrompt = createFIMPrompt(context, document.languageId);
|
||||||
|
|
||||||
vscode.window.withProgress(
|
vscode.window.withProgress(
|
||||||
|
@ -106,69 +102,27 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
progress.report({ message: "Generating..." });
|
progress.report({ message: "Generating..." });
|
||||||
|
|
||||||
let completionText = response.data.response;
|
let completionText = response.data.response;
|
||||||
|
// Remove any FIM tags and leading/trailing whitespace
|
||||||
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
|
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
|
||||||
|
|
||||||
// Split the completion text by new lines
|
// Remove the context lines
|
||||||
const lines = completionText.split('\n');
|
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)
|
||||||
|
);
|
||||||
|
|
||||||
// Create a decoration for each line of the response
|
// Apply the edit
|
||||||
const previewRanges = lines.map((line: string, idx: number) => {
|
const edit = new vscode.WorkspaceEdit();
|
||||||
const linePos = new vscode.Position(position.line + idx, 0);
|
edit.replace(document.uri, rangeToReplace, completionText);
|
||||||
const range = new vscode.Range(linePos, linePos); // Set range at the start of each new line
|
await vscode.workspace.applyEdit(edit);
|
||||||
return {
|
|
||||||
range,
|
|
||||||
renderOptions: {
|
|
||||||
before: {
|
|
||||||
contentText: line,
|
|
||||||
color: '#888888',
|
|
||||||
fontStyle: 'italic',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// Apply the decorations for multiline preview
|
// Move the cursor to the end of the inserted text
|
||||||
textEditor.setDecorations(previewDecorationType, previewRanges);
|
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) => {
|
progress.report({ message: "Fabelous completion finished." });
|
||||||
if (event.document.uri.toString() === document.uri.toString()) {
|
|
||||||
const change = event.contentChanges[0];
|
|
||||||
|
|
||||||
// Handle Backspace to decline the preview
|
|
||||||
if (change && change.text === '' && change.rangeLength === 1) {
|
|
||||||
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
|
|
||||||
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) {
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
disposable.dispose(); // Clean up the listener after accepting the completion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
|
@ -181,7 +135,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:}');
|
||||||
|
|
Loading…
Reference in New Issue