From f122d99ba1de2fa162a6e2ca47fd493a35a02223 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Sat, 5 Oct 2024 09:18:22 +0300 Subject: [PATCH] code now works with everything except tab --- src/extension.ts | 62 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index cdf30b8..ff82814 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -116,6 +116,8 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo let previewInserted = false; let originalContent: string; let previewStartLine: number; + + const storeAndInsertPreview = async () => { previewStartLine = startLine; const endLine = document.lineCount - 1; @@ -148,8 +150,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo textEditor.setDecorations(previewDecorationType, previewRanges); previewInserted = true; }; - - const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { + const disposable = vscode.window.onDidChangeTextEditorSelection(async (event) => { + const textEditor = vscode.window.activeTextEditor; + if (!textEditor || !previewInserted || isHandlingChange) { + return; + } + + isHandlingChange = true; + + try { + const activeSelection = textEditor.selection; + const changeStartLine = activeSelection.active.line; + + // Detect Tab key press by checking the active selection + if (event.kind === vscode.TextEditorSelectionChangeKind.Keyboard && changeStartLine >= previewStartLine) { + const changeText = textEditor.document.getText(activeSelection); + + if (changeText === '') { + // Tab key (empty selection) -> Accept the preview + await acceptPreview(textEditor, textEditor.document, startLine, activeSelection.active, completionText); + } + } + } finally { + isHandlingChange = false; + } + }); + + vscode.workspace.onDidChangeTextDocument(async (event) => { const textEditor = vscode.window.activeTextEditor; if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted || isHandlingChange) { return; @@ -166,7 +193,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo await restoreOriginalContent(); } - // Detect Tab key press + // Detect Tab key press by checking if the inserted text is a tab character if (change.text === '\t' && changeStartLine >= previewStartLine) { await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText); } @@ -175,26 +202,24 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo isHandlingChange = false; } }); - - - + + // Restore original content if Backspace is pressed (decline the preview) const restoreOriginalContent = async () => { if (!previewInserted) return; - + const endLine = document.lineCount - 1; 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(); + disposable.dispose(); // Cancel listener when preview is discarded }; - - // Modify the acceptPreview function to match the working version - + + // Accept the preview when Tab is pressed const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => { console.log("Accepting preview with completion text:", completionText); textEditor.setDecorations(previewDecorationType, []); @@ -202,21 +227,24 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo // Adjust the insertion logic to avoid duplicate newlines const insertText = completionText; - + // Replace the 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(); - disposable.dispose(); + disposable.dispose(); // Cancel listener when preview is accepted previewInserted = false; console.log("Preview accepted"); }; - + // Call this function to initiate the preview await storeAndInsertPreview(); + + + } catch (err: any) { vscode.window.showErrorMessage(