feat/preview #3
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "fabelous-autocoder",
|
||||
"version": "0.2.58",
|
||||
"version": "0.2.6",
|
||||
"displayName": "Fabelous Autocoder",
|
||||
"description": "A simple to use Ollama autocompletion Plugin",
|
||||
"icon": "icon.png",
|
||||
|
|
|
@ -53,11 +53,14 @@ function createFIMPrompt(prefix: string, language: string): string {
|
|||
}
|
||||
|
||||
const previewDecorationType = vscode.window.createTextEditorDecorationType({
|
||||
after: {
|
||||
color: '#888888', // Grayed-out preview text
|
||||
fontStyle: 'italic',
|
||||
rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations
|
||||
},
|
||||
textDecoration: 'none; display: none;', // Hide the original text
|
||||
});
|
||||
|
||||
|
||||
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
|
||||
const document = textEditor.document;
|
||||
const position = textEditor.selection.active;
|
||||
|
@ -112,73 +115,59 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
|||
// Split the completion text by new lines
|
||||
const lines = completionText.split('\n');
|
||||
|
||||
// Define the start line and end line based on context (adjusting the preview to the same lines)
|
||||
let startLine = position.line - (context.split('\n').length - 1);
|
||||
if (startLine < 0) startLine = 0; // Ensure startLine is not negative
|
||||
// Define the start line based on the current position
|
||||
const startLine = position.line;
|
||||
const endLine = Math.min(startLine + lines.length, document.lineCount);
|
||||
|
||||
const endLine = Math.min(startLine + lines.length, document.lineCount); // Ensure endLine doesn't exceed document line count
|
||||
// Apply the preview, replacing existing content
|
||||
const previewRanges: vscode.DecorationOptions[] = [];
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const currentLine = startLine + i;
|
||||
if (currentLine >= document.lineCount) break;
|
||||
|
||||
// Apply the preview line by line over the context range
|
||||
const previewRanges: vscode.DecorationOptions[] = lines.map((line: string, index: number): vscode.DecorationOptions | null => {
|
||||
const currentLine = startLine + index;
|
||||
|
||||
// Ensure the current line is within the bounds of the document
|
||||
if (currentLine >= document.lineCount) return null;
|
||||
|
||||
const _ = new vscode.Position(currentLine, 0); // Position for each preview line
|
||||
const endOfLine = document.lineAt(currentLine).text.length; // Full range for the current line
|
||||
|
||||
const range = new vscode.Range(
|
||||
new vscode.Position(currentLine, 0),
|
||||
new vscode.Position(currentLine, endOfLine)
|
||||
); // Create range for each line
|
||||
|
||||
return {
|
||||
range: range, // Range for the line
|
||||
const range = document.lineAt(currentLine).range;
|
||||
previewRanges.push({
|
||||
range,
|
||||
renderOptions: {
|
||||
after: {
|
||||
contentText: line,
|
||||
color: '#888888', // Grayed-out text
|
||||
fontStyle: 'italic', // Italic text for preview
|
||||
contentText: lines[i],
|
||||
}
|
||||
}
|
||||
};
|
||||
}).filter((range: vscode.DecorationOptions | null): range is vscode.DecorationOptions => range !== null); // Filter out any invalid ranges
|
||||
textEditor.setDecorations(previewDecorationType, previewRanges as any); // Apply the decorations for the preview
|
||||
});
|
||||
}
|
||||
|
||||
textEditor.setDecorations(previewDecorationType, previewRanges);
|
||||
|
||||
let previewInserted = true;
|
||||
|
||||
// Handle preview acceptance or dismissal with Tab and Backspace
|
||||
// Handle preview acceptance or dismissal
|
||||
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
||||
if (event.document.uri.toString() === document.uri.toString()) {
|
||||
const change = event.contentChanges[0];
|
||||
|
||||
// Dismiss preview with Backspace
|
||||
if (change && change.text === '' && change.rangeLength === 1) {
|
||||
textEditor.setDecorations(previewDecorationType, []); // Clear preview
|
||||
disposable.dispose(); // Clean up event listener
|
||||
textEditor.setDecorations(previewDecorationType, []);
|
||||
disposable.dispose();
|
||||
previewInserted = false;
|
||||
}
|
||||
|
||||
// Accept preview with Tab key
|
||||
if (change && change.text === '\t' && previewInserted) {
|
||||
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
|
||||
textEditor.setDecorations(previewDecorationType, []);
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
|
||||
// Replace the context with the actual completion
|
||||
const acceptedText = completionText;
|
||||
|
||||
const rangeToReplace = new vscode.Range(
|
||||
new vscode.Position(startLine, 0),
|
||||
new vscode.Position(endLine, document.lineAt(endLine - 1).text.length)
|
||||
new vscode.Position(endLine, document.lineAt(endLine - 1).range.end.character)
|
||||
);
|
||||
|
||||
edit.replace(document.uri, rangeToReplace, acceptedText);
|
||||
edit.replace(document.uri, rangeToReplace, completionText);
|
||||
|
||||
await vscode.workspace.applyEdit(edit);
|
||||
await document.save(); // Optionally save after replacing
|
||||
|
||||
disposable.dispose(); // Clean up event listener
|
||||
disposable.dispose();
|
||||
previewInserted = false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue