1use anyhow::Result;
2use indexmap::IndexMap;
3use strum::IntoEnumIterator;
4use theme::{
5 FontStyleContent, FontWeightContent, HighlightStyleContent, StatusColorsContent,
6 ThemeColorsContent, ThemeContent, ThemeStyleContent,
7};
8
9use crate::vscode::{VsCodeTheme, VsCodeTokenScope};
10use crate::ThemeMetadata;
11
12use super::ZedSyntaxToken;
13
14pub(crate) fn try_parse_font_weight(font_style: &str) -> Option<FontWeightContent> {
15 match font_style {
16 style if style.contains("bold") => Some(FontWeightContent::Bold),
17 _ => None,
18 }
19}
20
21pub(crate) fn try_parse_font_style(font_style: &str) -> Option<FontStyleContent> {
22 match font_style {
23 style if style.contains("italic") => Some(FontStyleContent::Italic),
24 style if style.contains("oblique") => Some(FontStyleContent::Oblique),
25 _ => None,
26 }
27}
28
29pub struct VsCodeThemeConverter {
30 theme: VsCodeTheme,
31 theme_metadata: ThemeMetadata,
32 syntax_overrides: IndexMap<String, Vec<String>>,
33}
34
35impl VsCodeThemeConverter {
36 pub fn new(
37 theme: VsCodeTheme,
38 theme_metadata: ThemeMetadata,
39 syntax_overrides: IndexMap<String, Vec<String>>,
40 ) -> Self {
41 Self {
42 theme,
43 theme_metadata,
44 syntax_overrides,
45 }
46 }
47
48 pub fn convert(self) -> Result<ThemeContent> {
49 let appearance = self.theme_metadata.appearance.into();
50
51 let status_colors = self.convert_status_colors()?;
52 let theme_colors = self.convert_theme_colors()?;
53 let syntax_theme = self.convert_syntax_theme()?;
54
55 Ok(ThemeContent {
56 name: self.theme_metadata.name,
57 appearance,
58 style: ThemeStyleContent {
59 window_background_appearance: Some(theme::WindowBackgroundContent::Opaque),
60 accents: Vec::new(), //TODO can we read this from the theme?
61 colors: theme_colors,
62 status: status_colors,
63 players: Vec::new(),
64 syntax: syntax_theme,
65 },
66 })
67 }
68
69 fn convert_status_colors(&self) -> Result<StatusColorsContent> {
70 let vscode_colors = &self.theme.colors;
71
72 let vscode_base_status_colors = StatusColorsContent {
73 hint: Some("#969696ff".to_string()),
74 ..Default::default()
75 };
76
77 Ok(StatusColorsContent {
78 conflict: vscode_colors
79 .git_decoration
80 .conflicting_resource_foreground
81 .clone(),
82 created: vscode_colors.editor_gutter.added_background.clone(),
83 deleted: vscode_colors.editor_gutter.deleted_background.clone(),
84 error: vscode_colors.editor_error.foreground.clone(),
85 error_background: vscode_colors.editor_error.background.clone(),
86 error_border: vscode_colors.editor_error.border.clone(),
87 hidden: vscode_colors.tab.inactive_foreground.clone(),
88 hint: vscode_colors
89 .editor_inlay_hint
90 .foreground
91 .clone()
92 .or(vscode_base_status_colors.hint),
93 hint_border: vscode_colors.editor_hint.border.clone(),
94 ignored: vscode_colors
95 .git_decoration
96 .ignored_resource_foreground
97 .clone(),
98 info: vscode_colors.editor_info.foreground.clone(),
99 info_background: vscode_colors.editor_info.background.clone(),
100 info_border: vscode_colors.editor_info.border.clone(),
101 modified: vscode_colors.editor_gutter.modified_background.clone(),
102 // renamed: None,
103 // success: None,
104 warning: vscode_colors.editor_warning.foreground.clone(),
105 warning_background: vscode_colors.editor_warning.background.clone(),
106 warning_border: vscode_colors.editor_warning.border.clone(),
107 ..Default::default()
108 })
109 }
110
111 fn convert_theme_colors(&self) -> Result<ThemeColorsContent> {
112 let vscode_colors = &self.theme.colors;
113
114 let vscode_panel_border = vscode_colors.panel.border.clone();
115 let vscode_tab_inactive_background = vscode_colors.tab.inactive_background.clone();
116 let vscode_editor_foreground = vscode_colors.editor.foreground.clone();
117 let vscode_editor_background = vscode_colors.editor.background.clone();
118 let vscode_scrollbar_slider_background = vscode_colors.scrollbar_slider.background.clone();
119 let vscode_token_colors_foreground = self
120 .theme
121 .token_colors
122 .iter()
123 .find(|token_color| token_color.scope.is_none())
124 .and_then(|token_color| token_color.settings.foreground.as_ref())
125 .cloned();
126
127 Ok(ThemeColorsContent {
128 border: vscode_panel_border.clone(),
129 border_variant: vscode_panel_border.clone(),
130 border_focused: vscode_colors.focus_border.clone(),
131 border_selected: vscode_panel_border.clone(),
132 border_transparent: vscode_panel_border.clone(),
133 border_disabled: vscode_panel_border.clone(),
134 elevated_surface_background: vscode_colors.dropdown.background.clone(),
135 surface_background: vscode_colors.panel.background.clone(),
136 background: vscode_editor_background.clone(),
137 element_background: vscode_colors.button.background.clone(),
138 element_hover: vscode_colors.list.hover_background.clone(),
139 element_selected: vscode_colors.list.active_selection_background.clone(),
140 drop_target_background: vscode_colors.list.drop_background.clone(),
141 ghost_element_hover: vscode_colors.list.hover_background.clone(),
142 ghost_element_selected: vscode_colors.list.active_selection_background.clone(),
143 text: vscode_colors
144 .foreground
145 .clone()
146 .or(vscode_token_colors_foreground.clone()),
147 text_muted: vscode_colors.tab.inactive_foreground.clone(),
148 status_bar_background: vscode_colors.status_bar.background.clone(),
149 title_bar_background: vscode_colors.title_bar.active_background.clone(),
150 toolbar_background: vscode_colors
151 .breadcrumb
152 .background
153 .clone()
154 .or(vscode_editor_background.clone()),
155 tab_bar_background: vscode_colors.editor_group_header.tabs_background.clone(),
156 tab_inactive_background: vscode_tab_inactive_background.clone(),
157 tab_active_background: vscode_colors
158 .tab
159 .active_background
160 .clone()
161 .or(vscode_tab_inactive_background.clone()),
162 panel_background: vscode_colors.panel.background.clone(),
163 scrollbar_thumb_background: vscode_scrollbar_slider_background.clone(),
164 scrollbar_thumb_hover_background: vscode_colors
165 .scrollbar_slider
166 .hover_background
167 .clone(),
168 scrollbar_thumb_border: vscode_scrollbar_slider_background.clone(),
169 scrollbar_track_background: vscode_editor_background.clone(),
170 scrollbar_track_border: vscode_colors.editor_overview_ruler.border.clone(),
171 pane_group_border: vscode_colors.editor_group.border.clone(),
172 editor_foreground: vscode_editor_foreground
173 .clone()
174 .or(vscode_token_colors_foreground.clone()),
175 editor_background: vscode_editor_background.clone(),
176 editor_gutter_background: vscode_editor_background.clone(),
177 editor_active_line_background: vscode_colors.editor.line_highlight_background.clone(),
178 editor_line_number: vscode_colors.editor_line_number.foreground.clone(),
179 editor_active_line_number: vscode_colors.editor.foreground.clone(),
180 editor_wrap_guide: vscode_panel_border.clone(),
181 editor_active_wrap_guide: vscode_panel_border.clone(),
182 terminal_background: vscode_colors.terminal.background.clone(),
183 terminal_ansi_black: vscode_colors.terminal.ansi_black.clone(),
184 terminal_ansi_bright_black: vscode_colors.terminal.ansi_bright_black.clone(),
185 terminal_ansi_red: vscode_colors.terminal.ansi_red.clone(),
186 terminal_ansi_bright_red: vscode_colors.terminal.ansi_bright_red.clone(),
187 terminal_ansi_green: vscode_colors.terminal.ansi_green.clone(),
188 terminal_ansi_bright_green: vscode_colors.terminal.ansi_bright_green.clone(),
189 terminal_ansi_yellow: vscode_colors.terminal.ansi_yellow.clone(),
190 terminal_ansi_bright_yellow: vscode_colors.terminal.ansi_bright_yellow.clone(),
191 terminal_ansi_blue: vscode_colors.terminal.ansi_blue.clone(),
192 terminal_ansi_bright_blue: vscode_colors.terminal.ansi_bright_blue.clone(),
193 terminal_ansi_magenta: vscode_colors.terminal.ansi_magenta.clone(),
194 terminal_ansi_bright_magenta: vscode_colors.terminal.ansi_bright_magenta.clone(),
195 terminal_ansi_cyan: vscode_colors.terminal.ansi_cyan.clone(),
196 terminal_ansi_bright_cyan: vscode_colors.terminal.ansi_bright_cyan.clone(),
197 terminal_ansi_white: vscode_colors.terminal.ansi_white.clone(),
198 terminal_ansi_bright_white: vscode_colors.terminal.ansi_bright_white.clone(),
199 link_text_hover: vscode_colors.text_link.active_foreground.clone(),
200 ..Default::default()
201 })
202 }
203
204 fn convert_syntax_theme(&self) -> Result<IndexMap<String, HighlightStyleContent>> {
205 let mut highlight_styles = IndexMap::new();
206
207 for syntax_token in ZedSyntaxToken::iter() {
208 let override_match = self
209 .syntax_overrides
210 .get(&syntax_token.to_string())
211 .and_then(|scope| {
212 self.theme.token_colors.iter().find(|token_color| {
213 token_color.scope == Some(VsCodeTokenScope::Many(scope.clone()))
214 })
215 });
216
217 let best_match = override_match
218 .or_else(|| syntax_token.find_best_token_color_match(&self.theme.token_colors))
219 .or_else(|| {
220 syntax_token.fallbacks().iter().find_map(|fallback| {
221 fallback.find_best_token_color_match(&self.theme.token_colors)
222 })
223 });
224
225 let Some(token_color) = best_match else {
226 log::warn!("No matching token color found for '{syntax_token}'");
227 continue;
228 };
229
230 log::info!(
231 "Matched '{syntax_token}' to '{}'",
232 token_color
233 .name
234 .clone()
235 .or_else(|| token_color
236 .scope
237 .as_ref()
238 .map(|scope| format!("{:?}", scope)))
239 .unwrap_or_else(|| "no identifier".to_string())
240 );
241
242 let highlight_style = HighlightStyleContent {
243 color: token_color.settings.foreground.clone(),
244 background_color: token_color.settings.background.clone(),
245 font_style: token_color
246 .settings
247 .font_style
248 .as_ref()
249 .and_then(|style| try_parse_font_style(style)),
250 font_weight: token_color
251 .settings
252 .font_style
253 .as_ref()
254 .and_then(|style| try_parse_font_weight(style)),
255 };
256
257 if highlight_style.is_empty() {
258 continue;
259 }
260
261 highlight_styles.insert(syntax_token.to_string(), highlight_style);
262 }
263
264 Ok(highlight_styles)
265 }
266}