diff --git a/src/extension.ts b/src/extension.ts index 1ec7535..cfa1354 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -118,8 +118,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo let originalContent: string; 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 () => { previewStartLine = startLine; const endLine = document.lineCount - 1; @@ -132,13 +131,15 @@ const storeAndInsertPreview = async () => { 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); + // Move the cursor to the end of the inserted content + const previewLines = previewContent.split('\n'); + const lastPreviewLineIndex = previewStartLine + previewLines.length - 1; + const lastPreviewLine = previewLines[previewLines.length - 1]; + const newPosition = new vscode.Position(lastPreviewLineIndex, lastPreviewLine.length); 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(previewStartLine + i, 0, previewStartLine + i + 1, 0); previewRanges.push({ @@ -154,62 +155,74 @@ const storeAndInsertPreview = async () => { previewInserted = true; }; + const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { + if (event.document.uri.toString() === document.uri.toString() && previewInserted) { + console.log("URI matches and preview inserted"); + if (event.contentChanges.length > 0) { + const change = event.contentChanges[0]; + console.log("Change:", change); - // 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(); - } - } - }); + // Accept preview with Tab key + if (change && change.text === '\t' && previewInserted) { + console.log("Tab key pressed, accepting preview"); + await acceptPreview(textEditor, document, startLine, position, completionText); + } + // Keep the existing backspace functionality + if (change.text === '' && change.range.start.line >= previewStartLine) { + console.log("Backspace detected"); + await restoreOriginalContent(); + } + } + } + }); const restoreOriginalContent = async () => { - const edit = new vscode.WorkspaceEdit(); + if (!previewInserted) return; + const endLine = document.lineCount - 1; - const endCharacter = document.lineAt(endLine).text.length; - const fullRange = new vscode.Range(previewStartLine, 0, endLine, endCharacter); + const fullRange = new vscode.Range(previewStartLine, 0, endLine, document.lineAt(endLine).text.length); + const edit = new vscode.WorkspaceEdit(); + edit.replace(document.uri, fullRange, originalContent); await vscode.workspace.applyEdit(edit); + 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); + + // Move cursor back to the end of the original content + const lastOriginalLineNumber = previewStartLine + originalContent.split('\n').length - 1; + const lastLineLength = originalContent.split('\n').pop()?.length || 0; + const newPosition = new vscode.Position(lastOriginalLineNumber, lastLineLength); textEditor.selection = new vscode.Selection(newPosition, newPosition); }; + - const acceptPreview = async () => { + // Modify the acceptPreview function to match the working version + const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => { + console.log("Accepting preview"); 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); + + // 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(); + await document.save(); // Optionally save after inserting // Move the cursor to the end of the inserted completion - 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); + const totalNewLines = insertText.split('\n').length; + const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0); textEditor.selection = new vscode.Selection(newPosition, newPosition); - previewInserted = false; disposable.dispose(); + previewInserted = false; + console.log("Preview accepted"); };