develop #6
|
@ -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,29 +151,33 @@ 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;
|
||||
|
||||
for (const change of event.contentChanges) {
|
||||
const changeStartLine = change.range.start.line;
|
||||
try {
|
||||
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();
|
||||
}
|
||||
|
||||
// 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 Backspace by a single-character deletion
|
||||
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
||||
await restoreOriginalContent();
|
||||
}
|
||||
|
||||
// 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,35 +191,25 @@ 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
|
||||
|
||||
// Replace the entire range from the start of the context to the current position
|
||||
|
||||
// Adjust the insertion logic to avoid duplicate newlines
|
||||
const insertText = completionText;
|
||||
|
||||
// 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;
|
||||
console.log("Preview accepted");
|
||||
|
|
Loading…
Reference in New Issue