1use 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#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
16pub enum DiffSide {
17 Old,
19 New,
21}
22
23#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
25pub struct HighlightKey {
26 pub file: FileId,
28 pub side: DiffSide,
30 pub source_line: u32,
32 pub text_hash: u64,
34 pub syntax: String,
36 pub theme: String,
38}
39
40#[derive(Clone, Debug, Eq, PartialEq)]
42pub struct HighlightRequest {
43 pub key: HighlightKey,
45 pub hunk: HunkId,
47 pub row: RowId,
49 pub path: Option<PathBuf>,
51 pub source: HighlightSource,
53}
54
55#[derive(Clone, Debug)]
57pub struct HighlightResult {
58 pub key: HighlightKey,
60 pub hunk: HunkId,
62 pub row: RowId,
64 pub line: tui::Line,
66}
67
68#[derive(Clone, Debug, Eq, PartialEq)]
70pub struct HighlightSource {
71 pub text: Arc<str>,
73 pub head: u32,
75 line_starts: Arc<[u32]>,
77}
78
79#[derive(Debug)]
81pub struct BuiltinHighlighter {
82 registry: syntax::Registry,
84 default_theme: String,
86 blob_caches: BTreeMap<BlobCacheKey, BlobCacheEntry>,
88}
89
90#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
92struct BlobCacheKey {
93 grammar: String,
95 text_ptr: usize,
97 text_len: usize,
99}
100
101#[derive(Debug)]
103struct BlobCacheEntry {
104 _source: Arc<str>,
106 cache: syntax::BlobCache,
108}
109
110pub trait DiffHighlighter {
112 fn highlight_batch(&mut self, requests: Vec<HighlightRequest>) -> Vec<HighlightResult>;
114}
115
116#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
118struct HighlightBatchKey {
119 file: FileId,
121 side: DiffSide,
123 syntax: String,
125 theme: String,
127 path: Option<PathBuf>,
129 source: HighlightSourceId,
131}
132
133#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
135struct HighlightSourceId {
136 ptr: usize,
138 len: usize,
140 head: u32,
142}
143
144impl HighlightKey {
145 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 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 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 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 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 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 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 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 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 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 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
292fn 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
312fn 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
320fn 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 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 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 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 pub fn highlight(&mut self, request: HighlightRequest) -> HighlightResult {
358 self.highlight_group(vec![request]).remove(0)
359 }
360
361 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 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 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 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 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
504fn 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#[derive(Clone, Copy, Debug)]
529pub struct HighlightKeyInput<'a> {
530 pub file: FileId,
532 pub side: DiffSide,
534 pub source_line: u32,
536 pub text: &'a str,
538 pub syntax: Option<&'a str>,
540 pub theme: &'a str,
542}
543
544trait SyntaxStyleExt {
546 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 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}