Added commands for 'go to last change' and 'insert at last change'.

This commit is contained in:
westim 2017-09-23 20:12:42 -04:00
parent 1ef93e90ab
commit 44a5fc08be
2 changed files with 58 additions and 0 deletions

View File

@ -2119,6 +2119,39 @@ class CommandGoForwardInChangelist extends BaseCommand {
}
}
@RegisterAction
class CommandGoLastChange extends BaseCommand {
modes = [ModeName.Normal];
keys = [['`', '.'], ["'", '.']];
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const lastPos = vimState.historyTracker.getLastHistoryStartPosition();
if (lastPos !== undefined) {
vimState.cursorPosition = lastPos[0];
}
return vimState;
}
}
@RegisterAction
class CommandInsertAtLastChange extends BaseCommand {
modes = [ModeName.Normal];
keys = ['g', 'i'];
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const lastPos = vimState.historyTracker.getLastChangeEndPosition();
if (lastPos !== undefined) {
vimState.cursorPosition = lastPos;
vimState.currentMode = ModeName.Insert;
}
return vimState;
}
}
@RegisterAction
export class CommandInsertAtFirstCharacter extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual];

View File

@ -548,6 +548,31 @@ export class HistoryTracker {
return this.historySteps[this.currentHistoryStepIndex].cursorEnd;
}
/**
* Gets the ending cursor position of the last Change of the last Step.
*
* In practice, this sets the cursor position to the end of
* the most recent text change.
*/
getLastChangeEndPosition(): Position | undefined {
if (this.currentHistoryStepIndex === 0) {
return undefined;
}
let lastChangeIndex = this.historySteps[this.currentHistoryStepIndex].changes.length;
if (lastChangeIndex === 0) {
return undefined;
}
return this.historySteps[this.currentHistoryStepIndex].changes[lastChangeIndex - 1].end();
}
getLastHistoryStartPosition(): Position[] | undefined {
if (this.currentHistoryStepIndex === 0) {
return undefined;
}
return this.historySteps[this.currentHistoryStepIndex].cursorStart;
}
setLastHistoryEndPosition(pos: Position[]) {
this.historySteps[this.currentHistoryStepIndex].cursorEnd = pos;
}