feat/preview #3
|
@ -176,6 +176,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handles Enter key separately
|
||||||
vscode.workspace.onDidChangeTextDocument(async (event) => {
|
vscode.workspace.onDidChangeTextDocument(async (event) => {
|
||||||
const textEditor = vscode.window.activeTextEditor;
|
const textEditor = vscode.window.activeTextEditor;
|
||||||
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted || isHandlingChange) {
|
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted || isHandlingChange) {
|
||||||
|
@ -188,14 +189,18 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
for (const change of event.contentChanges) {
|
for (const change of event.contentChanges) {
|
||||||
const changeStartLine = change.range.start.line;
|
const changeStartLine = change.range.start.line;
|
||||||
|
|
||||||
// Detect Backspace by a single-character deletion
|
if (change.text.includes('\n') && changeStartLine >= previewStartLine) {
|
||||||
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
// Accept the preview and move to the next line
|
||||||
await restoreOriginalContent();
|
await acceptPreview(textEditor, textEditor.document, startLine, textEditor.selection.active, completionText);
|
||||||
|
await vscode.commands.executeCommand('default:type', { text: '\n' });
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect Tab key press by checking if the inserted text is a tab character
|
// Handle Backspace
|
||||||
if (change.text === '\t' && changeStartLine >= previewStartLine) {
|
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
||||||
await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText);
|
// Discard the preview if Backspace is pressed
|
||||||
|
await restoreOriginalContent();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -203,6 +208,31 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
vscode.window.onDidChangeTextEditorSelection(async (event) => {
|
||||||
|
const textEditor = vscode.window.activeTextEditor;
|
||||||
|
if (!textEditor || !previewInserted || isHandlingChange) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
isHandlingChange = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Handle arrow keys or any other navigation keys
|
||||||
|
const currentSelection = event.selections[0];
|
||||||
|
const { document } = textEditor;
|
||||||
|
|
||||||
|
// Detect unwanted acceptance from simple navigation
|
||||||
|
if (currentSelection.start.line < previewStartLine) {
|
||||||
|
await restoreOriginalContent();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
isHandlingChange = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Restore original content if Backspace is pressed (decline the preview)
|
// Restore original content if Backspace is pressed (decline the preview)
|
||||||
const restoreOriginalContent = async () => {
|
const restoreOriginalContent = async () => {
|
||||||
if (!previewInserted) return;
|
if (!previewInserted) return;
|
||||||
|
@ -221,7 +251,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
|
|
||||||
// Accept the preview when Tab is pressed
|
// Accept the preview when Tab is pressed
|
||||||
const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => {
|
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, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
|
||||||
|
@ -237,7 +266,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
|
|
||||||
disposable.dispose(); // Cancel listener when preview is accepted
|
disposable.dispose(); // Cancel listener when preview is accepted
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
console.log("Preview accepted");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Call this function to initiate the preview
|
// Call this function to initiate the preview
|
||||||
|
|
Loading…
Reference in New Issue