-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.