feat/preview #3
170
src/extension.ts
170
src/extension.ts
|
@ -61,6 +61,7 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({
|
||||||
textDecoration: 'none; display: none;', // Hide the original text
|
textDecoration: 'none; display: none;', // Hide the original text
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
|
@ -113,90 +114,101 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
let completionText = response.data.response;
|
let completionText = response.data.response;
|
||||||
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
|
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
|
||||||
|
|
||||||
// Split the completion text by new lines
|
let previewInserted = false;
|
||||||
const newLines = completionText.split('\n');
|
let originalContent: string;
|
||||||
|
let previewStartLine: number;
|
||||||
|
|
||||||
// Calculate the number of new lines in the completion, plus 2 extra lines
|
// Store the original content and insert preview
|
||||||
const totalNewLines = newLines.length + 2;
|
const storeAndInsertPreview = async () => {
|
||||||
|
previewStartLine = startLine;
|
||||||
|
const endLine = document.lineCount - 1;
|
||||||
|
const endCharacter = document.lineAt(endLine).text.length;
|
||||||
|
const fullRange = new vscode.Range(startLine, 0, endLine, endCharacter);
|
||||||
|
originalContent = document.getText(fullRange);
|
||||||
|
|
||||||
// Create preview decorations and insert new lines
|
const previewContent = completionText + '\n'.repeat(1);
|
||||||
const previewRanges: vscode.DecorationOptions[] = [];
|
const edit = new vscode.WorkspaceEdit();
|
||||||
const edit = new vscode.WorkspaceEdit();
|
// Overwrite the context by replacing instead of inserting
|
||||||
const document = textEditor.document;
|
edit.replace(document.uri, fullRange, previewContent);
|
||||||
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
|
||||||
for (let i = 0; i < totalNewLines; i++) {
|
// Set decorations on the newly inserted lines
|
||||||
const lineContent = i < newLines.length ? newLines[i] : '';
|
const previewRanges: vscode.DecorationOptions[] = [];
|
||||||
const position = new vscode.Position(startLine + i, 0);
|
const previewLines = previewContent.split('\n');
|
||||||
|
for (let i = 0; i < previewLines.length; i++) {
|
||||||
// Insert a new line
|
const range = new vscode.Range(startLine + i, 0, startLine + i + 1, 0);
|
||||||
edit.insert(document.uri, position, '\n');
|
previewRanges.push({
|
||||||
|
range,
|
||||||
// Create a range for the newly inserted line
|
renderOptions: {
|
||||||
const range = new vscode.Range(position, position.translate(1, 0));
|
after: {
|
||||||
|
contentText: previewLines[i],
|
||||||
previewRanges.push({
|
|
||||||
range,
|
|
||||||
renderOptions: {
|
|
||||||
after: {
|
|
||||||
contentText: lineContent,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Apply the edit to insert new lines
|
});
|
||||||
await vscode.workspace.applyEdit(edit);
|
|
||||||
|
|
||||||
// Set decorations on the newly inserted lines
|
|
||||||
textEditor.setDecorations(previewDecorationType, previewRanges);
|
|
||||||
|
|
||||||
let previewInserted = true;
|
|
||||||
|
|
||||||
// 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, []);
|
|
||||||
disposable.dispose();
|
|
||||||
previewInserted = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Accept preview with Tab key
|
|
||||||
if (change && change.text === '\t' && previewInserted) {
|
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
|
||||||
const edit = new vscode.WorkspaceEdit();
|
|
||||||
|
|
||||||
// Insert the completion text
|
|
||||||
const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines
|
|
||||||
|
|
||||||
// Replace the entire range from the start of the context to the current position
|
|
||||||
const replaceRange = new vscode.Range(startLine, 0, position.line, position.character);
|
|
||||||
edit.replace(document.uri, replaceRange, insertText);
|
|
||||||
|
|
||||||
await vscode.workspace.applyEdit(edit);
|
|
||||||
await document.save(); // Optionally save after inserting
|
|
||||||
|
|
||||||
// Move the cursor to the end of the inserted completion
|
|
||||||
const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0);
|
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
|
||||||
|
|
||||||
disposable.dispose();
|
|
||||||
previewInserted = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (err: any) {
|
|
||||||
vscode.window.showErrorMessage(
|
|
||||||
"Fabelous Autocoder encountered an error: " + err.message
|
|
||||||
);
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
textEditor.setDecorations(previewDecorationType, previewRanges);
|
||||||
|
previewInserted = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Handle preview acceptance or dismissal
|
||||||
|
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
||||||
|
if (event.document.uri.toString() === document.uri.toString() && previewInserted) {
|
||||||
|
const change = event.contentChanges[0];
|
||||||
|
|
||||||
|
// Dismiss preview with Backspace
|
||||||
|
if (change && change.text === '' && change.rangeLength === 1) {
|
||||||
|
await restoreOriginalContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accept preview with Tab key
|
||||||
|
if (change && change.text === '\t') {
|
||||||
|
await acceptPreview();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const restoreOriginalContent = async () => {
|
||||||
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
const endLine = document.lineCount - 1;
|
||||||
|
const endCharacter = document.lineAt(endLine).text.length;
|
||||||
|
const fullRange = new vscode.Range(previewStartLine, 0, endLine, endCharacter);
|
||||||
|
edit.replace(document.uri, fullRange, originalContent);
|
||||||
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
|
previewInserted = false;
|
||||||
|
disposable.dispose();
|
||||||
|
};
|
||||||
|
|
||||||
|
const acceptPreview = async () => {
|
||||||
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
const endLine = document.lineCount - 1;
|
||||||
|
const endCharacter = document.lineAt(endLine).text.length;
|
||||||
|
const fullRange = new vscode.Range(previewStartLine, 0, endLine, endCharacter);
|
||||||
|
edit.replace(document.uri, fullRange, completionText);
|
||||||
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
await document.save();
|
||||||
|
|
||||||
|
// Move the cursor to the end of the inserted completion
|
||||||
|
const newPosition = new vscode.Position(previewStartLine + completionText.split('\n').length - 1, 0);
|
||||||
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
|
||||||
|
previewInserted = false;
|
||||||
|
disposable.dispose();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Call this function to initiate the preview
|
||||||
|
await storeAndInsertPreview();
|
||||||
|
|
||||||
|
} catch (err: any) {
|
||||||
|
vscode.window.showErrorMessage(
|
||||||
|
"Fabelous Autocoder encountered an error: " + err.message
|
||||||
|
);
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue