Compare commits
No commits in common. "f6ea8494a72e4884c955b2d9ab35df2e4f44c8d3" and "804a3113e45d3a70a28e80ed8c37322f68c1496f" have entirely different histories.
f6ea8494a7
...
804a3113e4
|
@ -116,8 +116,6 @@ 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;
|
||||||
|
@ -150,34 +148,8 @@ 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
isHandlingChange = true;
|
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
||||||
|
|
||||||
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;
|
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;
|
||||||
|
@ -189,18 +161,14 @@ 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;
|
||||||
|
|
||||||
if (change.text.includes('\n') && changeStartLine >= previewStartLine) {
|
// Detect Backspace by a single-character deletion
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle Backspace
|
|
||||||
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
||||||
// Discard the preview if Backspace is pressed
|
|
||||||
await restoreOriginalContent();
|
await restoreOriginalContent();
|
||||||
break;
|
}
|
||||||
|
|
||||||
|
// Detect Tab key press
|
||||||
|
if (change.text === '\t' && changeStartLine >= previewStartLine) {
|
||||||
|
await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -208,32 +176,8 @@ 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 () => {
|
const restoreOriginalContent = async () => {
|
||||||
if (!previewInserted) return;
|
if (!previewInserted) return;
|
||||||
|
|
||||||
|
@ -246,11 +190,13 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
|
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
disposable.dispose(); // Cancel listener when preview is discarded
|
disposable.dispose();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Accept the preview when Tab is pressed
|
// 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) => {
|
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();
|
||||||
|
|
||||||
|
@ -264,16 +210,14 @@ 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(); // Cancel listener when preview is accepted
|
disposable.dispose();
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
|
console.log("Preview accepted");
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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
|
||||||
|
|
Loading…
Reference in New Issue