1use std::collections::HashMap;
2
3use crate::HighlightId;
4use gpui::fonts::HighlightStyle;
5use serde::Deserialize;
6
7pub struct SyntaxTheme {
8 pub(crate) highlights: Vec<(String, HighlightStyle)>,
9}
10
11impl SyntaxTheme {
12 pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
13 Self { highlights }
14 }
15
16 pub fn highlight_style(&self, id: HighlightId) -> Option<HighlightStyle> {
17 self.highlights
18 .get(id.0 as usize)
19 .map(|entry| entry.1.clone())
20 }
21
22 #[cfg(any(test, feature = "test-support"))]
23 pub fn highlight_name(&self, id: HighlightId) -> Option<&str> {
24 self.highlights.get(id.0 as usize).map(|e| e.0.as_str())
25 }
26}
27
28impl<'de> Deserialize<'de> for SyntaxTheme {
29 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
30 where
31 D: serde::Deserializer<'de>,
32 {
33 let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
34
35 let mut result = Self::new(Vec::new());
36 for (key, style) in syntax_data {
37 match result
38 .highlights
39 .binary_search_by(|(needle, _)| needle.cmp(&key))
40 {
41 Ok(i) | Err(i) => {
42 result.highlights.insert(i, (key, style));
43 }
44 }
45 }
46
47 Ok(result)
48 }
49}