clutcher / bh

Issue tracker for Better Highlights Intellij IDEA plugin
7 stars 0 forks source link

Allow regexp highlighting by capture groups #93

Closed aerontek closed 8 months ago

aerontek commented 8 months ago

I'm not sure if this is already available in the plugin, but I couldn't find out how to do it. Please let me know if I've just missed it somewhere.

Using regexp, I'd like to be able to highlight a surrounding structure with a particular color while leaving the interior unchanged. For example, consider this test case annotation in Rust:

#[case("myString", 42, false)]

I'd like to be able to define the regexp ^\s*#\[case\(.*\)\]\n$ and have it highlight #[case( and )] in whatever color I pick, but leave the interior text "myString", 42, false as its normal highlighting. I don't really care how I would need to define the "interior" part, perhaps a specifically delimited capture group?

I don't think this would need to decrease performance in any meaningful way since it is not performing additional regexp searches, but rather its just changing the start/stop positions of the highlighting.

As far as I can think for my own purposes, this usage would be primarily helpful for the things JetBrains does not yet highlight (such as crate-specific syntax like #[case(..)] from the rstest crate in Rust).

clutcher commented 8 months ago

@aerontek Technically it is not a problem to implement highlight by regexp group, but it becomes complicated from UI settings standpoint.

Right now I tend to implement automatic behavior to highlight last capture group, so there would be no need to do any settings UI change. Would it fit all your use cases?

clutcher commented 8 months ago

@aerontek Released in 2024.1.1. You can reopen it if needed.

aerontek commented 8 months ago

@clutcher Thanks for this! What you've launched is usable for me, though I'll suggest an improvement below. Thanks for the quick turn-around, much appreciated!

Ideally, we would have some simple checkbox like "Only highlight capture groups". When enabled, any capture group would be highlighted, but the rest of the pattern would not. When disabled, the whole pattern is highlighted. This could also be a dropdown or radio with two options for a bit more clarity: "Only highlight capture groups" and "Highlight entire pattern"

In my case, the current method of only highlighting the last capture group requires me to define two RegExp to get what I want. My use case is fairly simple... I have test case annotations like #[case( ... )]. I want the leading #[case( and trailing )] to be highlighted, but not the inner ... (whatever that might be).

Ideally, I'd create one RegExp pattern with two capture groups like (#\[case\().*(\)\])\n and check the "Only highlight capture groups" setting. That way, the leading and trailing parts are highlighted, but the inner part of the pattern is not. Currently, I have to add two patterns, using a capture group for each surrounding piece: (#\[case\().*\)\]\n and #\[case\(.*(\)\])\n

It's not a big deal to add the two patterns, but for performance reasons (i.e. repeating the search when it doesn't have to), it would be nice to just have one pattern with the setting.

clutcher commented 8 months ago

@aerontek I'll check that idea.

aerontek commented 8 months ago

@clutcher I just noticed that what you implemented introduced a slight regression. One of my other unrelated regexp happened to have a capture group in it because I was using a quantifier ? after it. But in that particular case, I wanted the entire pattern to match (which is what it did before). After this latest update, that made it so that only the capture group was highlighted, of course.

I fixed the issue on that pattern by simply changing the group from capturing ( ... )? to non-capturing (?: ... )?, so the functionality is still possible as is without much effort, but it may surprise some users that happen to use capture groups when they update. Especially if they have to recreate those patterns with non-capture groups since editing an existing pattern doesn't seem to be possible in the UI at the moment. Just wanted to let you know in case you hadn't already noticed. Thanks again!

clutcher commented 8 months ago

@aerontek Thanks, I didn't noticed it.

In next release I will adjust behavior to highlight all groups if they are defined instead of only last one.

clutcher commented 8 months ago

@aerontek released in latest version 2024.1.2.