theme: Properly merge SyntaxTheme styles to allow for partial overrides (#11911)

This PR improves the merging behavior for the `SyntaxTheme` such that
user-provided values get merged into the base theme.

This makes it possible to override individual styles without clobbering
the unspecified styles in the base theme.

Release Notes:

- Improved merging of `syntax` styles in the theme.
This commit is contained in:
Marshall Bowers
2024-05-16 09:58:47 -04:00
committed by GitHub
parent 4ba57d730b
commit 178ffabca6
3 changed files with 188 additions and 44 deletions
+22 -24
View File
@@ -118,10 +118,30 @@ impl ThemeRegistry {
};
player_colors.merge(&user_theme.style.players);
let mut syntax_colors = match user_theme.appearance {
let syntax_theme = match user_theme.appearance {
AppearanceContent::Light => SyntaxTheme::light(),
AppearanceContent::Dark => SyntaxTheme::dark(),
};
let syntax_highlights = user_theme
.style
.syntax
.iter()
.map(|(syntax_token, highlight)| {
(
syntax_token.clone(),
HighlightStyle {
color: highlight
.color
.as_ref()
.and_then(|color| try_parse_color(color).ok()),
font_style: highlight.font_style.map(Into::into),
font_weight: highlight.font_weight.map(Into::into),
..Default::default()
},
)
})
.collect::<Vec<_>>();
let syntax_theme = SyntaxTheme::merge(Arc::new(syntax_theme), syntax_highlights);
let window_background_appearance = user_theme
.style
@@ -129,28 +149,6 @@ impl ThemeRegistry {
.map(Into::into)
.unwrap_or_default();
if !user_theme.style.syntax.is_empty() {
syntax_colors.highlights = user_theme
.style
.syntax
.iter()
.map(|(syntax_token, highlight)| {
(
syntax_token.clone(),
HighlightStyle {
color: highlight
.color
.as_ref()
.and_then(|color| try_parse_color(color).ok()),
font_style: highlight.font_style.map(Into::into),
font_weight: highlight.font_weight.map(Into::into),
..Default::default()
},
)
})
.collect::<Vec<_>>();
}
Theme {
id: uuid::Uuid::new_v4().to_string(),
name: user_theme.name.into(),
@@ -164,7 +162,7 @@ impl ThemeRegistry {
colors: theme_colors,
status: status_colors,
player: player_colors,
syntax: Arc::new(syntax_colors),
syntax: syntax_theme,
accents: Vec::new(),
},
}