Undo was terrible. Changed Position to be selection so you could just undo manually instead.

This commit is contained in:
Nathan Hedge 2024-01-27 21:31:40 -06:00
parent b00c82b1e2
commit 2938353228
No known key found for this signature in database
GPG Key ID: 1ADBA36D6E304C5C
1 changed files with 7 additions and 37 deletions

View File

@ -38,18 +38,6 @@ updateVSConfig();
// No need for restart for any of these settings // No need for restart for any of these settings
vscode.workspace.onDidChangeConfiguration(updateVSConfig); vscode.workspace.onDidChangeConfiguration(updateVSConfig);
// internal function to handle undo functionality
async function handleUndo(document: vscode.TextDocument, startingPosition: vscode.Position, endingPosition: vscode.Position, optons?: vscode.MessageOptions) {
vscode.window.showInformationMessage("Ollama autocompletion was cancelled. Would you like it to undo its changes?", "Keep", "Undo").then(r => {
if (r === "Undo") {
const edit = new vscode.WorkspaceEdit();
const range = new vscode.Range(startingPosition, endingPosition);
edit.delete(document.uri, range);
vscode.workspace.applyEdit(edit);
}
});
}
// internal function for autocomplete, not directly exposed // internal function for autocomplete, not directly exposed
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document; const document = textEditor.document;
@ -70,16 +58,10 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
try { try {
progress.report({ message: "Starting model..." }); progress.report({ message: "Starting model..." });
//tracker
// let oldPosition = position;
let currentPosition = position;
// let lastToken = "";
let axiosCancelPost: () => void; let axiosCancelPost: () => void;
const axiosCancelToken = new axios.CancelToken((c) => { const axiosCancelToken = new axios.CancelToken((c) => {
const cancelPost = function () { const cancelPost = function () {
c("Autocompletion request terminated by user cancel"); c("Autocompletion request terminated by user cancel");
handleUndo(document, position, currentPosition);
}; };
axiosCancelPost = cancelPost; axiosCancelPost = cancelPost;
if (cancellationToken) cancellationToken.onCancellationRequested(cancelPost); if (cancellationToken) cancellationToken.onCancellationRequested(cancelPost);
@ -103,23 +85,17 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
} }
); );
//tracker
let currentPosition = position;
response.data.on('data', async (d: Uint8Array) => { response.data.on('data', async (d: Uint8Array) => {
progress.report({ message: "Generating..." }); progress.report({ message: "Generating..." });
// Check for user input (cancel) // Check for user input (cancel)
if (currentPosition != textEditor.selection.start) { if (currentPosition.line != textEditor.selection.end.line || currentPosition.character != textEditor.selection.end.character) {
console.log(currentPosition, textEditor.selection.start);
axiosCancelPost(); // cancel axios => cancel finished promise => close notification axiosCancelPost(); // cancel axios => cancel finished promise => close notification
return; return;
} }
// if (lastToken != "") {
// const lastInput = document.getText(new vscode.Range(oldPosition, textEditor.selection.active));
// if (lastInput !== lastToken) {
// console.log(lastInput, lastToken);
// axiosCancelPost(); // cancel axios => cancel finished promise => close notification
// return;
// }
// }
// Get a completion from the response // Get a completion from the response
const completion: string = JSON.parse(d.toString()).response; const completion: string = JSON.parse(d.toString()).response;
@ -131,11 +107,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
//complete edit for token //complete edit for token
const edit = new vscode.WorkspaceEdit(); const edit = new vscode.WorkspaceEdit();
const range = new vscode.Position( edit.insert(document.uri, currentPosition, completion);
currentPosition.line,
currentPosition.character
);
edit.insert(document.uri, range, completion);
await vscode.workspace.applyEdit(edit); await vscode.workspace.applyEdit(edit);
// Move the cursor to the end of the completion // Move the cursor to the end of the completion
@ -145,18 +117,16 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
(completionLines.length > 1 ? 0 : currentPosition.character) + completionLines[completionLines.length - 1].length (completionLines.length > 1 ? 0 : currentPosition.character) + completionLines[completionLines.length - 1].length
); );
const newSelection = new vscode.Selection( const newSelection = new vscode.Selection(
newPosition, position,
newPosition newPosition
); );
// oldPosition = currentPosition;
currentPosition = newPosition; currentPosition = newPosition;
// completion bar // completion bar
progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) }); progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) });
// move cursor // move cursor
const editor = vscode.window.activeTextEditor; textEditor.selection = newSelection;
if (editor) editor.selection = newSelection;
}); });
// Keep cancel window available // Keep cancel window available