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

Feature request: Set style on range #87

Open
mcclure opened this issue Nov 9, 2024 · 2 comments
Open

Feature request: Set style on range #87

mcclure opened this issue Nov 9, 2024 · 2 comments

Comments

@mcclure
Copy link

mcclure commented Nov 9, 2024

Hello, I am interested in using tui-textarea. However I have a need. For my application, it must be possible to apply a style to a range of text. For example, maybe I am implementing syntax highlighting in a programming language. Or maybe I am implementing a spell checker and I want misspelled words to be highlighted. Or maybe I want the word under the cursor to always highlight.

Is this possible? Reading the docs, tui-textarea currently there is styling for specific special items known to tui-textarea, such as the search highlight or the cursor or the selection. But it seems like I cannot just say "highlight characters 4 through 7 on line 5". (Ideally, my custom "range" highlight should combine with the existing style types intelligently— for example if I set characters 4-7 as red, and the cursor character as an underline, and I move the cursor to position 5, that character should be red and underlined).

There is already a feature request for syntax highlighting. But I think this is a different request, simpler and more basic. Syntax highlighting controlled by tui-textarea would not allow me to style special ranges of text with meaning known only to my application.

@mcclure
Copy link
Author

mcclure commented Dec 8, 2024

This feature is a must-have for my application (I need at least two additional highlight ranges beyond what base TextArea offers) so I am trying to prototype it… I'm doing this via an extra_highlights: Vec<(usize, usize, usize, Style)> /* line_idx, from_char, to_char, style */ which is a member of TextArea and gets passed down into LineHighlighter.

I am getting tripped up because I do not quite understand what the Boundary/Span system is or how it works. Gonna just type out what I think is happening:

  • A boundary is a highlight kind (from an enum) plus a position. A span is a style plus a string to display.
  • Each of the bespoke highlight types (cursor, select, search) are added to a list.
  • The list is sorted.
  • The list is traversed, creating a stack of active highlights, as you go you snarf text off the line and put it plus the appropriate style into the span list.

This makes sense, what I don't get is:

  • I don't understand how you handle endings. One of the boundary types is "End". Okay, so you hit an end and you pop a layer off the stack. Fine. But what if the boundaries have non-stacklike behavior? For example I could imagine a line that looks like

Jack[selection begins]daws lo[search begins]ve my bi[selection ends]g sphinx of qua[search ends]rtz.

I haven't yet found where/how this case is handled.

I will look at it closer tomorrow but any guidance appreciated!

@mcclure
Copy link
Author

mcclure commented Dec 8, 2024

Actually, maybe I don't need to understand how it works. I just added my boundaries to LineHighlighter and it seems to work. I will likely drop a first-pass PR soon. I am still curious what happens if I set "overlapping" ranges (as in my sphinx example above) however.

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

1 participant