having now switech to use tab to complete completion, but the preview itself is still a mess

This commit is contained in:
Falko Victor Habel 2024-09-23 09:13:03 +02:00
parent ac7afb4b4e
commit 66c88a053b
2 changed files with 24 additions and 32 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fabelous-autocoder",
"version": "0.1.7",
"version": "0.2.49",
"displayName": "Fabelous Autocoder",
"description": "A simple to use Ollama autocompletion Plugin",
"icon": "icon.png",

View File

@ -48,7 +48,6 @@ 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`;
}
@ -59,8 +58,6 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({
rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations
});
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document;
const position = textEditor.selection.active;
@ -115,15 +112,16 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
// Split the completion text by new lines
const lines = completionText.split('\n');
// Remove context lines and insert the preview
const startLine = Math.max(0, position.line - 1); // Start 1 line before the cursor
const endLine = position.line + 1; // End 1 line after the cursor
// Define the preview start and end lines
const startLine = Math.max(0, position.line); // Use cursor position directly
const endLine = position.line + lines.length;
const rangeToReplace = new vscode.Range(
new vscode.Position(startLine, 0),
new vscode.Position(endLine, 0)
);
// Apply grayed-out italic styling to the preview
// Apply the preview with multi-line support
const previewRanges = lines.map((line: string, index: number) => {
const linePos = new vscode.Position(startLine + index, 0);
return {
@ -138,39 +136,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
};
});
const previewDecorationType = vscode.window.createTextEditorDecorationType({
color: '#888888', // Grayed-out color
fontStyle: 'italic', // Italic style
});
// Apply the preview as decoration
textEditor.setDecorations(previewDecorationType, previewRanges);
// Flag to ensure we only accept or dismiss once
let previewInserted = true;
// Event handler to accept or dismiss the preview
// Handle preview acceptance or dismissal with Tab and Backspace
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
if (event.document.uri.toString() === document.uri.toString()) {
const change = event.contentChanges[0];
// Handle Backspace to dismiss the preview
// Dismiss preview with Backspace
if (change && change.text === '' && change.rangeLength === 1) {
// Remove the decoration preview
textEditor.setDecorations(previewDecorationType, []);
textEditor.setDecorations(previewDecorationType, []); // Clear preview
disposable.dispose(); // Clean up event listener
previewInserted = false;
}
// Handle Enter to accept the preview
if (change && change.text === '\n' && previewInserted) {
// Remove the decoration preview and insert actual completion text
textEditor.setDecorations(previewDecorationType, []); // Remove decorations
// Accept preview with Tab key
if (change && change.text === '\t' && previewInserted) {
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
const edit = new vscode.WorkspaceEdit();
// Replace the context with the actual completion
const acceptedText = completionText;
edit.replace(document.uri, rangeToReplace, acceptedText); // Insert actual completion
edit.replace(document.uri, rangeToReplace, acceptedText);
await vscode.workspace.applyEdit(edit);
await document.save(); // Optionally save after replacing
disposable.dispose(); // Clean up event listener
previewInserted = false;