Skip to content

Commit

Permalink
SyntaxEditor: Allow retrieving syntax theme, optimize updates to theme
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Nov 2, 2023
1 parent 241c4ca commit ac389d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/edit/syntect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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,
Expand All @@ -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)>,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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> {
Expand Down
12 changes: 11 additions & 1 deletion src/edit/vi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<P: AsRef<std::path::Path>>(
Expand All @@ -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) {
Expand Down

0 comments on commit ac389d9

Please sign in to comment.