Skip to main content

rustik_diff/
highlight.rs

1//! Highlight request and result protocol for diff rendering.
2
3use std::collections::{BTreeMap, hash_map::DefaultHasher};
4use std::hash::{Hash, Hasher};
5use std::path::{Path, PathBuf};
6use std::sync::Arc;
7
8use rustik_highlight as syntax;
9use rustik_tui as tui;
10use rustik_tui::{Color, Label, Line, Style};
11
12use crate::{DiffLine, FileDiff, FileId, Hunk, HunkId, RowId};
13
14/// Side of the diff used as syntax-highlighting source context.
15#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
16pub enum DiffSide {
17    /// Old side of a deleted or context line.
18    Old,
19    /// New side of an added or context line.
20    New,
21}
22
23/// Cache key for one highlighted diff row.
24#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
25pub struct HighlightKey {
26    /// File containing the row.
27    pub file: FileId,
28    /// Diff side to highlight.
29    pub side: DiffSide,
30    /// One-based source line number.
31    pub source_line: u32,
32    /// Hash of the row text.
33    pub text_hash: u64,
34    /// Syntax key or `auto`.
35    pub syntax: String,
36    /// Theme key.
37    pub theme: String,
38}
39
40/// Request sent from a visible diff viewport to a highlighter.
41#[derive(Clone, Debug, Eq, PartialEq)]
42pub struct HighlightRequest {
43    /// Stable cache key for this request.
44    pub key: HighlightKey,
45    /// Hunk containing the row.
46    pub hunk: HunkId,
47    /// Row to highlight.
48    pub row: RowId,
49    /// Source path used for syntax inference.
50    pub path: Option<PathBuf>,
51    /// Source-side text used for range highlighting.
52    pub source: HighlightSource,
53}
54
55/// Highlighted line returned by a worker.
56#[derive(Clone, Debug)]
57pub struct HighlightResult {
58    /// Cache key copied from the original request.
59    pub key: HighlightKey,
60    /// Hunk containing the highlighted row.
61    pub hunk: HunkId,
62    /// Highlighted row.
63    pub row: RowId,
64    /// Renderable highlighted line.
65    pub line: tui::Line,
66}
67
68/// File-side source text used to highlight a diff row with surrounding context.
69#[derive(Clone, Debug, Eq, PartialEq)]
70pub struct HighlightSource {
71    /// Source-side text assembled from the diff.
72    pub text: Arc<str>,
73    /// Source line represented by the head of [`HighlightSource::text`].
74    pub head: u32,
75    /// Byte offset of each source line.
76    line_starts: Arc<[u32]>,
77}
78
79/// Pure built-in highlighter for visible diff rows.
80#[derive(Debug)]
81pub struct BuiltinHighlighter {
82    /// Grammar and theme registry.
83    registry: syntax::Registry,
84    /// Theme used when requests ask for automatic theme selection.
85    default_theme: String,
86    /// Per-source tokenizer caches reused across batches.
87    blob_caches: BTreeMap<BlobCacheKey, BlobCacheEntry>,
88}
89
90/// Identifier for cached blob-highlight state.
91#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
92struct BlobCacheKey {
93    /// Grammar name used to tokenize the source.
94    grammar: String,
95    /// Address of the cached source allocation.
96    text_ptr: usize,
97    /// Length of the cached source allocation.
98    text_len: usize,
99}
100
101/// Cached tokenizer state for one source allocation.
102#[derive(Debug)]
103struct BlobCacheEntry {
104    /// Source owner that keeps the cached allocation pinned.
105    _source: Arc<str>,
106    /// Per-line parser state and scope cache.
107    cache: syntax::BlobCache,
108}
109
110/// Highlights diff rows requested by a [`crate::DiffViewer`].
111pub trait DiffHighlighter {
112    /// Highlights one batch of visible diff rows.
113    fn highlight_batch(&mut self, requests: Vec<HighlightRequest>) -> Vec<HighlightResult>;
114}
115
116/// Grouping key for requests that can share one range highlighter.
117#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
118struct HighlightBatchKey {
119    /// File containing the requested rows.
120    file: FileId,
121    /// Source side containing the requested rows.
122    side: DiffSide,
123    /// Syntax key requested by the viewer.
124    syntax: String,
125    /// Theme key requested by the viewer.
126    theme: String,
127    /// Path used for syntax inference.
128    path: Option<PathBuf>,
129    /// Source allocation identity.
130    source: HighlightSourceId,
131}
132
133/// Allocation identity for source text shared by highlight requests.
134#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
135struct HighlightSourceId {
136    /// Address of the source text allocation.
137    ptr: usize,
138    /// Length of the source text allocation.
139    len: usize,
140    /// Source line represented by the head of the source text.
141    head: u32,
142}
143
144impl HighlightKey {
145    /// Creates a key for a row of source text.
146    pub fn new(input: HighlightKeyInput<'_>) -> Self {
147        let mut hasher = DefaultHasher::new();
148        input.text.hash(&mut hasher);
149
150        Self {
151            file: input.file,
152            side: input.side,
153            source_line: input.source_line,
154            text_hash: hasher.finish(),
155            syntax: input.syntax.unwrap_or("auto").to_owned(),
156            theme: input.theme.to_owned(),
157        }
158    }
159}
160
161impl HighlightSource {
162    /// Creates source text beginning at a source line.
163    pub fn new(text: impl Into<Arc<str>>, head: u32) -> Self {
164        let text = text.into();
165        let mut line_starts = Vec::new();
166        if !text.is_empty() {
167            line_starts.push(0);
168            line_starts.extend(
169                text.bytes()
170                    .enumerate()
171                    .filter(|(index, byte)| *byte == b'\n' && index + 1 < text.len())
172                    .map(|(index, _)| (index + 1) as u32),
173            );
174        }
175        Self {
176            text,
177            head,
178            line_starts: line_starts.into(),
179        }
180    }
181
182    /// Returns the zero-based source-text line index for a source line.
183    pub fn line_index(&self, source_line: u32) -> Option<usize> {
184        source_line.checked_sub(self.head).map(|line| line as usize)
185    }
186
187    /// Returns one source line without its trailing line terminator.
188    pub fn line(&self, source_line: u32) -> Option<&str> {
189        let index = self.line_index(source_line)?;
190        self.indexed_line(index)
191    }
192
193    /// Returns one source line by its zero-based index.
194    fn indexed_line(&self, index: usize) -> Option<&str> {
195        let start = *self.line_starts.get(index)? as usize;
196        let mut end = self
197            .line_starts
198            .get(index + 1)
199            .map_or(self.text.len(), |next| *next as usize);
200        if end > start && self.text.as_bytes()[end - 1] == b'\n' {
201            end -= 1;
202        }
203        if end > start && self.text.as_bytes()[end - 1] == b'\r' {
204            end -= 1;
205        }
206        Some(&self.text[start..end])
207    }
208
209    /// Returns the bytes retained by this source and its line index.
210    pub(crate) fn retained_bytes(&self) -> usize {
211        self.text
212            .len()
213            .saturating_add(self.line_starts.len() * size_of::<u32>())
214    }
215
216    /// Returns whether this source reproduces every row `hunk` shows on `side`.
217    ///
218    /// Highlighted rows are rendered from the source text, not the diff, so a
219    /// source that has drifted from the patch would display lines the patch
220    /// never contained. Worktree files drift whenever they are edited after the
221    /// diff was produced.
222    pub fn matches_hunk(&self, hunk: &Hunk, side: DiffSide) -> bool {
223        let Some(mut line) = self.line_index(hunk_head(hunk, side)) else {
224            return false;
225        };
226
227        hunk.lines
228            .iter()
229            .filter(|line| source_len(line, side) != 0)
230            .all(|expected| {
231                let matches = self.indexed_line(line) == Some(expected.text.as_str());
232                line += 1;
233                matches
234            })
235    }
236
237    /// Returns a compact identity for grouping requests that share this source.
238    fn id(&self) -> HighlightSourceId {
239        HighlightSourceId {
240            ptr: self.text.as_ptr() as usize,
241            len: self.text.len(),
242            head: self.head,
243        }
244    }
245}
246
247impl Hunk {
248    /// Builds contiguous source text for one hunk side.
249    pub fn highlight_source(&self, side: DiffSide) -> HighlightSource {
250        let mut text = String::new();
251        push_hunk_source_lines(self, side, &mut text);
252        HighlightSource::new(text, hunk_head(self, side))
253    }
254}
255
256impl FileDiff {
257    /// Returns the source path for a file side, falling back to the other side.
258    pub fn source_path(&self, side: DiffSide) -> Option<PathBuf> {
259        let (primary, fallback) = match side {
260            DiffSide::Old => (&self.header.old_path, &self.header.new_path),
261            DiffSide::New => (&self.header.new_path, &self.header.old_path),
262        };
263        primary.clone().or_else(|| fallback.clone())
264    }
265
266    /// Builds contiguous source text for one file side.
267    ///
268    /// Returns `None` when the file's hunks are not contiguous on `side`.
269    pub fn highlight_source(&self, side: DiffSide) -> Option<HighlightSource> {
270        let mut text = String::new();
271        let mut head = None;
272        let mut next_line = None;
273
274        for hunk in &self.hunks {
275            let hunk_head = hunk_head(hunk, side);
276            if next_line.is_some_and(|line| line != hunk_head) {
277                return None;
278            }
279            head.get_or_insert(hunk_head);
280            push_hunk_source_lines(hunk, side, &mut text);
281            let len = hunk
282                .lines
283                .iter()
284                .map(|line| source_len(line, side))
285                .sum::<u32>();
286            next_line = Some(hunk_head + len);
287        }
288        head.map(|head| HighlightSource::new(text, head))
289    }
290}
291
292/// Appends source-side hunk lines to `text`.
293///
294/// Blank lines are kept so a [`HighlightSource`] line index stays aligned with
295/// its source line number.
296fn push_hunk_source_lines(hunk: &Hunk, side: DiffSide, text: &mut String) {
297    let mut first = text.is_empty();
298
299    for line in &hunk.lines {
300        if source_len(line, side) == 0 {
301            continue;
302        }
303        if first {
304            first = false;
305        } else {
306            text.push('\n');
307        }
308        text.push_str(&line.text);
309    }
310}
311
312/// Returns the first source line for one hunk side.
313fn hunk_head(hunk: &Hunk, side: DiffSide) -> u32 {
314    match side {
315        DiffSide::Old => hunk.header.old_line_no,
316        DiffSide::New => hunk.header.new_line_no,
317    }
318}
319
320/// Returns how many source lines a diff line contributes on `side`.
321fn source_len(line: &DiffLine, side: DiffSide) -> u32 {
322    match side {
323        DiffSide::Old => line.kind.old_len(),
324        DiffSide::New => line.kind.new_len(),
325    }
326}
327
328impl BuiltinHighlighter {
329    /// Creates a highlighter from caller-loaded grammars and themes.
330    pub fn new(registry: syntax::Registry, default_theme: impl Into<String>) -> Self {
331        Self {
332            registry,
333            default_theme: default_theme.into(),
334            blob_caches: BTreeMap::new(),
335        }
336    }
337
338    /// Adds `grammar` to this highlighter if a grammar with the same name is absent.
339    pub fn ensure_grammar(&mut self, grammar: syntax::Grammar) {
340        if self.registry.grammar_by_name(&grammar.name).is_err() {
341            self.registry.add_grammar(grammar);
342        }
343    }
344
345    /// Returns whether a path selects a loaded non-plain grammar.
346    pub fn has_path_grammar(&self, path: &Path) -> bool {
347        self.registry
348            .query(syntax::GrammarQuery {
349                syntax: None,
350                path: Some(path),
351                head: None,
352            })
353            .is_ok_and(|grammar| grammar.scope_name != "text.plain")
354    }
355
356    /// Highlights one request.
357    pub fn highlight(&mut self, request: HighlightRequest) -> HighlightResult {
358        self.highlight_group(vec![request]).remove(0)
359    }
360
361    /// Highlights multiple requests.
362    pub fn highlight_requests<I>(&mut self, requests: I) -> Vec<HighlightResult>
363    where
364        I: IntoIterator<Item = HighlightRequest>,
365    {
366        let mut batches: BTreeMap<HighlightBatchKey, Vec<HighlightRequest>> = BTreeMap::new();
367        for request in requests {
368            batches
369                .entry(HighlightBatchKey::from(&request))
370                .or_default()
371                .push(request);
372        }
373
374        batches
375            .into_values()
376            .flat_map(|requests| self.highlight_group(requests))
377            .collect()
378    }
379
380    /// Highlights grouped requests with one range highlighter.
381    fn highlight_group(&mut self, requests: Vec<HighlightRequest>) -> Vec<HighlightResult> {
382        let Some(first) = requests.first() else {
383            return Vec::new();
384        };
385        let Ok(grammar) = self.registry.query(syntax::GrammarQuery {
386            syntax: (first.key.syntax != "auto").then_some(first.key.syntax.as_str()),
387            path: first.path.as_deref(),
388            head: first.source.text.lines().next(),
389        }) else {
390            return requests.into_iter().map(HighlightResult::plain).collect();
391        };
392        let theme_name = if first.key.theme == "auto" {
393            self.default_theme.as_str()
394        } else {
395            &first.key.theme
396        };
397        let Ok(theme) = self.registry.theme_by_name(theme_name) else {
398            return requests.into_iter().map(HighlightResult::plain).collect();
399        };
400        let range = requests.iter().filter_map(|request| {
401            request
402                .source
403                .line_index(request.key.source_line)
404                .map(|line| line..line.saturating_add(1))
405        });
406        let mut start = usize::MAX;
407        let mut end = 0;
408        for range in range {
409            start = start.min(range.start);
410            end = end.max(range.end);
411        }
412        if start >= end {
413            return requests.into_iter().map(HighlightResult::plain).collect();
414        }
415        let source = first.source.text.clone();
416        let cache_key = BlobCacheKey {
417            grammar: grammar.name.clone(),
418            text_ptr: source.as_ptr() as usize,
419            text_len: source.len(),
420        };
421        let cache = self
422            .blob_caches
423            .remove(&cache_key)
424            .map(|entry| entry.cache)
425            .unwrap_or_default();
426
427        let mut lines = BTreeMap::new();
428        let cache = {
429            let mut blob = syntax::BlobHighlighter::with_cache(grammar, source.as_ref(), cache);
430            blob.highlight_styled_range(theme, start..end, |line| {
431                lines.insert(line.line_index, line_from_spans(line.text, line.styles));
432            });
433            blob.into_cache()
434        };
435        self.blob_caches.insert(
436            cache_key,
437            BlobCacheEntry {
438                _source: source,
439                cache,
440            },
441        );
442
443        requests
444            .into_iter()
445            .map(|request| {
446                let line = request
447                    .source
448                    .line_index(request.key.source_line)
449                    .and_then(|index| lines.remove(&index))
450                    .unwrap_or_else(|| request.plain_line());
451                HighlightResult::new(request, line)
452            })
453            .collect()
454    }
455}
456
457impl DiffHighlighter for BuiltinHighlighter {
458    fn highlight_batch(&mut self, requests: Vec<HighlightRequest>) -> Vec<HighlightResult> {
459        self.highlight_requests(requests)
460    }
461}
462
463impl HighlightRequest {
464    /// Returns the unhighlighted source row for this request.
465    fn plain_line(&self) -> Line {
466        self.source
467            .line(self.key.source_line)
468            .map(Line::new)
469            .unwrap_or_default()
470    }
471}
472
473impl HighlightResult {
474    /// Pairs a highlighted line with the metadata copied from its request.
475    pub fn new(request: HighlightRequest, line: Line) -> Self {
476        Self {
477            key: request.key,
478            hunk: request.hunk,
479            row: request.row,
480            line,
481        }
482    }
483
484    /// Returns a plain result for an unhighlightable request.
485    fn plain(request: HighlightRequest) -> Self {
486        let line = request.plain_line();
487        Self::new(request, line)
488    }
489}
490
491impl From<&HighlightRequest> for HighlightBatchKey {
492    fn from(request: &HighlightRequest) -> Self {
493        Self {
494            file: request.key.file,
495            side: request.key.side,
496            syntax: request.key.syntax.clone(),
497            theme: request.key.theme.clone(),
498            path: request.path.clone(),
499            source: request.source.id(),
500        }
501    }
502}
503
504/// Converts style spans into a TUI line.
505fn line_from_spans(text: &str, spans: &[syntax::StyleSpan]) -> Line {
506    if text.is_empty() || spans.is_empty() {
507        return Line::new(text);
508    }
509    let mut line = Line::blank();
510    let mut cursor = 0;
511
512    for span in spans {
513        if cursor < span.start {
514            line.push(Label::new(&text[cursor..span.start]));
515        }
516        if span.start < span.end {
517            line.push(Label::new(&text[span.start..span.end]).style(span.style.tui_style()));
518        }
519        cursor = span.end;
520    }
521    if cursor < text.len() {
522        line.push(Label::new(&text[cursor..]));
523    }
524    line
525}
526
527/// Input fields used to create a [`HighlightKey`].
528#[derive(Clone, Copy, Debug)]
529pub struct HighlightKeyInput<'a> {
530    /// File containing the row.
531    pub file: FileId,
532    /// Diff side to highlight.
533    pub side: DiffSide,
534    /// One-based source line number.
535    pub source_line: u32,
536    /// Source text.
537    pub text: &'a str,
538    /// Optional syntax override.
539    pub syntax: Option<&'a str>,
540    /// Theme name.
541    pub theme: &'a str,
542}
543
544/// Converts syntax styles into TUI styles.
545trait SyntaxStyleExt {
546    /// Converts this syntax style into a TUI style.
547    fn tui_style(self) -> Style;
548}
549
550impl SyntaxStyleExt for syntax::Style {
551    fn tui_style(self) -> Style {
552        let mut out = Style::default();
553        if let Some(color) = self.foreground {
554            out = out.fg(Color::RGB(color.r, color.g, color.b));
555        }
556        if self.font_style.contains(syntax::FontStyle::BOLD) {
557            out = out.bold();
558        }
559        if self.font_style.contains(syntax::FontStyle::ITALIC) {
560            out = out.italic();
561        }
562        if self.font_style.contains(syntax::FontStyle::UNDERLINE) {
563            out = out.underline();
564        }
565        if self.font_style.contains(syntax::FontStyle::STRIKETHROUGH) {
566            out = out.strikethrough();
567        }
568        out
569    }
570}
571
572#[cfg(test)]
573mod tests {
574    use super::*;
575    use crate::Decode;
576    use std::str::FromStr;
577
578    #[test]
579    fn source_lines_before_head_are_absent() {
580        let source = HighlightSource::new("two\nthree\n", 2);
581
582        assert_eq!(source.line_index(1), None);
583        assert_eq!(source.line(1), None);
584        assert_eq!(source.line_index(2), Some(0));
585        assert_eq!(source.line(2), Some("two"));
586    }
587
588    #[test]
589    fn indexed_source_lines_match_rust_line_boundaries() {
590        let source = HighlightSource::new("one\r\ntwo\n\nthree", 1);
591
592        assert_eq!(source.line(1), Some("one"));
593        assert_eq!(source.line(2), Some("two"));
594        assert_eq!(source.line(3), Some(""));
595        assert_eq!(source.line(4), Some("three"));
596        assert_eq!(source.line(5), None);
597    }
598
599    #[test]
600    fn file_highlight_source_spans_the_new_side() {
601        let document = crate::DiffDocument::decode_str(
602            "\
603diff --git a/a.css b/a.css
604index 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100644
605--- a/a.css
606+++ b/a.css
607@@ -1,3 +1,3 @@
608 .x {
609-background: url(old.svg);
610+background: url(new.svg);
611 }
612",
613        )
614        .unwrap();
615        let source = document.files[0].highlight_source(DiffSide::New).unwrap();
616
617        assert_eq!(source.head, 1);
618        assert_eq!(source.line(1), Some(".x {"));
619        assert_eq!(source.line(2), Some("background: url(new.svg);"));
620        assert_eq!(source.line(3), Some("}"));
621    }
622
623    #[test]
624    fn highlight_source_keeps_a_leading_blank_line() {
625        // A hunk whose first source line is blank must not collapse it away,
626        // or every later line index shifts up by one against its line number.
627        // The blank context line is a single space; spell the fixture with
628        // `concat!` so that space cannot be mistaken for trailing whitespace.
629        let document = crate::DiffDocument::decode_str(concat!(
630            "diff --git a/a.rs b/a.rs\n",
631            "index 1111111111111111111111111111111111111111..2222222222222222222222222222222222222222 100644\n",
632            "--- a/a.rs\n",
633            "+++ b/a.rs\n",
634            "@@ -2,3 +2,3 @@\n",
635            " \n",
636            "-old\n",
637            "+new\n",
638            " tail\n",
639        ))
640        .unwrap();
641        let hunk = &document.files[0].hunks[0];
642
643        let source = hunk.highlight_source(DiffSide::New);
644        assert_eq!(source.head, 2);
645        assert_eq!(source.line(2), Some(""));
646        assert_eq!(source.line(3), Some("new"));
647        assert_eq!(source.line(4), Some("tail"));
648
649        let file = document.files[0].highlight_source(DiffSide::New).unwrap();
650        assert_eq!(file.line(2), Some(""));
651        assert_eq!(file.line(3), Some("new"));
652    }
653
654    #[cfg(feature = "builtins")]
655    #[test]
656    fn css_custom_properties_use_variable_theme_style() {
657        let grammar = syntax::Grammar::from_str(syntax::syntaxes::CSS).unwrap();
658        let theme =
659            syntax::Theme::parse(include_str!("themes/rustik-catppuccin-mocha.json")).unwrap();
660        let line = "  --color-background-light: #dad8d8;";
661        let mut state = syntax::LineState::default();
662        let mut tokenizer = syntax::LineTokenizer::new(&grammar);
663        let _ = tokenizer.tokenize_line(&mut state, ":root {");
664        let spans = tokenizer.tokenize_line(&mut state, line);
665        let styles = theme.style_spans(&grammar, line, &spans);
666        let custom_property = line.find("--color-background-light").unwrap();
667        let style = styles
668            .iter()
669            .find(|style| style.start <= custom_property && style.end > custom_property)
670            .unwrap();
671
672        assert!(scopes_for_byte(&grammar, &spans, custom_property).contains(&"variable.css"));
673        assert_eq!(
674            style.style.foreground,
675            Some(syntax::Rgb {
676                r: 137,
677                g: 180,
678                b: 250
679            })
680        );
681    }
682
683    #[cfg(feature = "builtins")]
684    #[test]
685    fn css_var_references_use_variable_theme_style() {
686        let grammar = syntax::Grammar::from_str(syntax::syntaxes::CSS).unwrap();
687        let theme =
688            syntax::Theme::parse(include_str!("themes/rustik-catppuccin-mocha.json")).unwrap();
689        let line = "  color: var(--color-dimmer);";
690        let mut state = syntax::LineState::default();
691        let mut tokenizer = syntax::LineTokenizer::new(&grammar);
692        let _ = tokenizer.tokenize_line(&mut state, ".note-meta {");
693        let spans = tokenizer.tokenize_line(&mut state, line);
694        let styles = theme.style_spans(&grammar, line, &spans);
695        let variable = line.find("--color-dimmer").unwrap();
696        let style = styles
697            .iter()
698            .find(|style| style.start <= variable && style.end > variable)
699            .unwrap();
700
701        assert!(scopes_for_byte(&grammar, &spans, variable).contains(&"variable.argument.css"));
702        assert_eq!(
703            style.style.foreground,
704            Some(syntax::Rgb {
705                r: 235,
706                g: 160,
707                b: 172
708            })
709        );
710    }
711
712    #[cfg(feature = "builtins")]
713    #[test]
714    fn rust_signature_parameters_use_normal_theme_style() {
715        let grammar = syntax::Grammar::from_str(syntax::syntaxes::RUST).unwrap();
716        let theme =
717            syntax::Theme::parse(include_str!("themes/rustik-catppuccin-mocha.json")).unwrap();
718        let line = "    mut cursor: String,";
719        let mut state = syntax::LineState::default();
720        let mut tokenizer = syntax::LineTokenizer::new(&grammar);
721        let _ = tokenizer.tokenize_line(&mut state, "fn reconcile_with<C, E>(");
722        let spans = tokenizer.tokenize_line(&mut state, line);
723        let styles = theme.style_spans(&grammar, line, &spans);
724        let parameter = line.find("cursor").unwrap();
725        let style = styles
726            .iter()
727            .find(|style| style.start <= parameter && style.end > parameter)
728            .unwrap();
729
730        assert!(scopes_for_byte(&grammar, &spans, parameter).contains(&"variable.parameter.rust"));
731        assert_eq!(
732            style.style.foreground,
733            Some(syntax::Rgb {
734                r: 205,
735                g: 214,
736                b: 244
737            })
738        );
739    }
740
741    fn scopes_for_byte<'a>(
742        grammar: &'a syntax::Grammar,
743        spans: &'a [syntax::ScopeSpan],
744        byte: usize,
745    ) -> Vec<&'a str> {
746        spans
747            .iter()
748            .filter(|span| span.start <= byte && span.end > byte)
749            .filter_map(|span| grammar.scopes.get(span.scope.index()))
750            .map(String::as_str)
751            .collect()
752    }
753}