Better logic for copying themed player colors into registry (#7867)

Release Notes:

- Fixed a potential panic when themes did not contain enough player
colors ([#7733](https://github.com/zed-industries/zed/issues/7733)).

Thanks to @maxdeviant for the code review and improvements!
This commit is contained in:
Joey Smith 2024-02-15 15:22:23 -07:00 committed by GitHub
parent bf1bcd027c
commit 94426c4393
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -118,30 +118,34 @@ impl ThemeRegistry {
AppearanceContent::Dark => PlayerColors::dark(), AppearanceContent::Dark => PlayerColors::dark(),
}; };
if !user_theme.style.players.is_empty() { if !user_theme.style.players.is_empty() {
player_colors = PlayerColors( for (idx, player) in user_theme.style.players.clone().into_iter().enumerate() {
user_theme let cursor = player
.style
.players
.into_iter()
.map(|player| PlayerColor {
cursor: player
.cursor .cursor
.as_ref() .as_ref()
.and_then(|color| try_parse_color(&color).ok()) .and_then(|color| try_parse_color(&color).ok());
.unwrap_or_default(), let background = player
background: player
.background .background
.as_ref() .as_ref()
.and_then(|color| try_parse_color(&color).ok()) .and_then(|color| try_parse_color(&color).ok());
.unwrap_or_default(), let selection = player
selection: player
.selection .selection
.as_ref() .as_ref()
.and_then(|color| try_parse_color(&color).ok()) .and_then(|color| try_parse_color(&color).ok());
.unwrap_or_default(),
}) if let Some(player_color) = player_colors.0.get_mut(idx) {
.collect(), *player_color = PlayerColor {
); cursor: cursor.unwrap_or(player_color.cursor),
background: background.unwrap_or(player_color.background),
selection: selection.unwrap_or(player_color.selection),
};
} else {
player_colors.0.push(PlayerColor {
cursor: cursor.unwrap_or_default(),
background: background.unwrap_or_default(),
selection: selection.unwrap_or_default(),
});
}
}
} }
let mut syntax_colors = match user_theme.appearance { let mut syntax_colors = match user_theme.appearance {