Skip to content

Commit e90c3a9

Browse files
authored
Merge pull request #846 from Jugen/code_area_enhancement
Enhancements to CodeArea and JavaKeywordsDemo
2 parents 3f8f305 + 7766ec2 commit e90c3a9

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsDemo.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import java.util.regex.Pattern;
88

99
import javafx.application.Application;
10+
import javafx.application.Platform;
1011
import javafx.scene.Scene;
12+
import javafx.scene.input.KeyCode;
13+
import javafx.scene.input.KeyEvent;
1114
import javafx.scene.layout.StackPane;
1215
import javafx.stage.Stage;
1316

@@ -103,6 +106,20 @@ public void start(Stage primaryStage) {
103106
// when no longer need syntax highlighting and wish to clean up memory leaks
104107
// run: `cleanupWhenNoLongerNeedIt.unsubscribe();`
105108

109+
110+
// auto-indent: insert previous line's indents on enter
111+
final Pattern whiteSpace = Pattern.compile( "^\\s+" );
112+
codeArea.addEventHandler( KeyEvent.KEY_PRESSED, KE ->
113+
{
114+
if ( KE.getCode() == KeyCode.ENTER ) {
115+
int caretPosition = codeArea.getCaretPosition();
116+
int currentParagraph = codeArea.getCurrentParagraph();
117+
Matcher m0 = whiteSpace.matcher( codeArea.getParagraph( currentParagraph-1 ).getSegments().get( 0 ) );
118+
if ( m0.find() ) Platform.runLater( () -> codeArea.insertText( caretPosition, m0.group() ) );
119+
}
120+
});
121+
122+
106123
codeArea.replaceText(0, 0, sampleCode);
107124

108125
Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);

richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.fxmisc.richtext;
22

3-
3+
import java.text.BreakIterator;
44
import java.util.Collection;
55

66
import 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

Comments
 (0)