11package org .fxmisc .richtext ;
22
3+ import static java .lang .Character .isWhitespace ;
34import static javafx .scene .input .KeyCode .*;
45import static javafx .scene .input .KeyCombination .*;
56import static org .fxmisc .richtext .model .TwoDimensional .Bias .*;
@@ -107,12 +108,12 @@ class StyledTextAreaBehavior {
107108 anyOf (
108109 keyPressed (RIGHT , SHORTCUT_DOWN ),
109110 keyPressed (KP_RIGHT , SHORTCUT_DOWN )
110- ), (b , e ) -> b .model . wordBreaksForwards ( 2 , SelectionPolicy .CLEAR )),
111+ ), (b , e ) -> b .skipToNextWord ( SelectionPolicy .CLEAR )),
111112 consume (
112113 anyOf (
113114 keyPressed (LEFT , SHORTCUT_DOWN ),
114115 keyPressed (KP_LEFT , SHORTCUT_DOWN )
115- ), (b , e ) -> b .model . wordBreaksBackwards ( 2 , SelectionPolicy .CLEAR )),
116+ ), (b , e ) -> b .skipToPrevWord ( SelectionPolicy .CLEAR )),
116117 consume (keyPressed (HOME , SHORTCUT_DOWN ), (b , e ) -> b .model .start (SelectionPolicy .CLEAR )),
117118 consume (keyPressed (END , SHORTCUT_DOWN ), (b , e ) -> b .model .end (SelectionPolicy .CLEAR )),
118119 // selection
@@ -134,12 +135,12 @@ class StyledTextAreaBehavior {
134135 anyOf (
135136 keyPressed (RIGHT , SHIFT_DOWN , SHORTCUT_DOWN ),
136137 keyPressed (KP_RIGHT , SHIFT_DOWN , SHORTCUT_DOWN )
137- ), (b , e ) -> b .model . wordBreaksForwards ( 2 , selPolicy )),
138+ ), (b , e ) -> b .skipToNextWord ( selPolicy )),
138139 consume (
139140 anyOf (
140141 keyPressed (LEFT , SHIFT_DOWN , SHORTCUT_DOWN ),
141142 keyPressed (KP_LEFT , SHIFT_DOWN , SHORTCUT_DOWN )
142- ), (b , e ) -> b .model . wordBreaksBackwards ( 2 , selPolicy )),
143+ ), (b , e ) -> b .skipToPrevWord ( selPolicy )),
143144 consume (keyPressed (A , SHORTCUT_DOWN ), (b , e ) -> b .model .selectAll ())
144145 );
145146
@@ -361,6 +362,27 @@ private void nextLine(SelectionPolicy selectionPolicy) {
361362 downLines (selectionPolicy , 1 );
362363 }
363364
365+ private void skipToPrevWord (SelectionPolicy selectionPolicy ) {
366+ int caretPos = model .getCaretPosition ();
367+
368+ // if (0 == caretPos), do nothing as can't move to the left anyway
369+ if (1 <= caretPos ) {
370+ boolean prevCharIsWhiteSpace = isWhitespace (model .getText (caretPos - 1 , caretPos ).charAt (0 ));
371+ model .wordBreaksBackwards (prevCharIsWhiteSpace ? 2 : 1 , selectionPolicy );
372+ }
373+ }
374+
375+ private void skipToNextWord (SelectionPolicy selectionPolicy ) {
376+ int caretPos = model .getCaretPosition ();
377+ int length = model .getLength ();
378+
379+ // if (caretPos == length), do nothing as can't move to the right anyway
380+ if (caretPos <= length - 1 ) {
381+ boolean nextCharIsWhiteSpace = isWhitespace (model .getText (caretPos , caretPos + 1 ).charAt (0 ));
382+ model .wordBreaksForwards (nextCharIsWhiteSpace ? 2 : 1 , selectionPolicy );
383+ }
384+ }
385+
364386 /* ********************************************************************** *
365387 * Mouse handling implementation *
366388 * ********************************************************************** */
0 commit comments