remove_compeltion_items #8

Merged
Fabel merged 7 commits from remove_compeltion_items into develop 2024-10-13 11:59:25 +00:00
3 changed files with 92 additions and 25 deletions
Showing only changes of commit 91d13d36df - Show all commits

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", "name": "fabelous-autocoder",
"version": "0.1.7", "version": "0.1.8",
"displayName": "Fabelous Autocoder", "displayName": "Fabelous Autocoder",
"description": "A simple to use Ollama autocompletion Plugin", "description": "A simple to use Ollama autocompletion Plugin",
"icon": "icon.png", "icon": "icon.png",
@ -112,6 +112,10 @@
{ {
"command": "fabelous-autocoder.autocomplete", "command": "fabelous-autocoder.autocomplete",
"title": "Fabelous autocompletion" "title": "Fabelous autocompletion"
},
{
"command": "fabelous-autocoder.acceptCompletion",
"title": "Accept Fabelous Autocompletion"
} }
] ]
}, },

View File

@ -15,6 +15,17 @@ let responsePreviewDelay: number;
let continueInline: boolean | undefined; let continueInline: boolean | undefined;
let keepAlive: number | undefined; let keepAlive: number | undefined;
let topP: 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() { function updateVSConfig() {
VSConfig = vscode.workspace.getConfiguration("fabelous-autocoder"); VSConfig = vscode.workspace.getConfiguration("fabelous-autocoder");
@ -24,6 +35,7 @@ function updateVSConfig() {
numPredict = VSConfig.get("max tokens predicted") || 1000; numPredict = VSConfig.get("max tokens predicted") || 1000;
promptWindowSize = VSConfig.get("prompt window size") || 2000; promptWindowSize = VSConfig.get("prompt window size") || 2000;
completionKeys = VSConfig.get("completion keys") || " "; completionKeys = VSConfig.get("completion keys") || " ";
// Here's where the response preview setting is read
responsePreview = VSConfig.get("response preview"); responsePreview = VSConfig.get("response preview");
responsePreviewMaxTokens = VSConfig.get("preview max tokens") || 50; responsePreviewMaxTokens = VSConfig.get("preview max tokens") || 50;
responsePreviewDelay = VSConfig.get("preview delay") || 0; responsePreviewDelay = VSConfig.get("preview delay") || 0;
@ -48,12 +60,10 @@ function getContextLines(document: vscode.TextDocument, position: vscode.Positio
return lines.join("\n"); return lines.join("\n");
} }
function createFIMPrompt(prefix: string, language: string): string { 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`;
} }
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;
@ -134,11 +144,11 @@ 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:}');
// This is where the response preview functionality is implemented
if (responsePreview) { if (responsePreview) {
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000)); await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
if (cancellationToken.isCancellationRequested) { if (cancellationToken.isCancellationRequested) {
@ -161,46 +171,98 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
...(keepAlive && { keep_alive: keepAlive }), ...(keepAlive && { keep_alive: keepAlive }),
...(topP && { top_p: topP }), ...(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) { } catch (error) {
console.error("Error fetching preview:", error); console.error("Error fetching preview:", error);
} }
} }
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Fabelous Autocoder'); item.documentation = new vscode.MarkdownString('Press `Enter` to accept the autocompletion, or `Escape` to dismiss');
if (continueInline || !responsePreview) {
item.command = { item.command = {
command: 'fabelous-autocoder.autocomplete', command: 'fabelous-autocoder.acceptCompletion',
title: 'Fabelous Autocomplete', title: 'Accept Fabelous Autocompletion',
arguments: [cancellationToken]
}; };
}
return [item]; 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) { function activate(context: vscode.ExtensionContext) {
inlinePreviewDecoration = createInlinePreviewDecoration();
const completionProvider = vscode.languages.registerCompletionItemProvider("*", { const completionProvider = vscode.languages.registerCompletionItemProvider("*", {
provideCompletionItems provideCompletionItems
}, },
...completionKeys.split("") ...completionKeys.split("")
); );
const externalAutocompleteCommand = vscode.commands.registerTextEditorCommand(
"fabelous-autocoder.autocomplete", const acceptCompletionCommand = vscode.commands.registerCommand(
(textEditor, _, cancellationToken?) => { "fabelous-autocoder.acceptCompletion",
autocompleteCommand(textEditor, cancellationToken); acceptCompletion
}
); );
context.subscriptions.push(completionProvider); 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() { } function deactivate() { }
module.exports = { export {
activate, activate,
deactivate, deactivate,
}; };