develop #6

Merged
Fabel merged 35 commits from develop into main 2024-10-09 18:50:26 +00:00
2 changed files with 24 additions and 32 deletions
Showing only changes of commit 66c88a053b - Show all commits

View File

@ -1,6 +1,6 @@
{ {
"name": "fabelous-autocoder", "name": "fabelous-autocoder",
"version": "0.1.7", "version": "0.2.49",
"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",

View File

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