diff --git a/src/extension.ts b/src/extension.ts index 0e417f5..1ec7535 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -119,6 +119,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo let previewStartLine: number; // Store the original content and insert preview +// Store the original content and insert preview const storeAndInsertPreview = async () => { previewStartLine = startLine; const endLine = document.lineCount - 1; @@ -128,15 +129,18 @@ const storeAndInsertPreview = async () => { const previewContent = completionText + '\n'.repeat(1); const edit = new vscode.WorkspaceEdit(); - // Overwrite the context by replacing instead of inserting - edit.replace(document.uri, fullRange, previewContent); + edit.replace(document.uri, fullRange, previewContent); // Overwrite the context 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 const previewRanges: vscode.DecorationOptions[] = []; const previewLines = previewContent.split('\n'); 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({ range, renderOptions: { @@ -160,7 +164,6 @@ const storeAndInsertPreview = async () => { if (change && change.text === '' && change.rangeLength === 1) { await restoreOriginalContent(); } - // Accept preview with Tab key if (change && change.text === '\t') { await acceptPreview(); @@ -178,7 +181,15 @@ const storeAndInsertPreview = async () => { textEditor.setDecorations(previewDecorationType, []); previewInserted = false; 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 () => { textEditor.setDecorations(previewDecorationType, []); @@ -191,13 +202,17 @@ const storeAndInsertPreview = async () => { 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); + 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); previewInserted = false; disposable.dispose(); }; + // Call this function to initiate the preview await storeAndInsertPreview();