FXMisc / RichTextFX

Rich-text area for JavaFX
BSD 2-Clause "Simplified" License
1.21k stars 237 forks source link

Question: What is the strategy to implement Highlight All for found matches for modifiable text? #1227

Closed PavelTurk closed 4 months ago

PavelTurk commented 4 months ago

I have a CodeArea with text that can be modified by user. Now, I want to implement Highlight All for all found matches (for search).

As I understand for all found matches I do codeArea. setStyle​(from, to, style). But, if the user updates some match so that it shouldn't be highlighted anymore then how should I control what text was modified to remove highlighting? What if user pastes some text inside found match or deletes large block of text? Could anyone say how such things are done with RichTextFX?

PavelTurk commented 4 months ago

I was thinking about the following solution:

  1. For highlighting I do codeArea.setStyleClass(from, to, "highlighted");
  2. User makes changes in text
  3. I get event on textProperty. Using old value, new value and caret position I get information about text change.
  4. On both sides of the changed piece of text I do:
    var styles = stycodeArea.getStyleAtPosition(position);
    if styles contains my "highlighted" style class then 
    var range = codeArea.getStyleRangeAtPosition(position)
    update styles in this range ???

    Is this a correct way?

Jugen commented 4 months ago

Yes that sounds about right .....

PavelTurk commented 4 months ago

@Jugen Thank you very much for your answer. Do I understand it correctly that there is no low level API for getting text changes in RichTextFX? For example, I would like to get events - text added/removed/replaced with text and positions.

Jugen commented 4 months ago

You can use either plainTextChanges or richChanges which includes style changes.

PavelTurk commented 4 months ago

@Jugen Yes, this is what I was looking for. Thank you very much for your help.