feat/preview #3

Merged
Fabel merged 25 commits from feat/preview into develop 2024-10-09 12:27:58 +00:00
1 changed files with 20 additions and 5 deletions
Showing only changes of commit ccd3528325 - Show all commits

View File

@ -119,6 +119,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
let previewStartLine: number; let previewStartLine: number;
// Store the original content and insert preview // Store the original content and insert preview
// Store the original content and insert preview
const storeAndInsertPreview = async () => { const storeAndInsertPreview = async () => {
previewStartLine = startLine; previewStartLine = startLine;
const endLine = document.lineCount - 1; const endLine = document.lineCount - 1;
@ -128,15 +129,18 @@ const storeAndInsertPreview = async () => {
const previewContent = completionText + '\n'.repeat(1); const previewContent = completionText + '\n'.repeat(1);
const edit = new vscode.WorkspaceEdit(); const edit = new vscode.WorkspaceEdit();
// Overwrite the context by replacing instead of inserting edit.replace(document.uri, fullRange, previewContent); // Overwrite the context
edit.replace(document.uri, fullRange, previewContent);
await vscode.workspace.applyEdit(edit); await vscode.workspace.applyEdit(edit);
// Move the cursor to the start of the inserted content for easy dismissal
const newPosition = new vscode.Position(previewStartLine, 0);
textEditor.selection = new vscode.Selection(newPosition, newPosition);
// Set decorations on the newly inserted lines // Set decorations on the newly inserted lines
const previewRanges: vscode.DecorationOptions[] = []; const previewRanges: vscode.DecorationOptions[] = [];
const previewLines = previewContent.split('\n'); const previewLines = previewContent.split('\n');
for (let i = 0; i < previewLines.length; i++) { for (let i = 0; i < previewLines.length; i++) {
const range = new vscode.Range(startLine + i, 0, startLine + i + 1, 0); const range = new vscode.Range(previewStartLine + i, 0, previewStartLine + i + 1, 0);
previewRanges.push({ previewRanges.push({
range, range,
renderOptions: { renderOptions: {
@ -160,7 +164,6 @@ const storeAndInsertPreview = async () => {
if (change && change.text === '' && change.rangeLength === 1) { if (change && change.text === '' && change.rangeLength === 1) {
await restoreOriginalContent(); await restoreOriginalContent();
} }
// Accept preview with Tab key // Accept preview with Tab key
if (change && change.text === '\t') { if (change && change.text === '\t') {
await acceptPreview(); await acceptPreview();
@ -178,7 +181,15 @@ const storeAndInsertPreview = async () => {
textEditor.setDecorations(previewDecorationType, []); textEditor.setDecorations(previewDecorationType, []);
previewInserted = false; previewInserted = false;
disposable.dispose(); disposable.dispose();
// Move the cursor to the end of the original content
const originalLines = originalContent.split('\n');
const lastLine = previewStartLine + originalLines.length - 1;
const lastLineLength = originalLines[originalLines.length - 1].length;
const newPosition = new vscode.Position(lastLine, lastLineLength);
textEditor.selection = new vscode.Selection(newPosition, newPosition);
}; };
const acceptPreview = async () => { const acceptPreview = async () => {
textEditor.setDecorations(previewDecorationType, []); textEditor.setDecorations(previewDecorationType, []);
@ -191,13 +202,17 @@ const storeAndInsertPreview = async () => {
await document.save(); await document.save();
// Move the cursor to the end of the inserted completion // Move the cursor to the end of the inserted completion
const newPosition = new vscode.Position(previewStartLine + completionText.split('\n').length - 1, 0); const completionLines = completionText.split('\n');
const lastLine = previewStartLine + completionLines.length - 1;
const lastLineLength = completionLines[completionLines.length - 1].length;
const newPosition = new vscode.Position(lastLine, lastLineLength);
textEditor.selection = new vscode.Selection(newPosition, newPosition); textEditor.selection = new vscode.Selection(newPosition, newPosition);
previewInserted = false; previewInserted = false;
disposable.dispose(); disposable.dispose();
}; };
// Call this function to initiate the preview // Call this function to initiate the preview
await storeAndInsertPreview(); await storeAndInsertPreview();