use std::{ collections::HashMap, path::{Path, PathBuf}, }; use anyhow::{anyhow, Context, Result}; use helix_core::hashmap; use helix_loader::merge_toml_values; use log::warn; use once_cell::sync::Lazy; use serde::{Deserialize, Deserializer}; use toml::{map::Map, Value}; use crate::graphics::UnderlineStyle; pub use crate::graphics::{Color, Modifier, Style}; pub static DEFAULT_THEME: Lazy = Lazy::new(|| Theme { name: "default".into(), ..toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme") }); pub static BASE16_DEFAULT_THEME: Lazy = Lazy::new(|| Theme { name: "base16_theme".into(), ..toml::from_slice(include_bytes!("../../base16_theme.toml")) .expect("Failed to parse base 16 default theme") }); #[derive(Clone, Debug)] pub struct Loader { user_dir: PathBuf, default_dir: PathBuf, } impl Loader { /// Creates a new loader that can load themes from two directories. pub fn new>(user_dir: P, default_dir: P) -> Self { Self { user_dir: user_dir.as_ref().join("themes"), default_dir: default_dir.as_ref().join("themes"), } } /// Loads a theme first looking in the `user_dir` then in `default_dir` pub fn load(&self, name: &str) -> Result { if name == "default" { return Ok(self.default()); } if name == "base16_default" { return Ok(self.base16_default()); } let theme = self.load_theme(name, name, false).map(Theme::from)?; Ok(Theme { name: name.into(), ..theme }) } // load the theme and its parent recursively and merge them // `base_theme_name` is the theme from the config.toml, // used to prevent some circular loading scenarios fn load_theme( &self, name: &str, base_them_name: &str, only_default_dir: bool, ) -> Result { let path = self.path(name, only_default_dir); let theme_toml = self.load_toml(path)?; let inherits = theme_toml.get("inherits"); let theme_toml = if let Some(parent_theme_name) = inherits { let parent_theme_name = parent_theme_name.as_str().ok_or_else(|| { anyhow!( "Theme: expected 'inherits' to be a string: {}", parent_theme_name ) })?; let parent_theme_toml = self.load_theme( parent_theme_name, base_them_name, base_them_name == parent_theme_name, )?; self.merge_themes(parent_theme_toml, theme_toml) } else { theme_toml }; Ok(theme_toml) } pub fn read_names(path: &Path) -> Vec { std::fs::read_dir(path) .map(|entries| { entries .filter_map(|entry| { let entry = entry.ok()?; let path = entry.path(); (path.extension()? == "toml") .then(|| path.file_stem().unwrap().to_string_lossy().into_owned()) }) .collect() }) .unwrap_or_default() } // merge one theme into the parent theme fn merge_themes(&self, parent_theme_toml: Value, theme_toml: Value) -> Value { let parent_palette = parent_theme_toml.get("palette"); let palette = theme_toml.get("palette"); // handle the table seperately since it needs a `merge_depth` of 2 // this would conflict with the rest of the theme merge strategy let palette_values = match (parent_palette, palette) { (Some(parent_palette), Some(palette)) => { merge_toml_values(parent_palette.clone(), palette.clone(), 2) } (Some(parent_palette), None) => parent_palette.clone(), (None, Some(palette)) => palette.clone(), (None, None) => Map::new().into(), }; // add the palette correctly as nested table let mut palette = Map::new(); palette.insert(String::from("palette"), palette_values); // merge the theme into the parent theme let theme = merge_toml_values(parent_theme_toml, theme_toml, 1); // merge the before specially handled palette into the theme merge_toml_values(theme, palette.into(), 1) } // Loads the theme data as `toml::Value` first from the user_dir then in default_dir fn load_toml(&self, path: PathBuf) -> Result { let data = std::fs::read(&path)?; let value = toml::from_slice(data.as_slice())?; Ok(value) } // Returns the path to the theme with the name // With `only_default_dir` as false the path will first search for the user path // disabled it ignores the user path and returns only the default path fn path(&self, name: &str, only_default_dir: bool) -> PathBuf { let filename = format!("{}.toml", name); let user_path = self.user_dir.join(&filename); if !only_default_dir && user_path.exists() { user_path } else { self.default_dir.join(filename) } } /// Lists all theme names available in default and user directory pub fn names(&self) -> Vec { let mut names = Self::read_names(&self.user_dir); names.extend(Self::read_names(&self.default_dir)); names } pub fn default_theme(&self, true_color: bool) -> Theme { if true_color { self.default() } else { self.base16_default() } } /// Returns the default theme pub fn default(&self) -> Theme { DEFAULT_THEME.clone() } /// Returns the alternative 16-color default theme pub fn base16_default(&self) -> Theme { BASE16_DEFAULT_THEME.clone() } } #[derive(Clone, Debug, Default)] pub struct Theme { name: String, // UI styles are stored in a HashMap styles: HashMap, // tree-sitter highlight styles are stored in a Vec to optimize lookups scopes: Vec, rainbow_length: usize, highlights: Vec