Skip to content

Commit

Permalink
Enhanced CodeArea selection to include underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurgen committed Aug 25, 2019
1 parent 3f8f305 commit f1043be
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.fxmisc.richtext;


import java.text.BreakIterator;
import java.util.Collection;

import javafx.beans.NamedArg;
Expand All @@ -21,7 +21,7 @@ public class CodeArea extends StyleClassedTextArea {
// don't apply preceding style to typed text
setUseInitialStyleForInsertion(true);
}

/**
* Creates an area that can render and edit the same {@link EditableStyledDocument} as another {@link CodeArea}.
*/
Expand Down Expand Up @@ -52,4 +52,50 @@ public CodeArea(@NamedArg("text") String text) {
// position the caret at the beginning
selectRange(0, 0);
}

@Override // to select words containing underscores
public void selectWord()
{
if ( getLength() == 0 ) return;

CaretSelectionBind<?,?,?> csb = getCaretSelectionBind();
int paragraph = csb.getParagraphIndex();
int position = csb.getColumnPosition();

String paragraphText = getText( paragraph );
BreakIterator breakIterator = BreakIterator.getWordInstance();
breakIterator.setText( paragraphText );

breakIterator.preceding( position );
int start = breakIterator.current();

while ( start > 0 && paragraphText.charAt( start-1 ) == '_' )
{
if ( --start > 0 && ! breakIterator.isBoundary( start-1 ) )
{
breakIterator.preceding( start );
start = breakIterator.current();
}
}

breakIterator.following( position );
int end = breakIterator.current();
int len = paragraphText.length();

while ( end < len && paragraphText.charAt( end ) == '_' )
{
if ( ++end < len && ! breakIterator.isBoundary( end+1 ) )
{
breakIterator.following( end );
end = breakIterator.current();
}
// For some reason single digits aren't picked up so ....
else if ( Character.isDigit( paragraphText.charAt( end ) ) )
{
end++;
}
}

csb.selectRange( paragraph, start, paragraph, end );
}
}

0 comments on commit f1043be

Please sign in to comment.