@@ -1,6 +1,8 @@
1−use helix_core::hashmap;
2use std::collections::HashMap;
1use std::collections::HashMap;
3+use serde::{Deserialize, Deserializer};
4#[cfg(feature = "term")]
6#[cfg(feature = "term")]
5pub use tui::style::{Color, Style};
7pub use tui::style::{Color, Style};
@@ -83,69 +85,88 @@ pub use tui::style::{Color, Style};
85/// Color theme for syntax highlighting.
87/// Color theme for syntax highlighting.
86pub struct Theme {
89pub struct Theme {
87 scopes: Vec<String>,
90 scopes: Vec<String>,
88− mapping: HashMap<&'static str, Style>,
91+ styles: HashMap<String, Style>,
94+impl<'de> Deserialize<'de> for Theme {
95+ fn deserialize<D>(deserializer: D) -> Result<Theme, D::Error>
97+ D: Deserializer<'de>,
99+ let mut styles = HashMap::new();
101+ if let Ok(colors) = HashMap::<String, Value>::deserialize(deserializer) {
102+ // scopes.reserve(colors.len());
103+ styles.reserve(colors.len());
104+ for (name, style_value) in colors {
105+ let mut style = Style::default();
106+ parse_style(&mut style, style_value);
107+ // scopes.push(name);
108+ styles.insert(name, style);
112+ let scopes = styles.keys().map(ToString::to_string).collect();
113+ Ok(Theme { scopes, styles })
117+fn parse_style(style: &mut Style, value: Value) {
118+ if let Value::Table(entries) = value {
119+ for (name, value) in entries {
120+ match name.as_str() {
122+ if let Some(color) = parse_color(value) {
123+ *style = style.fg(color);
127+ if let Some(color) = parse_color(value) {
128+ *style = style.bg(color);
134+ } else if let Some(color) = parse_color(value) {
135+ *style = style.fg(color);
139+fn hex_string_to_rgb(s: &str) -> Option<(u8, u8, u8)> {
140+ if s.starts_with("#") && s.len() >= 7 {
141+ if let (Ok(red), Ok(green), Ok(blue)) = (
142+ u8::from_str_radix(&s[1..3], 16),
143+ u8::from_str_radix(&s[3..5], 16),
144+ u8::from_str_radix(&s[5..7], 16),
146+ Some((red, green, blue))
91−impl Default for Theme {
155+fn parse_color(value: Value) -> Option<Color> {
92− fn default() -> Self {
156+ if let Value::String(s) = value {
93− let mapping = hashmap! {
157+ if let Some((red, green, blue)) = hex_string_to_rgb(&s) {
94− "attribute" => Style::default().fg(Color::Rgb(219, 191, 239)), // lilac
158+ Some(Color::Rgb(red, green, blue))
95− "keyword" => Style::default().fg(Color::Rgb(236, 205, 186)), // almond
159+ } else {
96− "punctuation" => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
160+ None
97− "punctuation.delimiter" => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
98− "operator" => Style::default().fg(Color::Rgb(219, 191, 239)), // lilac
99− "property" => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
100− "variable.parameter" => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
101− // TODO distinguish type from type.builtin?
102− "type" => Style::default().fg(Color::Rgb(255, 255, 255)), // white
103− "type.builtin" => Style::default().fg(Color::Rgb(255, 255, 255)), // white
104− "constructor" => Style::default().fg(Color::Rgb(219, 191, 239)), // lilac
105− "function" => Style::default().fg(Color::Rgb(255, 255, 255)), // white
106− "function.macro" => Style::default().fg(Color::Rgb(219, 191, 239)), // lilac
107− "comment" => Style::default().fg(Color::Rgb(105, 124, 129)), // sirocco
108− "variable.builtin" => Style::default().fg(Color::Rgb(159, 242, 143)), // mint
109− "constant" => Style::default().fg(Color::Rgb(255, 255, 255)), // white
110− "constant.builtin" => Style::default().fg(Color::Rgb(255, 255, 255)), // white
111− "string" => Style::default().fg(Color::Rgb(204, 204, 204)), // silver
112− "escape" => Style::default().fg(Color::Rgb(239, 186, 93)), // honey
113− // used for lifetimes
114− "label" => Style::default().fg(Color::Rgb(239, 186, 93)), // honey
116− // TODO: diferentiate number builtin
117− // TODO: diferentiate doc comment
118− // TODO: variable as lilac
119− // TODO: mod/use statements as white
120− // TODO: mod stuff as chamoise
121− // TODO: add "(scoped_identifier) @path" for std::mem::
123− // concat (ERROR) @syntax-error and "MISSING ;" selectors for errors
125− "module" => Style::default().fg(Color::Rgb(255, 0, 0)), // white
126− "variable" => Style::default().fg(Color::Rgb(255, 0, 0)), // white
127− "function.builtin" => Style::default().fg(Color::Rgb(255, 0, 0)), // white
129− "ui.background" => Style::default().bg(Color::Rgb(59, 34, 76)), // midnight
130− "ui.linenr" => Style::default().fg(Color::Rgb(90, 89, 119)), // comet
131− "ui.statusline" => Style::default().bg(Color::Rgb(40, 23, 51)), // revolver
132− "ui.popup" => Style::default().bg(Color::Rgb(40, 23, 51)), // revolver
134− "warning" => Style::default().fg(Color::Rgb(255, 205, 28)),
135− "error" => Style::default().fg(Color::Rgb(244, 120, 104)),
136− "info" => Style::default().fg(Color::Rgb(111, 68, 240)),
137− "hint" => Style::default().fg(Color::Rgb(204, 204, 204)),
140− let scopes = mapping.keys().map(ToString::to_string).collect();
163+ None
142− Self { scopes, mapping }
146impl Theme {
167impl Theme {
147 pub fn get(&self, scope: &str) -> Style {
168 pub fn get(&self, scope: &str) -> Style {
148− self.mapping
169+ self.styles
149 .get(scope)
170 .get(scope)
150 .copied()
171 .copied()
151 .unwrap_or_else(|| Style::default().fg(Color::Rgb(0, 0, 255)))
172 .unwrap_or_else(|| Style::default().fg(Color::Rgb(0, 0, 255)))