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

Add helpers for modifying highlighted lines #198

Merged
merged 4 commits into from
Aug 22, 2018
Merged

Conversation

trishume
Copy link
Owner

@trishume trishume commented Aug 7, 2018

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.

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.
@trishume trishume requested a review from robinst August 7, 2018 18:25
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
Copy link
Collaborator

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..)

Copy link
Owner Author

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)>) {
Copy link
Collaborator

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
}
Copy link
Collaborator

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.
Copy link
Collaborator

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?

Copy link
Owner Author

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.

@sharkdp
Copy link
Contributor

sharkdp commented Aug 18, 2018

Fantastic! We have an open feature request in bat which would also benefit from these helper functions.

@trishume trishume merged commit 5a51df2 into master Aug 22, 2018
@trishume trishume deleted the highlighting-helpers branch August 22, 2018 14:46
@sharkdp
Copy link
Contributor

sharkdp commented Aug 31, 2018

@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?

@trishume
Copy link
Owner Author

@sharkdp yah unfortunately 😞

The main thing I want to do before release is #196 because that's another breaking change. Then just changelogs and stuff. I'll try and find the time to work on that soon.

@sharkdp
Copy link
Contributor

sharkdp commented Aug 31, 2018

Great, thank you!

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.

4 participants