Skip to content

Commit c544e2c

Browse files
committed
For undo/redo, isolate changes made externally from our own changes
Make change X, git revert, make change Y, undo. Before this commit, undo would take us to before the revert. Now, correctly takes us to before change Y.
1 parent 9afe7d2 commit c544e2c

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/history/historyTracker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,9 @@ export class HistoryTracker {
681681
*
682682
* Determines what changed by diffing the document against what it used to look like.
683683
*/
684-
public addChange(force: boolean = false): void {
684+
public addChange(force: boolean = false): boolean {
685685
if (this.getDocumentVersion() === this.previousDocumentState.versionNumber) {
686-
return;
686+
return false;
687687
}
688688

689689
if (this.nextStepCursorsAtStart === undefined) {
@@ -698,12 +698,12 @@ export class HistoryTracker {
698698
// We can ignore changes while we're in insert/replace mode, since we can't interact with them (via undo, etc.) until we're back to normal mode
699699
// This allows us to avoid a little bit of work per keystroke, but more importantly, it means we'll get bigger contiguous edit chunks to merge.
700700
// This is particularly impactful when there are multiple cursors, which are otherwise difficult to optimize.
701-
return;
701+
return false;
702702
}
703703

704704
const newText = this.getDocumentText();
705705
if (newText === this.previousDocumentState.text) {
706-
return;
706+
return false;
707707
}
708708

709709
// TODO: This is actually pretty stupid! Since we already have the cursorPosition,
@@ -741,6 +741,8 @@ export class HistoryTracker {
741741
text: newText,
742742
versionNumber: this.getDocumentVersion(),
743743
};
744+
745+
return true;
744746
}
745747

746748
/**

src/mode/modeHandler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,10 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
613613
}
614614

615615
// Catch any text change not triggered by us (example: tab completion).
616-
this.vimState.historyTracker.addChange();
616+
const changeAdded = this.vimState.historyTracker.addChange();
617+
if (changeAdded) {
618+
this.vimState.historyTracker.finishCurrentStep();
619+
}
617620

618621
const recordedState = this.vimState.recordedState;
619622
recordedState.actionKeys.push(key);

test/mode/normalModeTests/undo.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ suite('Undo', () => {
148148
keysPressed: 'R' + '123' + '<C-g>u' + '456' + '<Esc>' + 'u',
149149
end: ['123|DEF'],
150150
});
151+
152+
// TODO: Make change X, git revert, make change Y, undo
153+
// TODO: Test interaction with VS Code's native undo
151154
});
152155

153156
suite('U', () => {

0 commit comments

Comments
 (0)