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

Syntax highlighting disappears if you copy some code and immediately paste it back #135

Closed
djskrien opened this issue Apr 2, 2015 · 7 comments

Comments

@djskrien
Copy link

djskrien commented Apr 2, 2015

Run the Java Keywords Demo. Select some of the text, type Ctrl-C to copy the selected text, and then type Ctrl-V to paste all the selected text back over itself. The syntax highlighting disappears for the pasted text.

@ghost
Copy link

ghost commented Apr 2, 2015

Would it get highlighted again after if you make any other key entry, space
for example? I think maybe the update does not listen to copy paste event,
only key entry.
On Apr 2, 2015 8:20 AM, "djskrien" notifications@github.com wrote:

Run the Java Keywords Demo. Select some of the text, type Ctrl-C to copy
the selected text, and then type Ctrl-V to paste all the selected text back
over itself. The syntax highlighting disappears for the pasted text.


Reply to this email directly or view it on GitHub
#135.

@djskrien
Copy link
Author

djskrien commented Apr 2, 2015

Yes, if you make another key entry after pasting, the highlighting reappears. In fact, the highlighting reappears even after pasting as long as you are not pasting back exactly the same code that is already there. This suggests that the highlighting is done by a ChangeListener of the textProperty of the CodeArea. Unfortunately, the ChangeListener does nothing if the old text matches the new text.

@TomasMikula
Copy link
Member

Good catch! Syntax highlighting is listening to (plain) text changes, and in this case, the text content does not change. Combined with the fact that Copy&Paste does not preserve styling, you get a change in style which is not detected by the parser.

@djskrien
Copy link
Author

djskrien commented Apr 2, 2015

Is there an easy fix for this problem?

@TomasMikula
Copy link
Member

Yes, you can observe richChanges() and trigger highlighting from there:

codeArea.richChanges().subscribe(change -> {
    codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText()));
});

@djskrien
Copy link
Author

djskrien commented Apr 2, 2015

I'm missing something. I replaced the code on lines 78-80 in JavaKeywords.java:

codeArea.textProperty().addListener((obs, oldText, newText) -> {
    codeArea.setStyleSpans(0, computeHighlighting(newText));
});

with the code you gave above:

codeArea.richChanges().subscribe(change -> {
    codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText()));
});

and tried to run it, but got infinite recursion.

@TomasMikula
Copy link
Member

You're not missing anything, it is a bug. richChanges() keeps reporting a change even if setStyleSpans did not result in an actual change. In the meantime, use an invalidation listener on textProperty. It gets invalidated even if there is no change in the end, so it will work to eliminate the original problem:

codeArea.textProperty().addListener(obs -> {
    codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText()));
});

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

No branches or pull requests

2 participants