|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +// all syntect imports are explicitly qualified, but their paths are shortened for convenience |
| 4 | +mod syntect { |
| 5 | + pub(super) use syntect::{ |
| 6 | + highlighting::{ |
| 7 | + Color, HighlightIterator, HighlightState, Highlighter, Style, Theme, ThemeSet, |
| 8 | + }, |
| 9 | + parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet}, |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +use owo_colors::{Rgb, Style, Styled}; |
| 14 | + |
| 15 | +use crate::{ |
| 16 | + highlighters::{Highlighter, HighlighterState}, |
| 17 | + SourceCode, |
| 18 | +}; |
| 19 | + |
| 20 | +/// Highlights miette [SourceCode] with the [syntect] highlighting crate. |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct SyntectHighlighter { |
| 23 | + theme: syntect::Theme, |
| 24 | + syntax_set: syntect::SyntaxSet, |
| 25 | +} |
| 26 | + |
| 27 | +impl Default for SyntectHighlighter { |
| 28 | + fn default() -> Self { |
| 29 | + let syntax_set = syntect::SyntaxSet::load_defaults_nonewlines(); |
| 30 | + let theme_set = syntect::ThemeSet::load_defaults(); |
| 31 | + let theme = theme_set.themes.get("base16-ocean.dark").unwrap().clone(); |
| 32 | + Self::new(theme, syntax_set) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl Highlighter for SyntectHighlighter { |
| 37 | + fn start_highlighter_state<'h>( |
| 38 | + &'h self, |
| 39 | + source: &dyn SourceCode, |
| 40 | + ) -> Option<Box<dyn HighlighterState + 'h>> { |
| 41 | + let syntax = self.get_syntax_from_source(source)?; |
| 42 | + let highlighter = syntect::Highlighter::new(&self.theme); |
| 43 | + let parse_state = syntect::ParseState::new(syntax); |
| 44 | + let highlight_state = |
| 45 | + syntect::HighlightState::new(&highlighter, syntect::ScopeStack::new()); |
| 46 | + Some(Box::new(SyntectHighlighterState { |
| 47 | + syntax_set: &self.syntax_set, |
| 48 | + highlighter, |
| 49 | + parse_state, |
| 50 | + highlight_state, |
| 51 | + })) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl SyntectHighlighter { |
| 56 | + /// Create a SyntectHighlighter |
| 57 | + pub fn new(theme: syntect::Theme, syntax_set: syntect::SyntaxSet) -> Self { |
| 58 | + Self { theme, syntax_set } |
| 59 | + } |
| 60 | + |
| 61 | + /// Determine syntect SyntaxReference to use for given SourceCode |
| 62 | + fn get_syntax_from_source(&self, source: &dyn SourceCode) -> Option<&syntect::SyntaxReference> { |
| 63 | + if let Some(language) = source.language() { |
| 64 | + self.syntax_set.find_syntax_by_name(language) |
| 65 | + } else if let Some(name) = source.name() { |
| 66 | + if let Some(ext) = Path::new(name).extension() { |
| 67 | + self.syntax_set |
| 68 | + .find_syntax_by_extension(ext.to_string_lossy().as_ref()) |
| 69 | + } else { |
| 70 | + None |
| 71 | + } |
| 72 | + } else { |
| 73 | + None |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Stateful highlighting iterator for [SyntectHighlighter] |
| 79 | +#[derive(Debug)] |
| 80 | +pub struct SyntectHighlighterState<'h> { |
| 81 | + syntax_set: &'h syntect::SyntaxSet, |
| 82 | + highlighter: syntect::Highlighter<'h>, |
| 83 | + parse_state: syntect::ParseState, |
| 84 | + highlight_state: syntect::HighlightState, |
| 85 | +} |
| 86 | + |
| 87 | +impl<'h> HighlighterState for SyntectHighlighterState<'h> { |
| 88 | + fn highlight_line<'s>(&mut self, line: &'s str) -> Vec<Styled<&'s str>> { |
| 89 | + let use_bg_color = false; |
| 90 | + let ops = self.parse_state.parse_line(line, &self.syntax_set).unwrap(); |
| 91 | + syntect::HighlightIterator::new( |
| 92 | + &mut self.highlight_state, |
| 93 | + &ops, |
| 94 | + line, |
| 95 | + &mut self.highlighter, |
| 96 | + ) |
| 97 | + .map(|(style, str)| (convert_style(style, use_bg_color).style(str))) |
| 98 | + .collect() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/* Convert syntect Style into owo_colors Style */ |
| 103 | +#[inline] |
| 104 | +fn convert_style(syntect_style: syntect::Style, use_bg_color: bool) -> Style { |
| 105 | + if use_bg_color { |
| 106 | + let fg = blend_fg_color(syntect_style); |
| 107 | + let bg = convert_color(syntect_style.background); |
| 108 | + Style::new().color(fg).on_color(bg) |
| 109 | + } else { |
| 110 | + let fg = convert_color(syntect_style.foreground); |
| 111 | + Style::new().color(fg) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/* Blend foreground RGB into background RGB according to alpha channel */ |
| 116 | +#[inline] |
| 117 | +fn blend_fg_color(syntect_style: syntect::Style) -> Rgb { |
| 118 | + let fg = syntect_style.foreground; |
| 119 | + if fg.a == 0xff { |
| 120 | + return convert_color(fg); |
| 121 | + } |
| 122 | + let bg = syntect_style.background; |
| 123 | + let ratio = fg.a as u32; |
| 124 | + let r = (fg.r as u32 * ratio + bg.r as u32 * (255 - ratio)) / 255; |
| 125 | + let g = (fg.g as u32 * ratio + bg.g as u32 * (255 - ratio)) / 255; |
| 126 | + let b = (fg.b as u32 * ratio + bg.b as u32 * (255 - ratio)) / 255; |
| 127 | + Rgb(r as u8, g as u8, b as u8) |
| 128 | +} |
| 129 | + |
| 130 | +/** Convert syntect color into owo color. |
| 131 | + * |
| 132 | + * Note: ignores alpha channel. use blend_fg_color if you need that |
| 133 | + */ |
| 134 | +#[inline] |
| 135 | +fn convert_color(color: syntect::Color) -> Rgb { |
| 136 | + Rgb(color.r, color.g, color.b) |
| 137 | +} |
0 commit comments