Skip to content

Commit

Permalink
fix: validate cursor when using cursor setters
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoon committed Feb 2, 2019
1 parent 4d63c47 commit 4a86d97
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
11 changes: 4 additions & 7 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ export abstract class BaseCommand extends BaseAction {
for (const { start, stop } of cursorsToIterateOver) {
this.multicursorIndex = cursorIndex++;

vimState.cursorStopPosition = stop;
vimState.cursorStartPosition = start;
vimState.cursorPosition = new Range(start, stop);

for (let j = 0; j < timesToRepeat; j++) {
vimState = await this.exec(stop, vimState);
Expand Down Expand Up @@ -812,8 +811,7 @@ class CommandReplaceInReplaceMode extends BaseCommand {
// If you backspace before the beginning of where you started to replace,
// just move the cursor back.

vimState.cursorStopPosition = position.getLeft();
vimState.cursorStartPosition = position.getLeft();
vimState.cursorPosition = new Range(position.getLeft(), position.getLeft());
} else if (
position.line > replaceState.replaceCursorStartPosition.line ||
position.character > replaceState.originalChars.length
Expand Down Expand Up @@ -3319,8 +3317,8 @@ class ActionJoin extends BaseCommand {
for (const { start, stop } of cursorsToIterateOver) {
this.multicursorIndex = i++;

vimState.cursorStopPosition = stop;
vimState.cursorStartPosition = start;
vimState.cursorStopPosition = stop;

vimState = await this.execJoinLines(start, stop, vimState, timesToRepeat);

Expand Down Expand Up @@ -3569,8 +3567,7 @@ class ActionReplaceCharacterVisual extends BaseCommand {
}
}

vimState.cursorStopPosition = start;
vimState.cursorStartPosition = start;
vimState.cursorPosition = new Range(start, start);
await vimState.setCurrentMode(ModeName.Normal);
return vimState;
}
Expand Down
7 changes: 3 additions & 4 deletions src/actions/commands/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ export class CommandInsertPreviousText extends BaseCommand {
}
}

vimState.cursorStopPosition = Position.FromVSCodePosition(vimState.editor.selection.end);
vimState.cursorStartPosition = Position.FromVSCodePosition(vimState.editor.selection.start);
vimState.cursorStopPosition = Position.FromVSCodePosition(vimState.editor.selection.end);
await vimState.setCurrentMode(ModeName.Insert);
return vimState;
}
Expand Down Expand Up @@ -272,8 +272,8 @@ export class CommandInsertInInsertMode extends BaseCommand {
}
}

vimState.cursorStopPosition = vimState.cursorStopPosition.getLeft();
vimState.cursorStartPosition = vimState.cursorStartPosition.getLeft();
vimState.cursorStopPosition = vimState.cursorStopPosition.getLeft();
} else {
if (vimState.isMultiCursor) {
vimState.recordedState.transformations.push({
Expand Down Expand Up @@ -470,8 +470,7 @@ class CommandCtrlUInInsertMode extends BaseCommand {
? position.getLineBegin()
: position.getLineBeginRespectingIndent();
await TextEditor.delete(new vscode.Range(start, position));
vimState.cursorStopPosition = start;
vimState.cursorStartPosition = start;
vimState.cursorPosition = new Range(start, start);
return vimState;
}
}
Expand Down
19 changes: 6 additions & 13 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ export class YankOperator extends BaseOperator {
if (vimState.surround) {
vimState.surround.range = new Range(start, end);
await vimState.setCurrentMode(ModeName.SurroundInputMode);
vimState.cursorStopPosition = start;
vimState.cursorStartPosition = start;

vimState.cursorPosition = new Range(start, start);
return vimState;
}

Expand Down Expand Up @@ -361,8 +359,7 @@ export class FormatOperator extends BaseOperator {
}

let newCursorPosition = new Position(line, 0).getFirstLineNonBlankChar();
vimState.cursorStopPosition = newCursorPosition;
vimState.cursorStartPosition = newCursorPosition;
vimState.cursorPosition = new Range(newCursorPosition, newCursorPosition);
await vimState.setCurrentMode(ModeName.Normal);
return vimState;
}
Expand Down Expand Up @@ -405,8 +402,7 @@ class UpperCaseVisualBlockOperator extends BaseOperator {
}

const cursorPosition = startPos.isBefore(endPos) ? startPos : endPos;
vimState.cursorStopPosition = cursorPosition;
vimState.cursorStartPosition = cursorPosition;
vimState.cursorPosition = new Range(cursorPosition, cursorPosition)
await vimState.setCurrentMode(ModeName.Normal);

return vimState;
Expand Down Expand Up @@ -450,8 +446,7 @@ class LowerCaseVisualBlockOperator extends BaseOperator {
}

const cursorPosition = startPos.isBefore(endPos) ? startPos : endPos;
vimState.cursorStopPosition = cursorPosition;
vimState.cursorStartPosition = cursorPosition;
vimState.cursorPosition = new Range(cursorPosition, cursorPosition);
await vimState.setCurrentMode(ModeName.Normal);

return vimState;
Expand Down Expand Up @@ -682,8 +677,7 @@ export class ToggleCaseOperator extends BaseOperator {
await ToggleCaseOperator.toggleCase(range);

const cursorPosition = start.isBefore(end) ? start : end;
vimState.cursorStopPosition = cursorPosition;
vimState.cursorStartPosition = cursorPosition;
vimState.cursorPosition = new Range(cursorPosition, cursorPosition)
await vimState.setCurrentMode(ModeName.Normal);

return vimState;
Expand Down Expand Up @@ -719,8 +713,7 @@ class ToggleCaseVisualBlockOperator extends BaseOperator {
}

const cursorPosition = startPos.isBefore(endPos) ? startPos : endPos;
vimState.cursorStopPosition = cursorPosition;
vimState.cursorStartPosition = cursorPosition;
vimState.cursorPosition = new Range(cursorPosition, cursorPosition)
await vimState.setCurrentMode(ModeName.Normal);

return vimState;
Expand Down
3 changes: 1 addition & 2 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ export class ModeHandler implements vscode.Disposable {
this.vimState.lastClickWasPastEol = false;
}

this.vimState.cursorStopPosition = newPosition;
this.vimState.cursorStartPosition = newPosition;
this.vimState.cursorPosition = new Range(newPosition, newPosition)
this.vimState.desiredColumn = newPosition.character;

// start visual mode?
Expand Down
31 changes: 22 additions & 9 deletions src/state/vimState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,41 @@ export class VimState implements vscode.Disposable {

public globalState = globalState;

/**
* The position the cursor will be when this action finishes.
*/
public get cursorStopPosition(): Position {
return this.cursors[0].stop;
public get cursorPosition(): Range {
return this.cursors[0];
}
public set cursorStopPosition(value: Position) {
this.cursors[0] = this.cursors[0].withNewStop(value);
public set cursorPosition(range: Range) {
if (!range.isValid(this.editor)) {
this.logger.warn(`invalid cursor position. ${range.toString()}.`);
}
this.cursors[0] = range;
}

/**
* The effective starting position of the movement, used along with cursorPosition to determine
* the range over which to run an Operator. May rarely be different than where the cursor
* The effective starting position of the movement, used along with cursorStopPosition to
* determine the range over which to run an Operator. May be different than where the cursor
* actually starts e.g. if you use the "aw" text motion in the middle of a word.
*/
public get cursorStartPosition(): Position {
return this.cursors[0].start;
}
public set cursorStartPosition(value: Position) {
if (!value.isValid(this.editor)) {
this.logger.warn(`invalid cursor position. ${value.toString()}.`);
}
this.cursors[0] = this.cursors[0].withNewStart(value);
}

public get cursorStopPosition(): Position {
return this.cursors[0].stop;
}
public set cursorStopPosition(value: Position) {
if (!value.isValid(this.editor)) {
this.logger.warn(`invalid cursor position. ${value.toString()}.`);
}
this.cursors[0] = this.cursors[0].withNewStop(value);
}

/**
* The position of every cursor.
*/
Expand Down

0 comments on commit 4a86d97

Please sign in to comment.