feat/preview #3

Merged
Fabel merged 25 commits from feat/preview into develop 2024-10-09 12:27:58 +00:00
1 changed files with 29 additions and 45 deletions
Showing only changes of commit 804a3113e4 - Show all commits

View File

@ -67,7 +67,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
const contextLines = 2;
const startLine = Math.max(0, position.line - contextLines);
const context = getContextLines(document, position);
let isHandlingChange = false;
const fimPrompt = createFIMPrompt(context, document.languageId);
vscode.window.withProgress(
@ -131,16 +131,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
// Split the preview content into lines
const previewLines = previewContent.split('\n');
// Calculate the exact position of the last character of the preview content
const lastPreviewLineIndex = previewStartLine + previewLines.length - 1;
const lastPreviewLine = previewLines[previewLines.length - 2]; // Second to last line (last line is newline)
const newPosition = new vscode.Position(lastPreviewLineIndex - 1, lastPreviewLine.length); // Place cursor at end of last line
// Set the new cursor position after inserting preview
textEditor.selection = new vscode.Selection(newPosition, newPosition);
// Explicitly scroll to reveal the cursor position at the end of the preview
textEditor.revealRange(new vscode.Range(newPosition, newPosition), vscode.TextEditorRevealType.InCenter);
// Set decorations on the newly inserted lines
const previewRanges: vscode.DecorationOptions[] = [];
@ -161,13 +151,13 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted) {
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted || isHandlingChange) {
return;
}
// Determine tab size setting
const tabSize = textEditor.options.tabSize as number || 4;
isHandlingChange = true;
try {
for (const change of event.contentChanges) {
const changeStartLine = change.range.start.line;
@ -176,14 +166,18 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
await restoreOriginalContent();
}
// Detect Tab by checking if the change is a multiple of spaces equal to the tab size
if (/^[ ]+$/.test(change.text) && change.text.length === tabSize && changeStartLine >= previewStartLine) {
await acceptPreview(textEditor, document, startLine, position, completionText);
// Detect Tab key press
if (change.text === '\t' && changeStartLine >= previewStartLine) {
await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText);
}
}
} finally {
isHandlingChange = false;
}
});
const restoreOriginalContent = async () => {
if (!previewInserted) return;
@ -197,34 +191,24 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
textEditor.setDecorations(previewDecorationType, []);
previewInserted = false;
disposable.dispose();
// Move cursor back to the end of the original content
const lastOriginalLineNumber = previewStartLine + originalContent.split('\n').length - 1;
const lastLineLength = originalContent.split('\n').pop()?.length || 0;
const newPosition = new vscode.Position(lastOriginalLineNumber, lastLineLength);
textEditor.selection = new vscode.Selection(newPosition, newPosition);
};
// 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) => {
console.log("Accepting preview");
console.log("Accepting preview with completion text:", completionText);
textEditor.setDecorations(previewDecorationType, []);
const edit = new vscode.WorkspaceEdit();
// Insert the completion text
const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines
// Adjust the insertion logic to avoid duplicate newlines
const insertText = completionText;
// Replace the entire range from the start of the context to the current position
// 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(); // Optionally save after inserting
// Move the cursor to the end of the inserted completion
const totalNewLines = insertText.split('\n').length;
const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0);
textEditor.selection = new vscode.Selection(newPosition, newPosition);
await document.save();
disposable.dispose();
previewInserted = false;