Skip to content

Commit

Permalink
Make word navigation more consistent.
Browse files Browse the repository at this point in the history
Always skip two word boundaries.
Fixes #69.
  • Loading branch information
TomasMikula committed Jul 8, 2014
1 parent 533cba1 commit 2462af9
Showing 1 changed file with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,39 +108,32 @@ default void nextChar(SelectionPolicy selectionPolicy) {


/**
* Moves the caret to the beginning of preceding word.
* Skips two word boundaries backwards.
* Based on the given selection policy, anchor either moves with
* the caret, stays put, or moves to the former caret position.
*/
default void previousWord(SelectionPolicy selectionPolicy) {
int textLength = getLength();
if (textLength == 0)
if(getLength() == 0) {
return;
}

String text = getText();
BreakIterator wordBreakIterator = BreakIterator.getWordInstance();
wordBreakIterator.setText(text);

int pos = wordBreakIterator.preceding(getCaretPosition());
if(pos != BreakIterator.DONE &&
!Character.isLetter(text.charAt(pos))) {
// we ended at the end of the word, skip to the beginning
wordBreakIterator.preceding(pos);
}
wordBreakIterator.setText(getText());
wordBreakIterator.preceding(getCaretPosition());
wordBreakIterator.previous();

// move/select
moveTo(wordBreakIterator.current(), selectionPolicy);
}

/**
* Moves the caret to the beginning of the following word.
* Skips two word boundaries forward.
* Based on the given selection policy, anchor either moves with
* the caret, stays put, or moves to the former caret position.
*/
default void nextWord(SelectionPolicy selectionPolicy) {
int textLength = getLength();
if (textLength == 0)
if(getLength() == 0) {
return;
}

BreakIterator wordBreakIterator = BreakIterator.getWordInstance();
wordBreakIterator.setText(getText());
Expand Down

0 comments on commit 2462af9

Please sign in to comment.