develop #6
|
@ -67,7 +67,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
const contextLines = 2;
|
const contextLines = 2;
|
||||||
const startLine = Math.max(0, position.line - contextLines);
|
const startLine = Math.max(0, position.line - contextLines);
|
||||||
const context = getContextLines(document, position);
|
const context = getContextLines(document, position);
|
||||||
|
let isHandlingChange = false;
|
||||||
const fimPrompt = createFIMPrompt(context, document.languageId);
|
const fimPrompt = createFIMPrompt(context, document.languageId);
|
||||||
|
|
||||||
vscode.window.withProgress(
|
vscode.window.withProgress(
|
||||||
|
@ -131,16 +131,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
// Split the preview content into lines
|
// Split the preview content into lines
|
||||||
const previewLines = previewContent.split('\n');
|
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
|
// Set decorations on the newly inserted lines
|
||||||
const previewRanges: vscode.DecorationOptions[] = [];
|
const previewRanges: vscode.DecorationOptions[] = [];
|
||||||
|
@ -161,29 +151,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
|
|
||||||
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
const disposable = 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) {
|
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted || isHandlingChange) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine tab size setting
|
isHandlingChange = true;
|
||||||
const tabSize = textEditor.options.tabSize as number || 4;
|
|
||||||
|
|
||||||
for (const change of event.contentChanges) {
|
try {
|
||||||
const changeStartLine = change.range.start.line;
|
for (const change of event.contentChanges) {
|
||||||
|
const changeStartLine = change.range.start.line;
|
||||||
|
|
||||||
// Detect Backspace by a single-character deletion
|
// Detect Backspace by a single-character deletion
|
||||||
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
||||||
await restoreOriginalContent();
|
await restoreOriginalContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect Tab by checking if the change is a multiple of spaces equal to the tab size
|
// Detect Tab key press
|
||||||
if (/^[ ]+$/.test(change.text) && change.text.length === tabSize && changeStartLine >= previewStartLine) {
|
if (change.text === '\t' && changeStartLine >= previewStartLine) {
|
||||||
await acceptPreview(textEditor, document, startLine, position, completionText);
|
await acceptPreview(textEditor, event.document, startLine, textEditor.selection.active, completionText);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
isHandlingChange = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const restoreOriginalContent = async () => {
|
const restoreOriginalContent = async () => {
|
||||||
if (!previewInserted) return;
|
if (!previewInserted) return;
|
||||||
|
|
||||||
|
@ -197,35 +191,25 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
disposable.dispose();
|
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
|
// 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");
|
console.log("Accepting preview with completion text:", completionText);
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
|
||||||
// Insert the completion text
|
// Adjust the insertion logic to avoid duplicate newlines
|
||||||
const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra 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);
|
const replaceRange = new vscode.Range(startLine, 0, position.line, position.character);
|
||||||
edit.replace(document.uri, replaceRange, insertText);
|
edit.replace(document.uri, replaceRange, insertText);
|
||||||
|
|
||||||
await vscode.workspace.applyEdit(edit);
|
await vscode.workspace.applyEdit(edit);
|
||||||
await document.save(); // Optionally save after inserting
|
await document.save();
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
disposable.dispose();
|
disposable.dispose();
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
console.log("Preview accepted");
|
console.log("Preview accepted");
|
||||||
|
|
Loading…
Reference in New Issue