Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #1063 #1124

Merged
merged 2 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,46 @@ class CommandGoToDefinition extends BaseCommand {
}
}

@RegisterAction
class CommandGoBackInChangelist extends BaseCommand {
modes = [ModeName.Normal];
keys = ["g", ";"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const originalIndex = vimState.historyTracker.changelistIndex;
const prevPos = vimState.historyTracker.getChangePositionAtindex(originalIndex);

if (prevPos !== undefined) {
vimState.cursorPosition = prevPos[0];
if (vimState.historyTracker.getChangePositionAtindex(originalIndex - 1) !== undefined) {
vimState.historyTracker.changelistIndex = originalIndex - 1;
}
}

return vimState;
}
}

@RegisterAction
class CommandGoForwardInChangelist extends BaseCommand {
modes = [ModeName.Normal];
keys = ["g", ","];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const originalIndex = vimState.historyTracker.changelistIndex;
const nextPos = vimState.historyTracker.getChangePositionAtindex(originalIndex);

if (nextPos !== undefined) {
vimState.cursorPosition = nextPos[0];
if (vimState.historyTracker.getChangePositionAtindex(originalIndex + 1) !== undefined) {
vimState.historyTracker.changelistIndex = originalIndex + 1;
}
}

return vimState;
}
}

// begin insert commands

@RegisterAction
Expand Down
27 changes: 27 additions & 0 deletions src/history/historyTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ export class HistoryTracker {
public lastContentChanges: vscode.TextDocumentContentChangeEvent[];
public currentContentChanges: vscode.TextDocumentContentChangeEvent[];

// Current index in changelist for navigation, resets when a new change is made
public changelistIndex = 0;

public lastInvokedMacro: RecordedState;

/**
Expand Down Expand Up @@ -408,6 +411,9 @@ export class HistoryTracker {

this.currentHistoryStep.cursorEnd = cursorPosition;
this.oldText = newText;

// A change has been made, reset the changelist navigation index to the end
this.changelistIndex = this.historySteps.length - 1;
}

/**
Expand Down Expand Up @@ -518,6 +524,27 @@ export class HistoryTracker {
this.historySteps[this.currentHistoryStepIndex].cursorEnd = pos;
}

getChangePositionAtindex(index: number): Position[] | undefined {
if (this.currentHistoryStepIndex === 0) {
return undefined;
}

let pos = this.getLastHistoryEndPosition();
pos = undefined;

if (this.historySteps[index] !== undefined) {
if (this.historySteps[index].changes.length > 0) {
if (this.historySteps[index].changes[0].isAdd) {
pos = [this.historySteps[index].changes[0].end()];
} else {
pos = [this.historySteps[index].changes[0].start];
}
}
}

return pos;
}

/**
* Handy for debugging the undo/redo stack. + means our current position, check
* means active.
Expand Down