Skip to content

Commit

Permalink
Handle move cursor in visual mode correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kou Tenichi committed Apr 6, 2017
1 parent 650bb6d commit 5ee0113
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/Actions/MoveCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 5ee0113

Please sign in to comment.