diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index be5e5b209a..ec55671682 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -3,7 +3,7 @@ use alloc::{string::String, vec::Vec}; #[cfg(feature = "std")] use std::{fs, io, path::Path}; use syntect::highlighting::{ - FontStyle, HighlightState, Highlighter, RangedHighlightIterator, Theme, ThemeSet, + FontStyle, HighlightState, Highlighter, RangedHighlightIterator, ThemeSet, }; use syntect::parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet}; @@ -12,6 +12,8 @@ use crate::{ Shaping, Style, Weight, Wrap, }; +pub use syntect::highlighting::Theme as SyntaxTheme; + #[derive(Debug)] pub struct SyntaxSystem { pub syntax_set: SyntaxSet, @@ -35,7 +37,7 @@ pub struct SyntaxEditor<'a> { editor: Editor, syntax_system: &'a SyntaxSystem, syntax: &'a SyntaxReference, - theme: &'a Theme, + theme: &'a SyntaxTheme, highlighter: Highlighter<'a>, syntax_cache: Vec<(ParseState, HighlightState)>, } @@ -65,9 +67,11 @@ impl<'a> SyntaxEditor<'a> { /// Modifies the theme of the [`SyntaxEditor`], returning false if the theme is missing pub fn update_theme(&mut self, theme_name: &str) -> bool { if let Some(theme) = self.syntax_system.theme_set.themes.get(theme_name) { - self.theme = theme; - self.highlighter = Highlighter::new(theme); - self.syntax_cache.clear(); + if self.theme != theme { + self.theme = theme; + self.highlighter = Highlighter::new(theme); + self.syntax_cache.clear(); + } true } else { @@ -130,6 +134,11 @@ impl<'a> SyntaxEditor<'a> { Color::rgb(0xFF, 0xFF, 0xFF) } } + + /// Get the current syntect theme + pub fn theme(&self) -> &SyntaxTheme { + self.theme + } } impl<'a> Edit for SyntaxEditor<'a> { diff --git a/src/edit/vi.rs b/src/edit/vi.rs index 05e1e43abe..f3cf7b3ccf 100644 --- a/src/edit/vi.rs +++ b/src/edit/vi.rs @@ -4,7 +4,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::{ Action, AttrsList, BorrowedWithFontSystem, Buffer, Color, Cursor, Edit, FontSystem, - SyntaxEditor, + SyntaxEditor, SyntaxTheme, }; #[derive(Clone, Debug, Eq, PartialEq)] @@ -37,6 +37,11 @@ impl<'a> ViEditor<'a> { } } + /// Modifies the theme of the [`SyntaxEditor`], returning false if the theme is missing + pub fn update_theme(&mut self, theme_name: &str) -> bool { + self.editor.update_theme(theme_name) + } + /// Load text from a file, and also set syntax to the best option #[cfg(feature = "std")] pub fn load_text>( @@ -58,6 +63,11 @@ impl<'a> ViEditor<'a> { self.editor.foreground_color() } + /// Get the current syntect theme + pub fn theme(&self) -> &SyntaxTheme { + self.editor.theme() + } + /// Set passthrough mode (true will turn off vi features) pub fn set_passthrough(&mut self, passthrough: bool) { if passthrough != (self.mode == ViMode::Passthrough) {