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