Skip to content

Commit

Permalink
HighlightingAssets: Encapsulate theme_set behind a getter
Browse files Browse the repository at this point in the history
Mainly to prepare for potential lazy-loading in the future to
improve startup performance. Like we did for syntax_set.
  • Loading branch information
Enselic committed Jul 29, 2021
1 parent 6acec2c commit add5c99
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
pub struct HighlightingAssets {
syntax_set_cell: LazyCell<SyntaxSet>,
serialized_syntax_set: Option<SerializedSyntaxSet>,
pub(crate) theme_set: ThemeSet,
theme_set: ThemeSet,
fallback_theme: Option<&'static str>,
}

Expand Down Expand Up @@ -140,7 +140,11 @@ impl HighlightingAssets {

pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
let _ = fs::create_dir_all(target_dir);
asset_to_cache(&self.theme_set, &target_dir.join("themes.bin"), "theme set")?;
asset_to_cache(
self.get_theme_set(),
&target_dir.join("themes.bin"),
"theme set",
)?;
asset_to_cache(
self.get_syntax_set()?,
&target_dir.join("syntaxes.bin"),
Expand Down Expand Up @@ -187,8 +191,12 @@ impl HighlightingAssets {
Ok(self.get_syntax_set()?.syntaxes())
}

fn get_theme_set(&self) -> &ThemeSet {
&self.theme_set
}

pub fn themes(&self) -> impl Iterator<Item = &str> {
self.theme_set.themes.keys().map(|s| s.as_ref())
self.get_theme_set().themes.keys().map(|s| s.as_ref())
}

/// Use [Self::get_syntax_for_file_name] instead
Expand Down Expand Up @@ -219,7 +227,7 @@ impl HighlightingAssets {
}

pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
match self.theme_set.themes.get(theme) {
match self.get_theme_set().themes.get(theme) {
Some(theme) => theme,
None => {
if theme == "ansi-light" || theme == "ansi-dark" {
Expand All @@ -229,7 +237,8 @@ impl HighlightingAssets {
if !theme.is_empty() {
bat_warning!("Unknown theme '{}', using default.", theme)
}
&self.theme_set.themes[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
&self.get_theme_set().themes
[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
}
}
}
Expand Down

0 comments on commit add5c99

Please sign in to comment.