aboutsummaryrefslogtreecommitdiff
path: root/helix-term
diff options
context:
space:
mode:
authorGokul Soumya2022-03-02 17:20:14 +0000
committerBlaž Hrastnik2022-03-08 04:59:38 +0000
commitbde0307c8795d521dce4be849f38bc1808f1b9a2 (patch)
treeb4db31163a0b4cae2b74d0944efda7c3c6db96c7 /helix-term
parent970a111aa3689e6cc46214859d71fdb0c87f7e14 (diff)
Allow highlighting additional spans in md renderer
Diffstat (limited to 'helix-term')
-rw-r--r--helix-term/src/ui/markdown.rs24
1 files changed, 17 insertions, 7 deletions
diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs
index 41021fde..5f78c3cc 100644
--- a/helix-term/src/ui/markdown.rs
+++ b/helix-term/src/ui/markdown.rs
@@ -31,6 +31,7 @@ pub fn highlighted_code_block<'a>(
language: &str,
theme: Option<&Theme>,
config_loader: Arc<syntax::Loader>,
+ additional_highlight_spans: Option<Vec<(usize, std::ops::Range<usize>)>>,
) -> Text<'a> {
let mut spans = Vec::new();
let mut lines = Vec::new();
@@ -55,10 +56,19 @@ pub fn highlighted_code_block<'a>(
None => return styled_multiline_text(text, code_style),
};
- let mut highlights = Vec::new();
+ let highlight_iter = syntax
+ .highlight_iter(rope.slice(..), None, None)
+ .map(|e| e.unwrap());
+ let highlight_iter: Box<dyn Iterator<Item = HighlightEvent>> =
+ if let Some(spans) = additional_highlight_spans {
+ Box::new(helix_core::syntax::merge(highlight_iter, spans))
+ } else {
+ Box::new(highlight_iter)
+ };
- for event in syntax.highlight_iter(rope.slice(..), None, None) {
- match event.unwrap() {
+ let mut highlights = Vec::new();
+ for event in highlight_iter {
+ match event {
HighlightEvent::HighlightStart(span) => {
highlights.push(span);
}
@@ -66,10 +76,9 @@ pub fn highlighted_code_block<'a>(
highlights.pop();
}
HighlightEvent::Source { start, end } => {
- let style = match highlights.first() {
- Some(span) => theme.get(&theme.scopes()[span.0]),
- None => text_style,
- };
+ let style = highlights
+ .iter()
+ .fold(text_style, |acc, span| acc.patch(theme.highlight(span.0)));
let mut slice = &text[start..end];
// TODO: do we need to handle all unicode line endings
@@ -195,6 +204,7 @@ impl Markdown {
language,
theme,
Arc::clone(&self.config_loader),
+ None,
);
lines.extend(tui_text.lines.into_iter());
} else {