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

Custom highlight support [WIP] #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mcclure
Copy link

@mcclure mcclure commented Dec 9, 2024

My attempt at #87 .

This adds new public functions to TextArea:

pub fn custom_highlight(
    &mut self,
    range: ((usize, usize), (usize, usize)),
    style: Style,
    priority: u8,
);

pub fn clear_custom_highlight(&mut self);

This works, I'm actually already using it in a working program (while prototyping this lives in that repo as cargo run --example vim). However, there are two high-level issues:

  1. I need feedback as to whether you like this API or whether you'd be more likely to accept a different one. The "priority" in particular is really iffy. I (see highlight.rs:26) changed the existing priorities to 10, 20, 30, so there's lots of space between them, but that seems… I mean, it works, but it might be awkward if you add more builtins later and have to populate the priorities in between.

  2. This code makes it really easy to create "overlapping regions". Textarea currently fails on this because it treats "end" like a stack. I don't believe this is a problem with this PR but a problem with the underlying code, as I think(?) I can recreate it by creating a search and a highlight at the same time (see my note in Feature request: Set style on range #87).

But look what happens when you try to use this patch:

Screencast.from.2024-12-08.20-17-29.webm

This program (the "ami" prototype I linked above) lets you input a song in a little music "programming language". If there is anything it doesn't recognize (basically anything other than a number) it highlights the first bad character in red. To test ranges, I changed the code so instead of highlighting one character it highlights the entire row after the highlighted character. There are two bad things I see here: One, the final character of the line is blue, and I don't know why; two, when the cursor starts overlapping (underlapping, the cursor priority is 30 and the error priority is 35) everything freaks out.

Minor issues: I haven't written tests yet, but if you tell me an external API you're happy with, I can do that; there is an outstanding clippy, an IMO spurious one (there's an eight-argument function and it thinks 7 should be the limit), I could refactor that but again want to know what final external API I'm targeting.

…he text area.

External API is pub fn custom_highlight(&mut self, range:((usize, usize), (usize, usize)), style:Style, priority:u8)
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 this pull request may close these issues.

1 participant