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

Capital W/B word movement #147

Merged
merged 4 commits into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
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 src/action/deleteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class DeleteAction {
let isEOL = motion.position.isLineEnd();

await TextEditor.delete(range);

if (isEOL) {
return motion.left().move();
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/mode/modeNormal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export class NormalMode extends Mode {
"gg" : async (c) => { return c.firstLineNonBlankChar().move(); },
"G" : async (c) => { return c.lastLineNonBlankChar().move(); },
"w" : async (c) => { return c.wordRight().move(); },
"W" : async (c) => { return c.WORDRight().move(); },
Copy link
Member

Choose a reason for hiding this comment

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

Not a big fan of differentiating functions using casing. I understand that vim differentiates using word vs. WORD, but I'm wondering if there are alternatives?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I'm not usually a fan either. Though I figured in this case it would be the most obvious vocabulary for someone familiar with vim.

I took a look at various other projects to see what they use, and I think I like "Big Word" the best. Still short, and the connection can be made to WORD pretty easily and recalled easily. I'll make an update to the pull request.

"e" : async (c) => { return c.goToEndOfCurrentWord().move(); },
"b" : async (c) => { return c.wordLeft().move(); },
"B" : async (c) => { return c.WORDLeft().move(); },
"}" : async (c) => { return c.goToEndOfCurrentParagraph().move(); },
"{" : async (c) => { return c.goToBeginningOfCurrentParagraph().move(); },
"%" : async () => { return vscode.commands.executeCommand("editor.action.jumpToBracket"); },
Expand Down
12 changes: 12 additions & 0 deletions src/motion/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,24 @@ export class Motion implements vscode.Disposable {
return this;
}

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

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

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

public lineBegin() : Motion {
this._position = this.position.getLineBegin();
this._desiredColumn = this._position.character;
Expand Down
121 changes: 73 additions & 48 deletions src/motion/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@ export enum PositionOptions {

export class Position extends vscode.Position {
private static NonWordCharacters = "/\\()\"':,.;<>~!@#$%^&*|+=[]{}`?-";
private static NonWORDCharacters = "";
private static WordDelimiters: string[] = ["(", ")", "[", "]", "{", "}", ":", " ",
"=", "<", ">", "|", "/", "'", "\"", "~", "`", "@", "*", "+", "-", "?", ",", ".", ";"];

private _nonWordCharRegex : RegExp;
private _nonWORDCharRegex : RegExp;

public positionOptions: PositionOptions = null;

constructor(line: number, character: number, options: PositionOptions) {
super(line, character);

let segments = ["(^[\t ]*$)"];
segments.push(`([^\\s${_.escapeRegExp(Position.NonWordCharacters) }]+)`);
segments.push(`[\\s${_.escapeRegExp(Position.NonWordCharacters) }]+`);

this.positionOptions = options;
this._nonWordCharRegex = new RegExp(segments.join("|"), "g");

this._nonWordCharRegex = this.makeWordRegex(Position.NonWordCharacters);
this._nonWORDCharRegex = this.makeWordRegex(Position.NonWORDCharacters);
}

private makeWordRegex(characterSet: string) : RegExp {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: mind moving the f(x) around such that public comes first then privates?

let escaped = characterSet && _.escapeRegExp(characterSet);
let segments = ["(^[\t ]*$)"];
segments.push(`([^\\s${escaped}]+)`);
segments.push(`[${escaped}]+`);
return new RegExp(segments.join("|"), "g");
}

public setLocation(line: number, character: number) : Position {
Expand Down Expand Up @@ -78,78 +86,95 @@ export class Position extends vscode.Position {
return this;
}

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

if (!TextEditor.isFirstLine(this) && this.character <= currentLine.firstNonWhitespaceCharacterIndex) {
// go to previous line
let prevLine = new Position(this.line - 1, this.character, this.positionOptions);
return prevLine.getLineEnd();
// perform search from very end of previous line (after last character)
workingPosition = new Position(this.line - 1, this.character, this.positionOptions);
currentLine = TextEditor.getLineAt(workingPosition);
currentCharacter = workingPosition.getLineEnd().character + 1;
}

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

let startWord: number;
let endWord: number;

if (words) {
words = words.reverse();
endWord = line.range.end.character;
for (var index = 0; index < words.length; index++) {
endWord = endWord - words[index].length;
var word = words[index].trim();
if (word.length > 0) {
startWord = line.text.indexOf(word, endWord);

if (startWord !== -1 && this.character > startWord) {
return new Position(this.line, startWord, this.positionOptions);
}
}
let positions = [];

regex.lastIndex = 0;
while (true) {
let result = regex.exec(currentLine.text);
if (result === null) {
break;
}
positions.push(result.index);
}

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

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

public getWordRight() : Position {
if (!TextEditor.isLastLine(this) && this.character === this.getLineEnd().character) {
public getWordLeft() : Position {
return this.getWordLeftWithRegex(this._nonWordCharRegex);
}

public getWORDLeft() : Position {
return this.getWordLeftWithRegex(this._nonWORDCharRegex);
}

private getWordRightWithRegex(regex: RegExp) : 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);
let words = line.text.match(this._nonWordCharRegex);

let startWord: number;
let endWord : number;
let currentLine = TextEditor.getLineAt(this);
let positions = [];

if (words) {
for (var index = 0; index < words.length; index++) {
var word = words[index].trim();
if (word.length > 0) {
startWord = line.text.indexOf(word, endWord);
endWord = startWord + word.length;
regex.lastIndex = 0;
while (true) {
let result = regex.exec(currentLine.text);
if (result === null) {
break;
}
positions.push(result.index);
}

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

if (this.line === this.getDocumentEnd().line) {
return this.getLineEnd();
} else {
return new Position(this.line + 1, 0, this.positionOptions);
// go to next line
let line = TextEditor.getLineAt(this.translate(1));
return new Position(line.lineNumber, line.firstNonWhitespaceCharacterIndex, this.positionOptions);
}
}

public getWordRight() : Position {
return this.getWordRightWithRegex(this._nonWordCharRegex);
}

public getWORDRight() : Position {
return this.getWordRightWithRegex(this._nonWORDCharRegex);
}

public getCurrentWordEnd(): Position {
if (!TextEditor.isLastLine(this) && this.character === this.getLineEnd().character) {
// go to next line
Expand Down
82 changes: 60 additions & 22 deletions test/motion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ suite("motion", () => {

suite("word motion", () => {
let text: string[] = [
"mary had",
"a",
"little lamb",
" hose fleece wassss"
"if (true) {",
" return true;",
"} else {",
" return false;",
"} // endif"
];

suiteSetup(() => {
Expand All @@ -225,44 +226,81 @@ suite("word motion", () => {

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

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

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

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

suite("word left", () => {
test("move cursor word left", () => {
test("move cursor word left across spaces", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 3).wordLeft();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 0);
});

test("first word should move to previous line of end of line", () => {
let motion = new Motion(MotionMode.Caret).moveTo(2, 0).wordLeft();
assert.equal(motion.position.line, 1);
test("move cursor word left within word", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 5).wordLeft();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 4);
});

test("first word should move to previous line, beginning of last word", () => {
let motion = new Motion(MotionMode.Caret).moveTo(1, 2).wordLeft();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 10);
});

});

suite("WORD right", () => {
test("move to WORD right", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 3).WORDRight();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 10);
});

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

suite("WORD left", () => {
test("move cursor WORD left across spaces", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 3).WORDLeft();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 0);
});

test("move cursor WORD left within WORD", () => {
let motion = new Motion(MotionMode.Caret).moveTo(0, 5).WORDLeft();
assert.equal(motion.position.line, 0);
assert.equal(motion.position.character, 3);
});

test("first WORD should move to previous line, beginning of last WORD", () => {
let motion = new Motion(MotionMode.Caret).moveTo(2, 0).WORDLeft();
assert.equal(motion.position.line, 1);
assert.equal(motion.position.character, 9);
});
});


test("line begin cursor on first non-blank character", () => {
let motion = new Motion(MotionMode.Caret).moveTo(3, 3).firstLineNonBlankChar();
assert.equal(motion.position.line, 0);
Expand All @@ -271,8 +309,8 @@ suite("word motion", () => {

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

Expand Down