@@ -1,6 +1,8 @@
1−use helix_core::hashmap;
21use std::collections::HashMap;
32
3+use serde::{Deserialize, Deserializer};
4+use toml::Value;
5+
46#[cfg(feature = "term")]
57pub use tui::style::{Color, Style};
68
@@ -83,69 +85,88 @@ pub use tui::style::{Color, Style};
8385// }
8486
8587/// Color theme for syntax highlighting.
88+#[derive(Debug)]
8689pub struct Theme {
8790 scopes: Vec<String>,
88− mapping: HashMap<&'static str, Style>,
91+ styles: HashMap<String, Style>,
92+}
93+
94+impl<'de> Deserialize<'de> for Theme {
95+ fn deserialize<D>(deserializer: D) -> Result<Theme, D::Error>
96+ where
97+ D: Deserializer<'de>,
98+ {
99+ let mut styles = HashMap::new();
100+
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);
109+ }
110+ }
111+
112+ let scopes = styles.keys().map(ToString::to_string).collect();
113+ Ok(Theme { scopes, styles })
114+ }
115+}
116+
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() {
121+ "fg" => {
122+ if let Some(color) = parse_color(value) {
123+ *style = style.fg(color);
124+ }
125+ }
126+ "bg" => {
127+ if let Some(color) = parse_color(value) {
128+ *style = style.bg(color);
129+ }
130+ }
131+ _ => (),
132+ }
133+ }
134+ } else if let Some(color) = parse_color(value) {
135+ *style = style.fg(color);
136+ }
137+}
138+
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),
145+ ) {
146+ Some((red, green, blue))
147+ } else {
148+ None
149+ }
150+ } else {
151+ None
152+ }
89153}
90154
91−impl Default for Theme {
92− fn default() -> Self {
93− let mapping = hashmap! {
94− "attribute" => Style::default().fg(Color::Rgb(219, 191, 239)), // lilac
95− "keyword" => Style::default().fg(Color::Rgb(236, 205, 186)), // almond
96− "punctuation" => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
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
115−
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::
122− //
123− // concat (ERROR) @syntax-error and "MISSING ;" selectors for errors
124−
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
128−
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
133−
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)),
138− };
139−
140− let scopes = mapping.keys().map(ToString::to_string).collect();
141−
142− Self { scopes, mapping }
155+fn parse_color(value: Value) -> Option<Color> {
156+ if let Value::String(s) = value {
157+ if let Some((red, green, blue)) = hex_string_to_rgb(&s) {
158+ Some(Color::Rgb(red, green, blue))
159+ } else {
160+ None
161+ }
162+ } else {
163+ None
143164 }
144165}
145166
146167impl Theme {
147168 pub fn get(&self, scope: &str) -> Style {
148− self.mapping
169+ self.styles
149170 .get(scope)
150171 .copied()
151172 .unwrap_or_else(|| Style::default().fg(Color::Rgb(0, 0, 255)))