develop #6

Merged
Fabel merged 35 commits from develop into main 2024-10-09 18:50:26 +00:00
1 changed files with 45 additions and 17 deletions
Showing only changes of commit f122d99ba1 - Show all commits

View File

@ -116,6 +116,8 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
let previewInserted = false; let previewInserted = false;
let originalContent: string; let originalContent: string;
let previewStartLine: number; let previewStartLine: number;
const storeAndInsertPreview = async () => { const storeAndInsertPreview = async () => {
previewStartLine = startLine; previewStartLine = startLine;
const endLine = document.lineCount - 1; const endLine = document.lineCount - 1;
@ -148,8 +150,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
textEditor.setDecorations(previewDecorationType, previewRanges); textEditor.setDecorations(previewDecorationType, previewRanges);
previewInserted = true; 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;
}
});
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) {
return; return;
@ -166,7 +193,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
await restoreOriginalContent(); 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) { if (change.text === '\t' && changeStartLine >= previewStartLine) {
await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText); await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText);
} }
@ -176,8 +203,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
} }
}); });
// Restore original content if Backspace is pressed (decline the preview)
const restoreOriginalContent = async () => { const restoreOriginalContent = async () => {
if (!previewInserted) return; if (!previewInserted) return;
@ -190,11 +216,10 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
textEditor.setDecorations(previewDecorationType, []); textEditor.setDecorations(previewDecorationType, []);
previewInserted = false; 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) => { const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => {
console.log("Accepting preview with completion text:", completionText); console.log("Accepting preview with completion text:", completionText);
textEditor.setDecorations(previewDecorationType, []); textEditor.setDecorations(previewDecorationType, []);
@ -210,7 +235,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
await vscode.workspace.applyEdit(edit); await vscode.workspace.applyEdit(edit);
await document.save(); await document.save();
disposable.dispose(); disposable.dispose(); // Cancel listener when preview is accepted
previewInserted = false; previewInserted = false;
console.log("Preview accepted"); console.log("Preview accepted");
}; };
@ -218,6 +243,9 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
// Call this function to initiate the preview // Call this function to initiate the preview
await storeAndInsertPreview(); await storeAndInsertPreview();
} catch (err: any) { } catch (err: any) {
vscode.window.showErrorMessage( vscode.window.showErrorMessage(
"Fabelous Autocoder encountered an error: " + err.message "Fabelous Autocoder encountered an error: " + err.message