added preview

This commit is contained in:
Falko Victor Habel 2024-09-10 16:39:33 +02:00
parent d4fc375e01
commit 91d13d36df
3 changed files with 92 additions and 25 deletions

1
2exe.txt Normal file
View File

@ -0,0 +1 @@
vsce package --baseContentUrl https://gitea.fabelous.app/fabel/Fabelous-Autocoder/src/branch/main --baseImagesUrl https://gitea.fabelous.app/fabel/Fabelous-Autocoder/src/branch/main

View File

@ -1,6 +1,6 @@
{
"name": "fabelous-autocoder",
"version": "0.1.7",
"version": "0.1.8",
"displayName": "Fabelous Autocoder",
"description": "A simple to use Ollama autocompletion Plugin",
"icon": "icon.png",
@ -112,7 +112,11 @@
{
"command": "fabelous-autocoder.autocomplete",
"title": "Fabelous autocompletion"
}
},
{
"command": "fabelous-autocoder.acceptCompletion",
"title": "Accept Fabelous Autocompletion"
}
]
},
"scripts": {

View File

@ -15,6 +15,17 @@ let responsePreviewDelay: number;
let continueInline: boolean | undefined;
let keepAlive: number | undefined;
let topP: number | undefined;
let inlinePreviewDecoration: vscode.TextEditorDecorationType;
let currentPreviewText: string | undefined;
function createInlinePreviewDecoration() {
return vscode.window.createTextEditorDecorationType({
after: {
color: '#808080',
fontStyle: 'italic'
}
});
}
function updateVSConfig() {
VSConfig = vscode.workspace.getConfiguration("fabelous-autocoder");
@ -24,6 +35,7 @@ function updateVSConfig() {
numPredict = VSConfig.get("max tokens predicted") || 1000;
promptWindowSize = VSConfig.get("prompt window size") || 2000;
completionKeys = VSConfig.get("completion keys") || " ";
// Here's where the response preview setting is read
responsePreview = VSConfig.get("response preview");
responsePreviewMaxTokens = VSConfig.get("preview max tokens") || 50;
responsePreviewDelay = VSConfig.get("preview delay") || 0;
@ -48,12 +60,10 @@ function getContextLines(document: vscode.TextDocument, position: vscode.Positio
return lines.join("\n");
}
function createFIMPrompt(prefix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`;
}
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document;
const position = textEditor.selection.active;
@ -134,11 +144,11 @@ 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:}');
// This is where the response preview functionality is implemented
if (responsePreview) {
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
if (cancellationToken.isCancellationRequested) {
@ -161,46 +171,98 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
...(keepAlive && { keep_alive: keepAlive }),
...(topP && { top_p: topP }),
}
}, {
cancelToken: new axios.CancelToken((c) => {
cancellationToken.onCancellationRequested(() => c("Autocompletion request terminated by completion cancel"));
})
});
currentPreviewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
updateInlinePreview(document, position);
if (currentPreviewText) {
item.label = currentPreviewText.split('\n')[0];
}
} catch (error) {
console.error("Error fetching preview:", error);
}
}
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Fabelous Autocoder');
if (continueInline || !responsePreview) {
item.command = {
command: 'fabelous-autocoder.autocomplete',
title: 'Fabelous Autocomplete',
arguments: [cancellationToken]
};
}
item.documentation = new vscode.MarkdownString('Press `Enter` to accept the autocompletion, or `Escape` to dismiss');
item.command = {
command: 'fabelous-autocoder.acceptCompletion',
title: 'Accept Fabelous Autocompletion',
};
return [item];
}
function removeInlinePreview() {
const editor = vscode.window.activeTextEditor;
if (editor) {
editor.setDecorations(inlinePreviewDecoration, []);
}
currentPreviewText = undefined;
}
function acceptCompletion() {
const editor = vscode.window.activeTextEditor;
if (editor && currentPreviewText) {
editor.edit(editBuilder => {
const position = editor.selection.active;
editBuilder.insert(position, currentPreviewText as string);
});
removeInlinePreview();
}
}
function activate(context: vscode.ExtensionContext) {
inlinePreviewDecoration = createInlinePreviewDecoration();
const completionProvider = vscode.languages.registerCompletionItemProvider("*", {
provideCompletionItems
},
...completionKeys.split("")
);
const externalAutocompleteCommand = vscode.commands.registerTextEditorCommand(
"fabelous-autocoder.autocomplete",
(textEditor, _, cancellationToken?) => {
autocompleteCommand(textEditor, cancellationToken);
}
const acceptCompletionCommand = vscode.commands.registerCommand(
"fabelous-autocoder.acceptCompletion",
acceptCompletion
);
context.subscriptions.push(completionProvider);
context.subscriptions.push(externalAutocompleteCommand);
context.subscriptions.push(acceptCompletionCommand);
// Handle Escape key to remove preview
context.subscriptions.push(vscode.commands.registerCommand('type', args => {
if (args.text === 'Escape') {
removeInlinePreview();
}
return vscode.commands.executeCommand('default:type', args);
}));
// Remove preview when cursor moves
context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(() => {
removeInlinePreview();
}));
}
function updateInlinePreview(document: vscode.TextDocument, position: vscode.Position) {
if (currentPreviewText) {
const editor = vscode.window.activeTextEditor;
if (editor && editor.document === document) {
const decorations = [{
range: new vscode.Range(position, position),
renderOptions: {
after: {
contentText: currentPreviewText
}
}
}];
editor.setDecorations(inlinePreviewDecoration, decorations);
}
}
}
function deactivate() { }
module.exports = {
export {
activate,
deactivate,
};
};