ZIm/crates/theme2/src/registry.rs
Marshall Bowers 36a73d657a
Remove old Theme definition (#3195)
This PR removes the old `Theme` definition in favor of the new
`ThemeVariant`s.

The new `SyntaxStyles` have been reverted to the old `SyntaxTheme` that
operates by storing the syntax styles as a vector of
`gpui2::HighlightStyle`s.

This is necessary for the intended usage by `language2`, where we find
the longest key in the theme's syntax styles that matches the capture
name:

18431051d9/crates/language2/src/highlight_map.rs (L15-L41)
2023-10-31 23:05:50 -04:00

49 lines
1.3 KiB
Rust

use crate::{zed_pro_family, ThemeFamily, ThemeVariant};
use anyhow::{anyhow, Result};
use gpui2::SharedString;
use std::{collections::HashMap, sync::Arc};
pub struct ThemeRegistry {
themes: HashMap<SharedString, Arc<ThemeVariant>>,
}
impl ThemeRegistry {
fn insert_theme_families(&mut self, families: impl IntoIterator<Item = ThemeFamily>) {
for family in families.into_iter() {
self.insert_themes(family.themes);
}
}
fn insert_themes(&mut self, themes: impl IntoIterator<Item = ThemeVariant>) {
for theme in themes.into_iter() {
self.themes.insert(theme.name.clone(), Arc::new(theme));
}
}
pub fn list_names(&self, _staff: bool) -> impl Iterator<Item = SharedString> + '_ {
self.themes.keys().cloned()
}
pub fn list(&self, _staff: bool) -> impl Iterator<Item = SharedString> + '_ {
self.themes.values().map(|theme| theme.name.clone())
}
pub fn get(&self, name: &str) -> Result<Arc<ThemeVariant>> {
self.themes
.get(name)
.ok_or_else(|| anyhow!("theme not found: {}", name))
.cloned()
}
}
impl Default for ThemeRegistry {
fn default() -> Self {
let mut this = Self {
themes: HashMap::default(),
};
this.insert_theme_families([zed_pro_family()]);
this
}
}