From f1043bed48d55458d2209321f33bc667f77f1f71 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Thu, 22 Aug 2019 18:52:58 +0200 Subject: [PATCH 1/2] Enhanced CodeArea selection to include underscore --- .../java/org/fxmisc/richtext/CodeArea.java | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java index 04c107562..a6e022ecf 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java @@ -1,6 +1,6 @@ package org.fxmisc.richtext; - +import java.text.BreakIterator; import java.util.Collection; import javafx.beans.NamedArg; @@ -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}. */ @@ -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 ); + } } From 7766ec22c92c227115429590071d9e57fc628f8d Mon Sep 17 00:00:00 2001 From: Jurgen Date: Sun, 25 Aug 2019 19:13:52 +0200 Subject: [PATCH 2/2] Added auto-indent on enter to JavaKeywordsDemo --- .../fxmisc/richtext/demo/JavaKeywordsDemo.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsDemo.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsDemo.java index f2cfc6a5e..9b2af293b 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsDemo.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsDemo.java @@ -7,7 +7,10 @@ import java.util.regex.Pattern; import javafx.application.Application; +import javafx.application.Platform; import javafx.scene.Scene; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; import javafx.scene.layout.StackPane; import javafx.stage.Stage; @@ -103,6 +106,20 @@ public void start(Stage primaryStage) { // when no longer need syntax highlighting and wish to clean up memory leaks // run: `cleanupWhenNoLongerNeedIt.unsubscribe();` + + // auto-indent: insert previous line's indents on enter + final Pattern whiteSpace = Pattern.compile( "^\\s+" ); + codeArea.addEventHandler( KeyEvent.KEY_PRESSED, KE -> + { + if ( KE.getCode() == KeyCode.ENTER ) { + int caretPosition = codeArea.getCaretPosition(); + int currentParagraph = codeArea.getCurrentParagraph(); + Matcher m0 = whiteSpace.matcher( codeArea.getParagraph( currentParagraph-1 ).getSegments().get( 0 ) ); + if ( m0.find() ) Platform.runLater( () -> codeArea.insertText( caretPosition, m0.group() ) ); + } + }); + + codeArea.replaceText(0, 0, sampleCode); Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);