Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add E (end of WORD), and fix up e (end of word). #160

Merged
merged 7 commits into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Status | Key | Description
:white_check_mark: | w | words forward
:white_check_mark: | W | N blank-separated WORDS forward
:white_check_mark: | e | forward to the end of the word
| E | forward to the end of the Nth blank-separated WORD
:white_check_mark: | E | forward to the end of the Nth blank-separated WORD
:white_check_mark: | b | words backward
:white_check_mark: | B | N blank-separated WORDS backward
| ge | backward to the end of the Nth word
Expand Down
1 change: 1 addition & 0 deletions src/mode/modeNormal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class NormalMode extends Mode {
"w" : async (c) => { return c.wordRight().move(); },
"W" : async (c) => { return c.bigWordRight().move(); },
"e" : async (c) => { return c.goToEndOfCurrentWord().move(); },
"E" : async (c) => { return c.goToEndOfCurrentBigWord().move(); },
"b" : async (c) => { return c.wordLeft().move(); },
"B" : async (c) => { return c.bigWordLeft().move(); },
"}" : async (c) => { return c.goToEndOfCurrentParagraph().move(); },
Expand Down
6 changes: 6 additions & 0 deletions src/motion/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ export class Motion implements vscode.Disposable {
return this;
}

public goToEndOfCurrentBigWord(): Motion {
this._position = this.position.getCurrentBigWordEnd();
this._desiredColumn = this._position.character;
return this;
}

public goToEndOfCurrentParagraph(): Motion {
this._position = this.position.getCurrentParagraphEnd();
this._desiredColumn = this.position.character;
Expand Down
69 changes: 44 additions & 25 deletions src/motion/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export enum PositionOptions {
export class Position extends vscode.Position {
private static NonWordCharacters = "/\\()\"':,.;<>~!@#$%^&*|+=[]{}`?-";
private static NonBigWordCharacters = "";
private static WordDelimiters: string[] = ["(", ")", "[", "]", "{", "}", ":", " ",
"=", "<", ">", "|", "/", "'", "\"", "~", "`", "@", "*", "+", "-", "?", ",", ".", ";"];

private _nonWordCharRegex : RegExp;
private _nonBigWordCharRegex : RegExp;
Expand Down Expand Up @@ -95,25 +93,11 @@ export class Position extends vscode.Position {
}

public getCurrentWordEnd(): Position {
if (!TextEditor.isLastLine(this) && this.character === this.getLineEnd().character) {
// go to next line
let line = TextEditor.getLineAt(this.translate(1));
return new Position(line.lineNumber, line.firstNonWhitespaceCharacterIndex, this.positionOptions);
}

let line = TextEditor.getLineAt(this);

if (Position.WordDelimiters.indexOf(line.text.charAt(this.character)) !== -1) {
return new Position(this.line, this.character + 1, this.positionOptions);
}

for (var index = this.character; index < line.text.length; index++) {
if (Position.WordDelimiters.indexOf(line.text.charAt(index)) !== -1) {
return new Position(this.line, index, this.positionOptions);
}
}
return this.getCurrentWordEndWithRegex(this._nonWordCharRegex);
}

return this.getLineEnd();
public getCurrentBigWordEnd(): Position {
return this.getCurrentWordEndWithRegex(this._nonBigWordCharRegex);
}

/**
Expand Down Expand Up @@ -240,9 +224,9 @@ export class Position extends vscode.Position {
}

private getWordLeftWithRegex(regex: RegExp) : Position {
var workingPosition = new Position(this.line, this.character, this.positionOptions);
var currentLine = TextEditor.getLineAt(this);
var currentCharacter = this.character;
let workingPosition = new Position(this.line, this.character, this.positionOptions);
let currentLine = TextEditor.getLineAt(this);
let currentCharacter = this.character;

if (!TextEditor.isFirstLine(this) && this.character <= currentLine.firstNonWhitespaceCharacterIndex) {
// perform search from very end of previous line (after last character)
Expand All @@ -262,7 +246,7 @@ export class Position extends vscode.Position {
positions.push(result.index);
}

for (var index = 0; index < positions.length; index++) {
for (let index = 0; index < positions.length; index++) {
let position = positions[positions.length - 1 - index];
if (currentCharacter > position) {
return new Position(workingPosition.line, position, workingPosition.positionOptions);
Expand Down Expand Up @@ -296,7 +280,7 @@ export class Position extends vscode.Position {
positions.push(result.index);
}

for (var index = 0; index < positions.length; index++) {
for (let index = 0; index < positions.length; index++) {
let position = positions[index];
if (this.character < position) {
return new Position(this.line, position, this.positionOptions);
Expand All @@ -311,4 +295,39 @@ export class Position extends vscode.Position {
return new Position(line.lineNumber, line.firstNonWhitespaceCharacterIndex, this.positionOptions);
}
}

private getCurrentWordEndWithRegex(regex: RegExp) : Position {
let workingPosition = new Position(this.line, this.character, this.positionOptions);
let currentLine = TextEditor.getLineAt(this);
let currentCharacter = this.character;

if (!TextEditor.isLastLine(this) && this.character >= this.getLineEnd().character) {
// go to next line
workingPosition = new Position(this.line + 1, this.character, this.positionOptions);
currentLine = TextEditor.getLineAt(workingPosition);
currentCharacter = 0;
}

let positions = [];

regex.lastIndex = 0;
while (true) {
let result = regex.exec(currentLine.text);
if (result === null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we get into an infinite loop.

break;
}
positions.push(result.index + result[0].length - 1);
}

for (let index = 0; index < positions.length; index++) {
let position = positions[index];
if (currentCharacter < position) {
return new Position(workingPosition.line, position, workingPosition.positionOptions);
}
}

// Wasn't at end of the line (of not last line), and couldn't find a column in the middle of the string?
// We'll just go to the end of the line?
return this.getLineEnd();
}
}
39 changes: 39 additions & 0 deletions test/motion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,45 @@ suite("word motion", () => {
});
});

suite("end of word right", () => {
test("move to end of current word right", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 4).goToEndOfCurrentWord();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 7);
});

test("move to end of next word right", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 7).goToEndOfCurrentWord();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 8);
});

test("end of last word should move to next line", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 10).goToEndOfCurrentWord();
assert.equal(motion.position.line, 1);
assert.equal(motion.position.character, 7);
});
});

suite("end of WORD right", () => {
test("move to end of current WORD right", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 4).goToEndOfCurrentBigWord();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 8);
});

test("move to end of next WORD right", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 8).goToEndOfCurrentBigWord();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 10);
});

test("end of last WORD should move to next line", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 10).goToEndOfCurrentBigWord();
assert.equal(motion.position.line, 1);
assert.equal(motion.position.character, 7);
});
});

test("line begin cursor on first non-blank character", () => {
let motion = new Motion(MotionMode.Caret).moveTo(3, 3).firstLineNonBlankChar();
Expand Down