Skip to content

Commit

Permalink
refactor: rename cursorPositionJustBeforeAnythingHappened to cursorsI…
Browse files Browse the repository at this point in the history
…nitialState
  • Loading branch information
jpoon committed Feb 2, 2019
1 parent b2899bd commit d96be68
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
13 changes: 10 additions & 3 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,9 @@ class CommandInsertInSearchMode extends BaseCommand {
vimState.globalState.searchStateIndex = vimState.globalState.searchStatePrevious.length - 1;

// Move cursor to next match
vimState.cursorStopPosition = searchState.getNextSearchMatchPosition(vimState.cursorStopPosition).pos;
vimState.cursorStopPosition = searchState.getNextSearchMatchPosition(
vimState.cursorStopPosition
).pos;

return vimState;
} else if (key === '<up>') {
Expand Down Expand Up @@ -1067,7 +1069,10 @@ class CommandCmdA extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.cursorStartPosition = new Position(0, vimState.desiredColumn);
vimState.cursorStopPosition = new Position(TextEditor.getLineCount() - 1, vimState.desiredColumn);
vimState.cursorStopPosition = new Position(
TextEditor.getLineCount() - 1,
vimState.desiredColumn
);
await vimState.setCurrentMode(ModeName.VisualLine);

return vimState;
Expand Down Expand Up @@ -4039,7 +4044,9 @@ abstract class IncrementDecrementNumberAction extends BaseCommand {
start,
end
);
vimState.cursorStopPosition = vimState.cursorStopPosition.getLeftByCount(num.suffix.length);
vimState.cursorStopPosition = vimState.cursorStopPosition.getLeftByCount(
num.suffix.length
);
return vimState;
} else if (num !== null) {
word = word.slice(num.prefix.length + num.value.toString().length);
Expand Down
4 changes: 1 addition & 3 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ export class YankOperator extends BaseOperator {
}

if (originalMode === ModeName.Normal && !moveCursor) {
vimState.cursors = vimState.cursorPositionJustBeforeAnythingHappened.map(
x => new Range(x, x)
);
vimState.cursors = vimState.cursorsInitialState;
} else {
vimState.cursorStopPosition = start;
}
Expand Down
10 changes: 8 additions & 2 deletions src/actions/textobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ export class SelectAnExpandingBlock extends ExpandingSelection {
let smallestRange: Range | undefined = undefined;

for (const iMotion of ranges) {
const currentSelectedRange = new Range(vimState.cursorStartPosition, vimState.cursorStopPosition);
const currentSelectedRange = new Range(
vimState.cursorStartPosition,
vimState.cursorStopPosition
);
if (iMotion.failed) {
continue;
}
Expand Down Expand Up @@ -237,7 +240,10 @@ export class SelectAnExpandingBlock extends ExpandingSelection {
smallestRange.start.line,
smallestRange.start.character
);
vimState.cursorStopPosition = new Position(smallestRange.stop.line, smallestRange.stop.character);
vimState.cursorStopPosition = new Position(
smallestRange.stop.line,
smallestRange.stop.character
);
vimState.recordedState.operatorPositionDiff = undefined;
return {
start: smallestRange.start,
Expand Down
8 changes: 4 additions & 4 deletions src/configuration/remapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ export class Remapper implements IRemapper {
await vimState.historyTracker.undoAndRemoveChanges(
Math.max(0, numCharsToRemove * vimState.cursors.length)
);
vimState.cursors = vimState.cursors.map(x =>
x.withNewStop(x.stop.getLeft(numCharsToRemove))
);
vimState.cursors = vimState.cursors.map(c => c.withNewStop(c.stop.getLeft(numCharsToRemove)));
}
// We need to remove the keys that were remapped into different keys from the state.
vimState.recordedState.actionKeys = vimState.recordedState.actionKeys.slice(
Expand Down Expand Up @@ -208,7 +206,9 @@ export class Remapper implements IRemapper {
.slice(0, inputtedKeys.length - keySlice.length)
.join('');
if (precedingKeys.length > 0 && !/^[0-9]+$/.test(precedingKeys)) {
this._logger.verbose(`key sequences need to match precisely. precedingKeys=${precedingKeys}.`);
this._logger.verbose(
`key sequences need to match precisely. precedingKeys=${precedingKeys}.`
);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/jumps/jump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Jump {
return new Jump({
editor: vimState.editor,
fileName: vimState.editor.document.fileName,
position: vimState.cursorPositionJustBeforeAnythingHappened[0],
position: vimState.cursorsInitialState[0].stop,
});
}

Expand Down
18 changes: 10 additions & 8 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,7 @@ export class ModeHandler implements vscode.Disposable {
key = '<D-d>';
}

this.vimState.cursorPositionJustBeforeAnythingHappened = this.vimState.cursors.map(
x => x.stop
);
this.vimState.cursorsInitialState = this.vimState.cursors;
this.vimState.recordedState.commandList.push(key);

try {
Expand Down Expand Up @@ -321,7 +319,7 @@ export class ModeHandler implements vscode.Disposable {
}

// Catch any text change not triggered by us (example: tab completion).
vimState.historyTracker.addChange(this.vimState.cursorPositionJustBeforeAnythingHappened);
vimState.historyTracker.addChange(this.vimState.cursorsInitialState.map(c => c.stop));

vimState.keyHistory.push(key);

Expand Down Expand Up @@ -588,7 +586,7 @@ export class ModeHandler implements vscode.Disposable {
this.vimState.alteredHistory = false;
vimState.historyTracker.ignoreChange();
} else {
vimState.historyTracker.addChange(this.vimState.cursorPositionJustBeforeAnythingHappened);
vimState.historyTracker.addChange(this.vimState.cursorsInitialState.map(c => c.stop));
}
}

Expand Down Expand Up @@ -915,7 +913,9 @@ export class ModeHandler implements vscode.Disposable {
vimState.cursorStartPosition = Position.FromVSCodePosition(
this.vimState.editor.selection.start
);
vimState.cursorStopPosition = Position.FromVSCodePosition(this.vimState.editor.selection.end);
vimState.cursorStopPosition = Position.FromVSCodePosition(
this.vimState.editor.selection.end
);
break;

case 'showCommandHistory':
Expand Down Expand Up @@ -1168,7 +1168,7 @@ export class ModeHandler implements vscode.Disposable {
}

vimState.isRunningDotCommand = false;
vimState.cursorPositionJustBeforeAnythingHappened = vimState.cursors.map(x => x.stop);
vimState.cursorsInitialState = vimState.cursors;
return vimState;
}

Expand Down Expand Up @@ -1360,7 +1360,9 @@ export class ModeHandler implements vscode.Disposable {

searchRanges.push.apply(searchRanges, searchState.matchRanges);

const { start, end, match } = searchState.getNextSearchMatchRange(vimState.cursorStopPosition);
const { start, end, match } = searchState.getNextSearchMatchRange(
vimState.cursorStopPosition
);

if (match) {
searchRanges.push(new vscode.Range(start, end));
Expand Down
14 changes: 11 additions & 3 deletions src/state/vimState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class VimState implements vscode.Disposable {
public alteredHistory = false;

public isRunningDotCommand = false;

/**
* The last visual selection before running the dot command
*/
Expand Down Expand Up @@ -142,8 +143,6 @@ export class VimState implements vscode.Disposable {
this.cursors[0] = this.cursors[0].withNewStop(value);
}



/**
* The position of every cursor.
*/
Expand All @@ -163,7 +162,16 @@ export class VimState implements vscode.Disposable {
this.isMultiCursor = this._cursors.length > 1;
}

public cursorPositionJustBeforeAnythingHappened = [new Position(0, 0)];
/**
* Initial state of cursors prior to any action being performed
*/
private _cursorsInitialState: Range[];
public get cursorsInitialState(): Range[] {
return this._cursorsInitialState;
}
public set cursorsInitialState(value: Range[]) {
this._cursorsInitialState = Object.assign([], value);
}

public isRecordingMacro: boolean = false;
public isReplayingMacro: boolean = false;
Expand Down

0 comments on commit d96be68

Please sign in to comment.