-
Notifications
You must be signed in to change notification settings - Fork 144
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
Add helpers for modifying highlighted lines #198
Conversation
Based on use cases from syntect users, modifying the style of some part of the highlighted source, most frequently changing the background colour to draw attention, is a common use case. Implementing it well requires some logic that we can provide a well-tested high quality implementation of.
src/util.rs
Outdated
/// the `Vec<(Style, &str)>` returned by `highlight` methods. Look at the source | ||
/// code for `modify_range` for an example usage. | ||
pub fn split_at<'a, A: Clone>(mut v: &[(A, &'a str)], mut split_i: usize) -> (Vec<(A, &'a str)>, Vec<(A, &'a str)>) { | ||
// Consume all tokens before the split |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was using 2 spaces here intentional? (Reminds me, I wanted to set up rustfmt..)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, Sublime messed up and didn't auto-detect indentation for some reason.
src/util.rs
Outdated
/// Generic for testing purposes and fancier use cases, but intended for use with | ||
/// the `Vec<(Style, &str)>` returned by `highlight` methods. Look at the source | ||
/// code for `modify_range` for an example usage. | ||
pub fn split_at<'a, A: Clone>(mut v: &[(A, &'a str)], mut split_i: usize) -> (Vec<(A, &'a str)>, Vec<(A, &'a str)>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this method read nicer if instead of making the args mut, you'd reassign with clearer names? As in something like:
let mut rest = v;
let mut rest_split_i = split_i;
src/util.rs
Outdated
result.extend(inside.iter().map(|(style, s)| { (style.apply(modifier), *s)})); | ||
result.append(&mut after); | ||
result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really nice to read thanks to split_at
:)!
src/util.rs
Outdated
@@ -1,7 +1,8 @@ | |||
//! Convenient utility methods, mostly for printing `syntect` data structures | |||
//! prettily to the terminal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is a bit of a grab bag of things (my adding of LinesWithEndings
started it).. How about having a separate highlighting::util
for the style related things?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine, it's not that big right now, and I'd prefer to keep down the number of use
lines required for simple use cases, it's already pretty high.
I think what's in here is connected enough by the theme of small utility functions that just use public APIs. I updated the doc comment to better reflect what's actually in here now though, so it's easier to find in the docs.
Fantastic! We have an open feature request in |
@trishume I guess it's not "straightforward" to release a new version (that includes these changes) at the moment since there have been a few breaking changes, right? |
Great, thank you! |
Based on use cases from syntect users, modifying the style of some part of the
highlighted source, most frequently changing the background colour to draw
attention, is a common use case. Implementing it well requires some logic that
we can provide a well-tested high quality implementation of.
See oli-obk/priroda#9 and getzola/zola#127 for examples of use cases for this feature.