feat/preview #3
|
@ -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",
|
||||
|
|
|
@ -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;
|
||||
|
@ -78,7 +75,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
|||
async (progress, progressCancellationToken) => {
|
||||
try {
|
||||
progress.report({ message: "Starting model..." });
|
||||
|
||||
|
||||
let axiosCancelPost: () => void;
|
||||
const axiosCancelToken = new axios.CancelToken((c) => {
|
||||
axiosCancelPost = () => {
|
||||
|
@ -106,24 +103,25 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
|||
'Authorization': apiAuthentication
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
progress.report({ message: "Generating..." });
|
||||
|
||||
|
||||
let completionText = response.data.response;
|
||||
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
|
||||
|
||||
// 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();
|
||||
const acceptedText = completionText;
|
||||
edit.replace(document.uri, rangeToReplace, acceptedText); // Insert actual completion
|
||||
|
||||
// Replace the context with the actual completion
|
||||
const acceptedText = completionText;
|
||||
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;
|
||||
|
@ -192,11 +184,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:}');
|
||||
|
||||
|
||||
if (responsePreview) {
|
||||
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
|
||||
if (cancellationToken.isCancellationRequested) {
|
||||
return [ item ];
|
||||
return [item];
|
||||
}
|
||||
|
||||
const context = getContextLines(document, position);
|
||||
|
|
Loading…
Reference in New Issue