Skip to content

Commit

Permalink
Fix behaviour at line end.
Browse files Browse the repository at this point in the history
  • Loading branch information
aioutecism committed Jan 3, 2016
1 parent fd676a0 commit 7e77109
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/Actions/MoveCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import {MotionCharacter} from '../Motions/Character';

export class ActionMoveCursor {

static byMotions(args: {motions: Motion[], isVisualMode?: boolean, isVisualLineMode?: boolean}): Thenable<boolean> {
static byMotions(args: {
motions: Motion[],
isVisualMode?: boolean,
isVisualLineMode?: boolean,
noEmptyAtLineEnd?: boolean
}): Thenable<boolean> {
args.isVisualMode = args.isVisualMode === undefined ? false : args.isVisualMode;
args.isVisualLineMode = args.isVisualLineMode === undefined ? false : args.isVisualLineMode;
args.noEmptyAtLineEnd = args.noEmptyAtLineEnd === undefined ? false : args.noEmptyAtLineEnd;

const activeTextEditor = window.activeTextEditor;

Expand All @@ -17,6 +23,8 @@ export class ActionMoveCursor {

// TODO: Preserve character position

const document = activeTextEditor.document;

activeTextEditor.selections = activeTextEditor.selections.map(selection => {
let anchor: Position;

Expand Down Expand Up @@ -55,6 +63,13 @@ export class ActionMoveCursor {
}
}
else {
if (args.noEmptyAtLineEnd) {
const lineEndCharacter = document.lineAt(active.line).text.length;
if (lineEndCharacter !== 0 && active.character === lineEndCharacter) {
active = active.translate(0, -1);
}
}

anchor = active;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Modes/Normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ModeNormal extends Mode {
name = 'NORMAL';

private maps: CommandMap[] = [
{ keys: '{motion}', command: ActionMoveCursor.byMotions },
{ keys: '{motion}', command: ActionMoveCursor.byMotions, args: {noEmptyAtLineEnd: true} },

{ keys: 'i', command: ActionMode.toInsert },
{ keys: 'I', command: () => ActionMoveCursor.byMotions({motions: [MotionLine.firstNonBlank()]}).then(ActionMode.toInsert) },
Expand Down
2 changes: 1 addition & 1 deletion src/Motions/Motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Motion {
toCharacter = Infinity;
}

toCharacter = Math.min(toCharacter, document.lineAt(toLine).text.length - 1);
toCharacter = Math.min(toCharacter, document.lineAt(toLine).text.length);
toCharacter = Math.max(toCharacter, 0);

return new Position(toLine, toCharacter);
Expand Down

0 comments on commit 7e77109

Please sign in to comment.