develop #6
|
@ -119,7 +119,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
let previewStartLine: number;
|
let previewStartLine: number;
|
||||||
|
|
||||||
// Store the original content and insert preview
|
// Store the original content and insert preview
|
||||||
// Store the original content and insert preview
|
|
||||||
const storeAndInsertPreview = async () => {
|
const storeAndInsertPreview = async () => {
|
||||||
previewStartLine = startLine;
|
previewStartLine = startLine;
|
||||||
const endLine = document.lineCount - 1;
|
const endLine = document.lineCount - 1;
|
||||||
|
@ -132,13 +131,15 @@ const storeAndInsertPreview = async () => {
|
||||||
edit.replace(document.uri, fullRange, previewContent); // Overwrite the context
|
edit.replace(document.uri, fullRange, previewContent); // Overwrite the context
|
||||||
await vscode.workspace.applyEdit(edit);
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
|
||||||
// Move the cursor to the start of the inserted content for easy dismissal
|
// Move the cursor to the end of the inserted content
|
||||||
const newPosition = new vscode.Position(previewStartLine, 0);
|
const previewLines = previewContent.split('\n');
|
||||||
|
const lastPreviewLineIndex = previewStartLine + previewLines.length - 1;
|
||||||
|
const lastPreviewLine = previewLines[previewLines.length - 1];
|
||||||
|
const newPosition = new vscode.Position(lastPreviewLineIndex, lastPreviewLine.length);
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
|
||||||
// Set decorations on the newly inserted lines
|
// Set decorations on the newly inserted lines
|
||||||
const previewRanges: vscode.DecorationOptions[] = [];
|
const previewRanges: vscode.DecorationOptions[] = [];
|
||||||
const previewLines = previewContent.split('\n');
|
|
||||||
for (let i = 0; i < previewLines.length; i++) {
|
for (let i = 0; i < previewLines.length; i++) {
|
||||||
const range = new vscode.Range(previewStartLine + i, 0, previewStartLine + i + 1, 0);
|
const range = new vscode.Range(previewStartLine + i, 0, previewStartLine + i + 1, 0);
|
||||||
previewRanges.push({
|
previewRanges.push({
|
||||||
|
@ -154,62 +155,74 @@ const storeAndInsertPreview = async () => {
|
||||||
previewInserted = true;
|
previewInserted = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Handle preview acceptance or dismissal
|
|
||||||
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
||||||
if (event.document.uri.toString() === document.uri.toString() && previewInserted) {
|
if (event.document.uri.toString() === document.uri.toString() && previewInserted) {
|
||||||
|
console.log("URI matches and preview inserted");
|
||||||
|
if (event.contentChanges.length > 0) {
|
||||||
const change = event.contentChanges[0];
|
const change = event.contentChanges[0];
|
||||||
|
console.log("Change:", change);
|
||||||
|
|
||||||
// Dismiss preview with Backspace
|
// Accept preview with Tab key
|
||||||
if (change && change.text === '' && change.rangeLength === 1) {
|
if (change && change.text === '\t' && previewInserted) {
|
||||||
|
console.log("Tab key pressed, accepting preview");
|
||||||
|
await acceptPreview(textEditor, document, startLine, position, completionText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep the existing backspace functionality
|
||||||
|
if (change.text === '' && change.range.start.line >= previewStartLine) {
|
||||||
|
console.log("Backspace detected");
|
||||||
await restoreOriginalContent();
|
await restoreOriginalContent();
|
||||||
}
|
}
|
||||||
// Accept preview with Tab key
|
|
||||||
if (change && change.text === '\t') {
|
|
||||||
await acceptPreview();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const restoreOriginalContent = async () => {
|
const restoreOriginalContent = async () => {
|
||||||
const edit = new vscode.WorkspaceEdit();
|
if (!previewInserted) return;
|
||||||
|
|
||||||
const endLine = document.lineCount - 1;
|
const endLine = document.lineCount - 1;
|
||||||
const endCharacter = document.lineAt(endLine).text.length;
|
const fullRange = new vscode.Range(previewStartLine, 0, endLine, document.lineAt(endLine).text.length);
|
||||||
const fullRange = new vscode.Range(previewStartLine, 0, endLine, endCharacter);
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
|
||||||
edit.replace(document.uri, fullRange, originalContent);
|
edit.replace(document.uri, fullRange, originalContent);
|
||||||
await vscode.workspace.applyEdit(edit);
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
disposable.dispose();
|
disposable.dispose();
|
||||||
|
|
||||||
// Move the cursor to the end of the original content
|
// Move cursor back to the end of the original content
|
||||||
const originalLines = originalContent.split('\n');
|
const lastOriginalLineNumber = previewStartLine + originalContent.split('\n').length - 1;
|
||||||
const lastLine = previewStartLine + originalLines.length - 1;
|
const lastLineLength = originalContent.split('\n').pop()?.length || 0;
|
||||||
const lastLineLength = originalLines[originalLines.length - 1].length;
|
const newPosition = new vscode.Position(lastOriginalLineNumber, lastLineLength);
|
||||||
const newPosition = new vscode.Position(lastLine, lastLineLength);
|
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const acceptPreview = async () => {
|
|
||||||
|
// 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");
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
const endLine = document.lineCount - 1;
|
|
||||||
const endCharacter = document.lineAt(endLine).text.length;
|
// Insert the completion text
|
||||||
const fullRange = new vscode.Range(previewStartLine, 0, endLine, endCharacter);
|
const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines
|
||||||
edit.replace(document.uri, fullRange, completionText);
|
|
||||||
|
// Replace the entire 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 vscode.workspace.applyEdit(edit);
|
||||||
await document.save();
|
await document.save(); // Optionally save after inserting
|
||||||
|
|
||||||
// Move the cursor to the end of the inserted completion
|
// Move the cursor to the end of the inserted completion
|
||||||
const completionLines = completionText.split('\n');
|
const totalNewLines = insertText.split('\n').length;
|
||||||
const lastLine = previewStartLine + completionLines.length - 1;
|
const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0);
|
||||||
const lastLineLength = completionLines[completionLines.length - 1].length;
|
|
||||||
const newPosition = new vscode.Position(lastLine, lastLineLength);
|
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
|
||||||
previewInserted = false;
|
|
||||||
disposable.dispose();
|
disposable.dispose();
|
||||||
|
previewInserted = false;
|
||||||
|
console.log("Preview accepted");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue