11package org .fxmisc .richtext ;
22
3-
3+ import java . text . BreakIterator ;
44import java .util .Collection ;
55
66import javafx .beans .NamedArg ;
@@ -21,7 +21,7 @@ public class CodeArea extends StyleClassedTextArea {
2121 // don't apply preceding style to typed text
2222 setUseInitialStyleForInsertion (true );
2323 }
24-
24+
2525 /**
2626 * Creates an area that can render and edit the same {@link EditableStyledDocument} as another {@link CodeArea}.
2727 */
@@ -52,4 +52,50 @@ public CodeArea(@NamedArg("text") String text) {
5252 // position the caret at the beginning
5353 selectRange (0 , 0 );
5454 }
55+
56+ @ Override // to select words containing underscores
57+ public void selectWord ()
58+ {
59+ if ( getLength () == 0 ) return ;
60+
61+ CaretSelectionBind <?,?,?> csb = getCaretSelectionBind ();
62+ int paragraph = csb .getParagraphIndex ();
63+ int position = csb .getColumnPosition ();
64+
65+ String paragraphText = getText ( paragraph );
66+ BreakIterator breakIterator = BreakIterator .getWordInstance ();
67+ breakIterator .setText ( paragraphText );
68+
69+ breakIterator .preceding ( position );
70+ int start = breakIterator .current ();
71+
72+ while ( start > 0 && paragraphText .charAt ( start -1 ) == '_' )
73+ {
74+ if ( --start > 0 && ! breakIterator .isBoundary ( start -1 ) )
75+ {
76+ breakIterator .preceding ( start );
77+ start = breakIterator .current ();
78+ }
79+ }
80+
81+ breakIterator .following ( position );
82+ int end = breakIterator .current ();
83+ int len = paragraphText .length ();
84+
85+ while ( end < len && paragraphText .charAt ( end ) == '_' )
86+ {
87+ if ( ++end < len && ! breakIterator .isBoundary ( end +1 ) )
88+ {
89+ breakIterator .following ( end );
90+ end = breakIterator .current ();
91+ }
92+ // For some reason single digits aren't picked up so ....
93+ else if ( Character .isDigit ( paragraphText .charAt ( end ) ) )
94+ {
95+ end ++;
96+ }
97+ }
98+
99+ csb .selectRange ( paragraph , start , paragraph , end );
100+ }
55101}
0 commit comments