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 .cursor
.players .as_ref()
.into_iter() .and_then(|color| try_parse_color(&color).ok());
.map(|player| PlayerColor { let background = player
cursor: player .background
.cursor .as_ref()
.as_ref() .and_then(|color| try_parse_color(&color).ok());
.and_then(|color| try_parse_color(&color).ok()) let selection = player
.unwrap_or_default(), .selection
background: player .as_ref()
.background .and_then(|color| try_parse_color(&color).ok());
.as_ref()
.and_then(|color| try_parse_color(&color).ok()) if let Some(player_color) = player_colors.0.get_mut(idx) {
.unwrap_or_default(), *player_color = PlayerColor {
selection: player cursor: cursor.unwrap_or(player_color.cursor),
.selection background: background.unwrap_or(player_color.background),
.as_ref() selection: selection.unwrap_or(player_color.selection),
.and_then(|color| try_parse_color(&color).ok()) };
.unwrap_or_default(), } else {
}) player_colors.0.push(PlayerColor {
.collect(), 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 {