Compare commits

...

2 Commits

1 changed files with 80 additions and 24 deletions

View File

@ -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,34 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
textEditor.setDecorations(previewDecorationType, previewRanges);
previewInserted = true;
};
const disposable = vscode.window.onDidChangeTextEditorSelection(async (event) => {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor || !previewInserted || isHandlingChange) {
return;
}
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
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;
}
});
// Handles Enter key separately
vscode.workspace.onDidChangeTextDocument(async (event) => {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted || isHandlingChange) {
return;
@ -161,14 +189,18 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
for (const change of event.contentChanges) {
const changeStartLine = change.range.start.line;
// Detect Backspace by a single-character deletion
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
await restoreOriginalContent();
if (change.text.includes('\n') && changeStartLine >= previewStartLine) {
// Accept the preview and move to the next line
await acceptPreview(textEditor, textEditor.document, startLine, textEditor.selection.active, completionText);
await vscode.commands.executeCommand('default:type', { text: '\n' });
break;
}
// Detect Tab key press
if (change.text === '\t' && changeStartLine >= previewStartLine) {
await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText);
// Handle Backspace
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
// Discard the preview if Backspace is pressed
await restoreOriginalContent();
break;
}
}
} finally {
@ -176,8 +208,32 @@ 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)
const restoreOriginalContent = async () => {
if (!previewInserted) return;
@ -190,13 +246,11 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
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, []);
const edit = new vscode.WorkspaceEdit();
@ -210,14 +264,16 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
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(
"Fabelous Autocoder encountered an error: " + err.message