Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug - CodeArea w/ LineNumberFactory throws when replacing multi-line text with .replaceText() #1021

Closed
KhalilSantana opened this issue Jun 17, 2021 · 1 comment · Fixed by #1022

Comments

@KhalilSantana
Copy link

Expected Behavior

Calling CodeArea.replaceText() should replace the contents of the CodeArea without throwing any exception.

Actual Behavior

When the CodeArea contains multiple lines, calling CodeArea.replaceText() triggers an exception, show below:

Button pressed
Button pressed
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Visible paragraphs' last index is [-1] but visibleParIndex was [0]
	at org.fxmisc.richtext.GenericStyledArea.visibleParToAllParIndex(GenericStyledArea.java:982)
	at org.fxmisc.richtext.ViewActions.firstVisibleParToAllParIndex(ViewActions.java:284)
	at org.fxmisc.richtext.LineNumberFactory.insertParagraphCheck(LineNumberFactory.java:172)
	at org.fxmisc.richtext.LineNumberFactory.lambda$new$2(LineNumberFactory.java:92)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
	at java.base/java.lang.Thread.run(Thread.java:831)
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Visible paragraphs' last index is [-1] but visibleParIndex was [0]
	at org.fxmisc.richtext.GenericStyledArea.visibleParToAllParIndex(GenericStyledArea.java:982)
	at org.fxmisc.richtext.ViewActions.firstVisibleParToAllParIndex(ViewActions.java:284)
	at org.fxmisc.richtext.LineNumberFactory.deleteParagraphCheck(LineNumberFactory.java:148)
	at org.fxmisc.richtext.LineNumberFactory.lambda$new$1(LineNumberFactory.java:91)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
	at java.base/java.lang.Thread.run(Thread.java:831)

Reproducible Demo

Run this, then press the button twice.

Use the following template to get started.

public class App extends Application {
    CodeArea codeArea;
    Button button;

    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        codeArea = new CodeArea();
        codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
        codeArea.setWrapText(false);

        button = new Button("Press me Twice");
        button.setOnAction(actionEvent -> {
            actionEvent.consume();
            System.out.println("Button pressed");
            codeArea.clear();
            // Doesn't really matter what is inside codeArea text,
            // As long as it span multiple lines, the exception is thrown
            codeArea.appendText("Lorem ipsum\n");
            // If you remove the \n from above it won't throw an exception anymore
            codeArea.replaceText(String.valueOf(Math.random()));
        });

        Parent root = new StackPane(codeArea, button);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

Environment info:

  • RichTextFX Version: 0.10.6 from Maven
  • Operating System: Windows 21H1 and ArchLinux
  • Java version: 16.0.1 (Microsoft and ArchLinux's OpenJDK builds)

As this bug is reproducible on multiple OSes and JDK builds, I don't think this is platform dependent.

@Jugen
Copy link
Collaborator

Jugen commented Jun 17, 2021

Hi @KhalilSantana, thank you very much for the bug report and the reproducible demo.
I have submitted a PR to address this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants