From 5ee011338211e51f08ee772927561129f5bc8ac8 Mon Sep 17 00:00:00 2001 From: Kou Tenichi Date: Thu, 6 Apr 2017 17:28:43 +0900 Subject: [PATCH] Handle move cursor in visual mode correctly. --- src/Actions/MoveCursor.ts | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Actions/MoveCursor.ts b/src/Actions/MoveCursor.ts index 16384819..82d7160e 100644 --- a/src/Actions/MoveCursor.ts +++ b/src/Actions/MoveCursor.ts @@ -2,6 +2,7 @@ import {window, Position, Selection} from 'vscode'; import {ActionReveal} from './Reveal'; import {Motion} from '../Motions/Motion'; import {UtilPosition} from '../Utils/Position'; +import {UtilSelection} from '../Utils/Selection'; export class ActionMoveCursor { @@ -66,34 +67,31 @@ export class ActionMoveCursor { activeTextEditor.selections = activeTextEditor.selections.map((selection, i) => { let anchor: Position; - let active = args.motions.reduce((position, motion) => { - return motion.apply(position, { - isInclusive: args.isVisualMode, - preferedColumn: ActionMoveCursor.preferedColumnBySelectionIndex[i] - }); - }, selection.active); + let active = args.motions.reduce( + (position, motion) => { + return motion.apply(position, { + preferedColumn: ActionMoveCursor.preferedColumnBySelectionIndex[i] + }); + }, + args.isVisualMode + ? UtilSelection.getActiveInVisualMode(selection) + : selection.active + ); if (args.isVisualMode) { anchor = selection.anchor; - if (anchor.isEqual(active)) { - if (active.isBefore(selection.active)) { - anchor = anchor.translate(0, +1); - if (active.character > 0) { - active = active.translate(0, -1); - } - } - else { - if (anchor.character > 0) { - anchor = anchor.translate(0, -1); - } + if (active.isAfterOrEqual(anchor)) { + const lineLength = activeTextEditor.document.lineAt(active.line).text.length; + if (active.character < lineLength) { active = active.translate(0, +1); } } - else if (active.isAfter(anchor) && selection.active.isBefore(selection.anchor)) { + + if (active.isAfter(anchor) && selection.isReversed) { anchor = anchor.translate(0, -1); } - else if (active.isBefore(anchor) && selection.active.isAfter(selection.anchor)) { + else if (active.isBefore(anchor) && !selection.isReversed) { anchor = anchor.translate(0, +1); } }