Skip to content

Commit

Permalink
Added auto-indent on enter to JavaKeywordsDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurgen committed Aug 25, 2019
1 parent f1043be commit 7766ec2
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7766ec2

Please sign in to comment.