Fix prevent zero value for buffer line height (#30832)

Closes #30802 

Release Notes:

- Fixed issue where setting `buffer_line_height.custom` to 0 would cause
text to disappear

---------

Co-authored-by: Michael Sloan <michael@zed.dev>
This commit is contained in:
Sergei Kartsev 2025-05-19 02:55:35 +02:00 committed by GitHub
parent 592568ff87
commit a829281841
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -553,10 +553,22 @@ pub enum BufferLineHeight {
Comfortable,
/// The default line height.
Standard,
/// A custom line height.
///
/// A line height of 1.0 is the height of the buffer's font size.
Custom(f32),
/// A custom line height, where 1.0 is the font's height. Must be at least 1.0.
Custom(#[serde(deserialize_with = "deserialize_line_height")] f32),
}
fn deserialize_line_height<'de, D>(deserializer: D) -> Result<f32, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = f32::deserialize(deserializer)?;
if value < 1.0 {
return Err(serde::de::Error::custom(
"buffer_line_height.custom must be at least 1.0",
));
}
Ok(value)
}
impl BufferLineHeight {
@ -1010,3 +1022,54 @@ fn merge<T: Copy>(target: &mut T, value: Option<T>) {
*target = value;
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_buffer_line_height_deserialize_valid() {
assert_eq!(
serde_json::from_value::<BufferLineHeight>(json!("comfortable")).unwrap(),
BufferLineHeight::Comfortable
);
assert_eq!(
serde_json::from_value::<BufferLineHeight>(json!("standard")).unwrap(),
BufferLineHeight::Standard
);
assert_eq!(
serde_json::from_value::<BufferLineHeight>(json!({"custom": 1.0})).unwrap(),
BufferLineHeight::Custom(1.0)
);
assert_eq!(
serde_json::from_value::<BufferLineHeight>(json!({"custom": 1.5})).unwrap(),
BufferLineHeight::Custom(1.5)
);
}
#[test]
fn test_buffer_line_height_deserialize_invalid() {
assert!(
serde_json::from_value::<BufferLineHeight>(json!({"custom": 0.99}))
.err()
.unwrap()
.to_string()
.contains("buffer_line_height.custom must be at least 1.0")
);
assert!(
serde_json::from_value::<BufferLineHeight>(json!({"custom": 0.0}))
.err()
.unwrap()
.to_string()
.contains("buffer_line_height.custom must be at least 1.0")
);
assert!(
serde_json::from_value::<BufferLineHeight>(json!({"custom": -1.0}))
.err()
.unwrap()
.to_string()
.contains("buffer_line_height.custom must be at least 1.0")
);
}
}