Skip to content

Commit

Permalink
Maintain visual selection after failed movement
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Fields committed Aug 4, 2020
1 parent f525b61 commit bc5f3e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
10 changes: 9 additions & 1 deletion src/actions/baseMotion.ts
Expand Up @@ -33,6 +33,14 @@ export interface IMovement {
registerMode?: RegisterMode;
}

export function failedMovement(vimState: VimState): IMovement {
return {
start: vimState.cursorStartPosition,
stop: vimState.cursorStopPosition,
failed: true,
};
}

export abstract class BaseMovement extends BaseAction {
modes = [Mode.Normal, Mode.Visual, Mode.VisualLine, Mode.VisualBlock];

Expand Down Expand Up @@ -108,7 +116,7 @@ export abstract class BaseMovement extends BaseAction {
): Promise<Position | IMovement> {
let recordedState = vimState.recordedState;
let result: Position | IMovement = new Position(0, 0); // bogus init to satisfy typechecker
let prevResult: IMovement = { start: position, stop: position, failed: true };
let prevResult = failedMovement(vimState);
let firstMovementStart: Position = new Position(position.line, position.character);

count = clamp(count, this.minCount, this.maxCount);
Expand Down
58 changes: 17 additions & 41 deletions src/actions/motion.ts
Expand Up @@ -14,7 +14,7 @@ import { VimState } from './../state/vimState';
import { configuration } from './../configuration/configuration';
import { shouldWrapKey } from './wrapping';
import { VimError, ErrorCode } from '../error';
import { BaseMovement, SelectionType, IMovement, isIMovement } from './baseMotion';
import { BaseMovement, SelectionType, IMovement, isIMovement, failedMovement } from './baseMotion';
import { globalState } from '../state/globalState';
import { reportSearch } from '../util/statusBarTextUtils';
import { SneakForward, SneakBackward } from './plugins/sneak';
Expand Down Expand Up @@ -692,7 +692,7 @@ class MoveFindForward extends BaseMovement {
vimState.lastCommaRepeatableMovement = new MoveFindBackward(this.keysPressed, true);

if (!result) {
return { start: position, stop: position, failed: true };
return failedMovement(vimState);
}

if (vimState.recordedState.operator) {
Expand Down Expand Up @@ -728,7 +728,7 @@ class MoveFindBackward extends BaseMovement {
vimState.lastCommaRepeatableMovement = new MoveFindForward(this.keysPressed, true);

if (!result) {
return { start: position, stop: position, failed: true };
return failedMovement(vimState);
}

return result;
Expand Down Expand Up @@ -767,7 +767,7 @@ class MoveTilForward extends BaseMovement {
vimState.lastCommaRepeatableMovement = new MoveTilBackward(this.keysPressed, true);

if (!result) {
return { start: position, stop: position, failed: true };
return failedMovement(vimState);
}

if (vimState.recordedState.operator) {
Expand Down Expand Up @@ -800,7 +800,7 @@ class MoveTilBackward extends BaseMovement {
vimState.lastCommaRepeatableMovement = new MoveTilForward(this.keysPressed, true);

if (!result) {
return { start: position, stop: position, failed: true };
return failedMovement(vimState);
}

return result;
Expand Down Expand Up @@ -1498,7 +1498,7 @@ class MoveToMatchingBracket extends BaseMovement {
position = position.getLeftIfEOL();

const lineText = TextEditor.getLineAt(position).text;
const failure = { start: position, stop: position, failed: true };
const failure = failedMovement(vimState);

for (let col = position.character; col < lineText.length; col++) {
const pairing = PairMatcher.pairings[lineText[col]];
Expand Down Expand Up @@ -1556,7 +1556,7 @@ class MoveToMatchingBracket extends BaseMovement {

// Check to make sure this is a valid percentage
if (count < 0 || count > 100) {
return { start: position, stop: position, failed: true };
return failedMovement(vimState);
}

const targetLine = Math.round((count * TextEditor.getLineCount()) / 100);
Expand All @@ -1576,12 +1576,8 @@ export abstract class MoveInsideCharacter extends ExpandingSelection {

public async execAction(position: Position, vimState: VimState): Promise<IMovement> {
const closingChar = PairMatcher.pairings[this.charToMatch].match;
let cursorStartPos = new Position(
vimState.cursorStartPosition.line,
vimState.cursorStartPosition.character
);
// maintain current selection on failure
const failure = { start: cursorStartPos, stop: position, failed: true };
let cursorStartPos = vimState.cursorStartPosition;
const failure = failedMovement(vimState);

// when matching inside content of a pair, search for the next pair if
// the inner content is already selected in full
Expand Down Expand Up @@ -1800,11 +1796,7 @@ export abstract class MoveQuoteMatch extends BaseMovement {
}

if (start === -1 || end === -1 || end === start || end < position.character) {
return {
start: position,
stop: position,
failed: true,
};
return failedMovement(vimState);
}

let startPos = new Position(position.line, start);
Expand Down Expand Up @@ -1889,12 +1881,11 @@ class MoveToUnclosedRoundBracketBackward extends MoveToMatchingBracket {
keys = ['[', '('];

public async execAction(position: Position, vimState: VimState): Promise<Position | IMovement> {
const failure = { start: position, stop: position, failed: true };
const charToMatch = ')';
const result = PairMatcher.nextPairedChar(position, charToMatch);

if (!result) {
return failure;
return failedMovement(vimState);
}
return result;
}
Expand All @@ -1905,12 +1896,11 @@ class MoveToUnclosedRoundBracketForward extends MoveToMatchingBracket {
keys = [']', ')'];

public async execAction(position: Position, vimState: VimState): Promise<Position | IMovement> {
const failure = { start: position, stop: position, failed: true };
const charToMatch = '(';
const result = PairMatcher.nextPairedChar(position, charToMatch);

if (!result) {
return failure;
return failedMovement(vimState);
}

if (
Expand All @@ -1930,12 +1920,11 @@ class MoveToUnclosedCurlyBracketBackward extends MoveToMatchingBracket {
keys = ['[', '{'];

public async execAction(position: Position, vimState: VimState): Promise<Position | IMovement> {
const failure = { start: position, stop: position, failed: true };
const charToMatch = '}';
const result = PairMatcher.nextPairedChar(position, charToMatch);

if (!result) {
return failure;
return failedMovement(vimState);
}
return result;
}
Expand All @@ -1946,12 +1935,11 @@ class MoveToUnclosedCurlyBracketForward extends MoveToMatchingBracket {
keys = [']', '}'];

public async execAction(position: Position, vimState: VimState): Promise<Position | IMovement> {
const failure = { start: position, stop: position, failed: true };
const charToMatch = '{';
const result = PairMatcher.nextPairedChar(position, charToMatch);

if (!result) {
return failure;
return failedMovement(vimState);
}

if (
Expand All @@ -1975,22 +1963,14 @@ abstract class MoveTagMatch extends ExpandingSelection {
const editorText = TextEditor.getText();
const offset = TextEditor.getOffsetAt(position);
const tagMatcher = new TagMatcher(editorText, offset, vimState);
const cursorStartPos = new Position(
vimState.cursorStartPosition.line,
vimState.cursorStartPosition.character
);
const start = tagMatcher.findOpening(this.includeTag);
const end = tagMatcher.findClosing(this.includeTag);

if (start === undefined || end === undefined) {
return {
start: cursorStartPos,
stop: position,
failed: true,
};
return failedMovement(vimState);
}

let startPosition = start >= 0 ? TextEditor.getPositionAt(start) : cursorStartPos;
let startPosition = start >= 0 ? TextEditor.getPositionAt(start) : vimState.cursorStartPosition;
let endPosition = end >= 0 ? TextEditor.getPositionAt(end) : position;
if (vimState.currentMode === Mode.Visual || vimState.currentMode === Mode.SurroundInputMode) {
endPosition = endPosition.getLeftThroughLineBreaks();
Expand All @@ -2011,11 +1991,7 @@ abstract class MoveTagMatch extends ExpandingSelection {
// if (vimState.recordedState.operator instanceof ChangeOperator) {
// await vimState.setCurrentMode(ModeName.Insert);
// }
// return {
// start: startPosition,
// stop: startPosition,
// failed: true,
// };
// return failedMovement(vimState);
// }
vimState.cursorStartPosition = startPosition;
return {
Expand Down
4 changes: 2 additions & 2 deletions src/actions/textobject.ts
Expand Up @@ -5,7 +5,7 @@ import { RegisterMode } from './../register/register';
import { VimState } from './../state/vimState';
import { TextEditor } from './../textEditor';
import { RegisterAction } from './base';
import { BaseMovement, IMovement } from './baseMotion';
import { BaseMovement, IMovement, failedMovement } from './baseMotion';
import {
MoveAClosingCurlyBrace,
MoveADoubleQuotes,
Expand Down Expand Up @@ -725,7 +725,7 @@ abstract class SelectArgument extends TextObjectMovement {
// multi-line statements.

public async execAction(position: Position, vimState: VimState): Promise<IMovement> {
const failure = { start: position, stop: position, failed: true };
const failure = failedMovement(vimState);

let leftSearchStartPosition = position;
let rightSearchStartPosition = position;
Expand Down

0 comments on commit bc5f3e0

Please sign in to comment.