Skip to content

Commit

Permalink
Merge pull request VSCodeVim#138 from johnfn/fix-word-motion
Browse files Browse the repository at this point in the history
Fix word and back-word motions, and fix tests.
  • Loading branch information
jpoon committed Feb 9, 2016
2 parents 51fe12f + 374166c commit d086ccb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/motion/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ export class Position extends vscode.Position {
}
}

return this.getLineBegin();
if (this.line === this.getDocumentEnd().line) {
return this.getLineEnd();
} else {
return new Position(this.line + 1, 0, this.positionOptions);
}
}

public getWordRight() : Position {
Expand All @@ -117,11 +121,11 @@ export class Position extends vscode.Position {
return new Position(line.lineNumber, line.firstNonWhitespaceCharacterIndex, this.positionOptions);
}

let line = TextEditor.getLineAt(this);
let line = TextEditor.getLineAt(this);
let words = line.text.match(this._nonWordCharRegex);

let startWord: number;
let endWord: number;
let endWord : number;

if (words) {
for (var index = 0; index < words.length; index++) {
Expand All @@ -137,7 +141,11 @@ export class Position extends vscode.Position {
}
}

return this.getLineEnd();
if (this.line === this.getDocumentEnd().line) {
return this.getLineEnd();
} else {
return new Position(this.line + 1, 0, this.positionOptions);
}
}

public getCurrentWordEnd(): Position {
Expand Down
45 changes: 40 additions & 5 deletions test/motion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {setupWorkspace, cleanUpWorkspace} from './testUtils';

suite("motion", () => {
let motionModes = [MotionMode.Caret, MotionMode.Cursor];
let text: Array<string> = [
let text: string[] = [
"mary had",
"a",
"little lamb",
Expand Down Expand Up @@ -193,17 +193,52 @@ suite("motion", () => {
assert.equal(motion.position.character, text[text.length - 1].length);
});

test("line begin cursor on first non-blank character", () => {
let motion = new Motion(MotionMode.Caret).move(3, 3).firstLineNonBlankChar();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 0);
});

test("last line begin cursor on first non-blank character", () => {
let motion = new Motion(MotionMode.Caret).move(0, 0).lastLineNonBlankChar();
assert.equal(motion.position.line, 3);
assert.equal(motion.position.character, 1);
});
});

suite("word motion", () => {
let text: string[] = [
"mary had",
"a",
"little lamb",
" hose fleece wassss"
];

suiteSetup(() => {
return setupWorkspace().then(() => {
return TextEditor.insert(text.join('\n'));
});
});

suiteTeardown(cleanUpWorkspace);

suite("word right", () => {
test("move to word right", () => {
let motion = new Motion(MotionMode.Caret).move(0, 0).wordRight();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 5);
});

test("last word should stay on line at last character", () => {
test("last word should move to next line", () => {
let motion = new Motion(MotionMode.Caret).move(0, 6).wordRight();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 7);
assert.equal(motion.position.line, 1);
assert.equal(motion.position.character, 0);
});

test("last word on last line should go to end of document (special case!)", () => {
let motion = new Motion(MotionMode.Caret).move(3, 14).wordRight();
assert.equal(motion.position.line, 3);
assert.equal(motion.position.character, 18);
});

test("end of line should move to next word on next line", () => {
Expand Down Expand Up @@ -307,4 +342,4 @@ suite("paragraph motion", () => {
assert.equal(motion.position.character, 0);
});
});
});
});

0 comments on commit d086ccb

Please sign in to comment.