use iced::Color;
use std::rc::Rc;
use std::sync::OnceLock;
use syntect::easy::HighlightLines;
use syntect::highlighting::{
HighlightIterator, HighlightState, Highlighter, Style, ThemeSet,
};
use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet};
use crate::canvas_editor::{CodeEditor, HighlightCache, ResolvedSyntax};
static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();
static THEME_SET: OnceLock<ThemeSet> = OnceLock::new();
fn color_from_style(style: Style) -> Color {
Color::from_rgb(
f32::from(style.foreground.r) / 255.0,
f32::from(style.foreground.g) / 255.0,
f32::from(style.foreground.b) / 255.0,
)
}
pub fn highlight_line_spans(
line: &str,
syntax: &syntect::parsing::SyntaxReference,
theme: &syntect::highlighting::Theme,
syntax_set: &SyntaxSet,
) -> Vec<(Color, String)> {
let mut highlighter = HighlightLines::new(syntax, theme);
let ranges = highlighter
.highlight_line(line, syntax_set)
.unwrap_or_else(|_| vec![(Style::default(), line)]);
ranges
.into_iter()
.map(|(style, text)| (color_from_style(style), text.to_string()))
.collect()
}
const BRACKET_PAIR_COLORS_DARK: [Color; 3] = [
Color { r: 1.0, g: 0.843, b: 0.0, a: 1.0 }, Color { r: 0.855, g: 0.439, b: 0.839, a: 1.0 }, Color { r: 0.529, g: 0.808, b: 0.980, a: 1.0 }, ];
const BRACKET_PAIR_COLORS_LIGHT: [Color; 3] = [
Color { r: 0.016, g: 0.192, b: 0.980, a: 1.0 }, Color { r: 0.192, g: 0.576, b: 0.192, a: 1.0 }, Color { r: 0.482, g: 0.220, b: 0.078, a: 1.0 }, ];
impl CodeEditor {
pub(super) fn bracket_pair_colors(&self) -> &'static [Color; 3] {
if crate::theme::is_dark_background(self.style.background) {
&BRACKET_PAIR_COLORS_DARK
} else {
&BRACKET_PAIR_COLORS_LIGHT
}
}
pub(crate) fn resolve_syntax(
&self,
) -> (
&'static SyntaxSet,
Option<&'static SyntaxReference>,
Option<&'static syntect::highlighting::Theme>,
) {
let syntax_set = SYNTAX_SET.get_or_init(|| {
#[cfg(feature = "two-face")]
{
two_face::syntax::extra_newlines()
}
#[cfg(not(feature = "two-face"))]
{
SyntaxSet::load_defaults_newlines()
}
});
let dark_background =
crate::theme::is_dark_background(self.style.background);
let mut memo = self.resolved_syntax.borrow_mut();
if let Some((syntax, theme)) = memo
.as_ref()
.and_then(|resolved| resolved.get(&self.syntax, dark_background))
{
return (syntax_set, syntax, theme);
}
let theme_set = THEME_SET.get_or_init(ThemeSet::load_defaults);
let theme_name = if dark_background {
"base16-ocean.dark"
} else {
"base16-ocean.light"
};
let theme = theme_set
.themes
.get(theme_name)
.or_else(|| theme_set.themes.values().next());
let syntax = match self.syntax.as_str() {
"python" => syntax_set.find_syntax_by_extension("py"),
"rust" => syntax_set.find_syntax_by_extension("rs"),
"javascript" => syntax_set.find_syntax_by_extension("js"),
"htm" => syntax_set.find_syntax_by_extension("html"),
"svg" => syntax_set.find_syntax_by_extension("xml"),
"markdown" => syntax_set.find_syntax_by_extension("md"),
"text" => Some(syntax_set.find_syntax_plain_text()),
_ => syntax_set.find_syntax_by_extension(self.syntax.as_str()),
}
.or(Some(syntax_set.find_syntax_plain_text()));
*memo = Some(ResolvedSyntax::new(
&self.syntax,
dark_background,
syntax,
theme,
));
(syntax_set, syntax, theme)
}
pub(crate) fn highlighted_line_cached(
&self,
logical_line: usize,
syntax: &syntect::parsing::SyntaxReference,
theme: &syntect::highlighting::Theme,
syntax_set: &SyntaxSet,
) -> Rc<Vec<(Color, String)>> {
let mut guard = self.highlight_cache.borrow_mut();
let needs_reset =
guard.as_ref().is_none_or(|cache| cache.syntax() != self.syntax);
if needs_reset {
*guard = Some(HighlightCache::new(self.syntax.clone()));
}
let Some(cache) = guard.as_mut() else {
return Rc::new(highlight_line_spans(
self.buffer.line(logical_line),
syntax,
theme,
syntax_set,
));
};
if let Some(spans) = cache.spans(logical_line) {
return spans;
}
let highlighter = Highlighter::new(theme);
let (mut parse_state, mut highlight_state) =
cache.resume_state().unwrap_or_else(|| {
(
ParseState::new(syntax),
HighlightState::new(&highlighter, ScopeStack::new()),
)
});
let line_count = self.buffer.line_count();
let target = logical_line.min(line_count.saturating_sub(1));
let missing_lines =
target.saturating_add(1).saturating_sub(cache.valid_len());
let lines_to_parse =
missing_lines.min(self.highlight_lines_remaining.get());
let parse_end = cache
.valid_len()
.saturating_add(lines_to_parse)
.saturating_sub(1)
.min(target);
let mut result = None;
if lines_to_parse > 0 {
for index in cache.valid_len()..=parse_end {
let mut line = self.buffer.line(index).to_string();
line.push('\n');
let ops = parse_state
.parse_line(&line, syntax_set)
.unwrap_or_default();
let spans: Vec<(Color, String)> = HighlightIterator::new(
&mut highlight_state,
&ops,
&line,
&highlighter,
)
.filter_map(|(style, text)| {
let text = text.strip_suffix('\n').unwrap_or(text);
if text.is_empty() {
None
} else {
Some((color_from_style(style), text.to_string()))
}
})
.collect();
let spans = Rc::new(spans);
cache.push_line(
Rc::clone(&spans),
parse_state.clone(),
highlight_state.clone(),
);
if index == logical_line {
result = Some(spans);
}
}
}
self.highlight_lines_remaining.set(
self.highlight_lines_remaining.get().saturating_sub(lines_to_parse),
);
result.or_else(|| cache.spans(logical_line)).unwrap_or_else(|| {
Rc::new(vec![(
self.style.text_color,
self.buffer.line(logical_line).to_string(),
)])
})
}
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use syntect::parsing::SyntaxSet;
use super::*;
#[test]
fn test_bracket_pair_colors_follow_the_editor_theme() {
let mut editor = CodeEditor::new("(a)", "rs");
editor.set_theme(crate::theme::from_iced_theme(&iced::Theme::Dark));
let dark = editor.bracket_pair_colors();
editor.set_theme(crate::theme::from_iced_theme(&iced::Theme::Light));
let light = editor.bracket_pair_colors();
assert_ne!(
dark[0], light[0],
"a light theme must not reuse the dark rainbow palette"
);
}
#[test]
fn test_bracket_pair_colors_contrast_with_their_background() {
for color in BRACKET_PAIR_COLORS_DARK {
assert!(
!crate::theme::is_dark_background(color),
"{color:?} is too dark for a dark background"
);
}
for color in BRACKET_PAIR_COLORS_LIGHT {
assert!(
crate::theme::is_dark_background(color),
"{color:?} is too light for a light background"
);
}
}
#[test]
fn test_highlight_line_spans_covers_full_line() {
let syntax_set = SyntaxSet::load_defaults_newlines();
let syntax = syntax_set.find_syntax_plain_text();
let theme = syntect::highlighting::Theme::default();
let line = "fn main() {}";
let spans = highlight_line_spans(line, syntax, &theme, &syntax_set);
assert!(!spans.is_empty(), "expected at least one span");
let combined: String =
spans.iter().map(|(_, text)| text.as_str()).collect();
assert_eq!(combined, line, "spans must cover the entire line");
}
#[test]
fn test_highlighted_line_cached_reuses_until_invalidated() {
let editor = CodeEditor::new("fn main() {}\nlet x = 1;", "rs");
let syntax_set = SyntaxSet::load_defaults_newlines();
let syntax = syntax_set.find_syntax_plain_text();
let theme = syntect::highlighting::Theme::default();
let first =
editor.highlighted_line_cached(0, syntax, &theme, &syntax_set);
let second =
editor.highlighted_line_cached(0, syntax, &theme, &syntax_set);
assert!(
Rc::ptr_eq(&first, &second),
"a cached line should be reused as the same Rc"
);
editor.invalidate_highlight_from(0);
let third =
editor.highlighted_line_cached(0, syntax, &theme, &syntax_set);
assert!(
!Rc::ptr_eq(&first, &third),
"invalidation should force the line to be recomputed"
);
}
#[test]
fn test_highlight_budget_uses_plain_fallback_without_scanning_to_target() {
let editor = CodeEditor::new("zero\none\ntwo\nthree\nfour", "txt");
let syntax_set = SyntaxSet::load_defaults_newlines();
let syntax = syntax_set.find_syntax_plain_text();
let theme = syntect::highlighting::Theme::default();
editor.highlight_lines_remaining.set(2);
let spans =
editor.highlighted_line_cached(4, syntax, &theme, &syntax_set);
let combined: String =
spans.iter().map(|(_, text)| text.as_str()).collect();
assert_eq!(combined, "four");
assert_eq!(
editor
.highlight_cache
.borrow()
.as_ref()
.map(super::HighlightCache::valid_len),
Some(2)
);
assert_eq!(editor.highlight_lines_remaining.get(), 0);
}
#[test]
fn test_highlighted_line_cached_handles_multiline_comments() {
let syntax_set = SyntaxSet::load_defaults_newlines();
let syntax = syntax_set
.find_syntax_by_extension("rs")
.unwrap_or_else(|| syntax_set.find_syntax_plain_text());
let theme = ThemeSet::load_defaults()
.themes
.get("base16-ocean.dark")
.cloned()
.unwrap_or_default();
let code = "let a = 1;\n/* open\nstill inside\n*/\nlet b = 2;";
let editor = CodeEditor::new(code, "rs");
let sequential =
editor.highlighted_line_cached(2, syntax, &theme, &syntax_set);
let independent = highlight_line_spans(
editor.buffer.line(2),
syntax,
&theme,
&syntax_set,
);
let sequential_color = sequential.first().map(|(color, _)| *color);
let independent_color = independent.first().map(|(color, _)| *color);
assert!(sequential_color.is_some());
assert!(independent_color.is_some());
assert_ne!(
sequential_color, independent_color,
"a line inside a block comment must be colored as a comment"
);
}
}