Use less verbose Jump factory method whenever possible

This commit is contained in:
Shawn Axsom 2018-09-05 08:55:45 -04:00
parent a5f3aee4e8
commit 70385c5e9f
4 changed files with 28 additions and 48 deletions

View File

@ -155,18 +155,8 @@ export async function activate(context: vscode.ExtensionContext) {
});
const recordFileJump = (fromHandler: ModeHandler | null, toHandler: ModeHandler) => {
const from = fromHandler
? new Jump({
editor: fromHandler.vimState.editor,
fileName: fromHandler.vimState.editor.document.fileName,
position: fromHandler.vimState.cursorPosition,
})
: null;
const to = new Jump({
editor: toHandler.vimState.editor,
fileName: toHandler.vimState.editor.document.fileName,
position: toHandler.vimState.cursorPosition,
});
const from = fromHandler ? Jump.fromStateNow(fromHandler.vimState) : null;
const to = Jump.fromStateNow(toHandler.vimState);
globalState.jumpTracker.recordFileJump(from, to);
};

View File

@ -2884,13 +2884,7 @@ class CommandNavigateBack extends ActionNavigateCommand {
}
getJumpToNavigate(position: Position, vimState: VimState) {
return vimState.globalState.jumpTracker.back(
new Jump({
editor: vimState.editor,
fileName: vimState.editor.document.fileName,
position: vimState.cursorPosition,
})
);
return vimState.globalState.jumpTracker.back(Jump.fromStateNow(vimState));
}
}
@ -2904,13 +2898,7 @@ class CommandNavigateForward extends ActionNavigateCommand {
}
getJumpToNavigate(position: Position, vimState: VimState) {
return vimState.globalState.jumpTracker.forward(
new Jump({
editor: vimState.editor,
fileName: vimState.editor.document.fileName,
position: vimState.cursorPosition,
})
);
return vimState.globalState.jumpTracker.forward(Jump.fromStateNow(vimState));
}
}

View File

@ -35,10 +35,10 @@ export class Jump {
}
/**
* Factory method for creating a Jump from a VimState.
* Factory method for creating a Jump from a VimState's current cursor position.
* @param vimState - State that contains the fileName and position for the jump
*/
static fromState(vimState: VimState) {
static fromStateNow(vimState: VimState) {
return new Jump({
editor: vimState.editor,
fileName: vimState.editor.document.fileName,
@ -46,6 +46,19 @@ export class Jump {
});
}
/**
* Factory method for creating a Jump from a VimState's cursor position,
* before any actions or commands were performed.
* @param vimState - State that contains the fileName and prior position for the jump
*/
static fromStateBefore(vimState: VimState) {
return new Jump({
editor: vimState.editor,
fileName: vimState.editor.document.fileName,
position: vimState.cursorPositionJustBeforeAnythingHappened[0],
});
}
/**
* Determine whether another jump matches the same file path and line number,
* regardless of whether the column numbers match.

View File

@ -375,11 +375,7 @@ export class ModeHandler implements vscode.Disposable {
let action = result as BaseAction;
let actionToRecord: BaseAction | undefined = action;
let originalLocation = new Jump({
editor: this.vimState.editor,
fileName: this.vimState.editor.document.fileName,
position: vimState.cursorPosition,
});
let originalLocation = Jump.fromStateNow(vimState);
if (recordedState.actionsRun.length === 0) {
recordedState.actionsRun.push(action);
@ -441,7 +437,10 @@ export class ModeHandler implements vscode.Disposable {
await this.updateView(vimState);
if (action.isJump) {
vimState.globalState.jumpTracker.recordJump(originalLocation, Jump.fromState(vimState));
vimState.globalState.jumpTracker.recordJump(
Jump.fromStateBefore(vimState),
Jump.fromStateNow(vimState)
);
}
return vimState;
@ -972,18 +971,12 @@ export class ModeHandler implements vscode.Disposable {
} else {
this.vimState.recordedState = new RecordedState();
for (let action of recordedMacro.actionsRun) {
let originalLocation = new Jump({
editor: this.vimState.editor,
fileName: this.vimState.editor.document.fileName,
position: vimState.cursorPosition,
});
await this.handleMultipleKeyEvents(action.keysPressed);
if (action.isJump) {
vimState.globalState.jumpTracker.recordJump(
originalLocation,
Jump.fromState(vimState)
Jump.fromStateBefore(vimState),
Jump.fromStateNow(vimState)
);
}
}
@ -1179,11 +1172,7 @@ export class ModeHandler implements vscode.Disposable {
vimState.isRunningDotCommand = true;
for (let action of actions) {
let originalLocation = new Jump({
editor: this.vimState.editor,
fileName: this.vimState.editor.document.fileName,
position: vimState.cursorPosition,
});
let originalLocation = Jump.fromStateNow(vimState);
recordedState.actionsRun.push(action);
vimState.keyHistory = vimState.keyHistory.concat(action.keysPressed);
@ -1203,7 +1192,7 @@ export class ModeHandler implements vscode.Disposable {
await this.updateView(vimState);
if (action.isJump) {
vimState.globalState.jumpTracker.recordJump(originalLocation, Jump.fromState(vimState));
vimState.globalState.jumpTracker.recordJump(originalLocation, Jump.fromStateNow(vimState));
}
}