use gpui_kit::component::input::MaskPattern;
use gpui_kit::{App, Window};
use rust_i18n::t;
use super::{InputField, initial_text, push_raw_text};
use crate::{
model::{CellValue, GridRow, OwnedValue},
state::edit::{CellEditor, EditError, EditInput, EditorOpen, EditorStart},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NumberKind {
Int,
Float,
#[cfg(feature = "decimal")]
Decimal,
}
#[derive(Clone, Debug)]
pub struct NumberEditor {
kind: Option<NumberKind>,
min: Option<OwnedValue>,
max: Option<OwnedValue>,
scale: Option<u32>,
decimal_separator: char,
}
impl Default for NumberEditor {
fn default() -> Self {
Self::new()
}
}
impl NumberEditor {
pub fn new() -> Self {
Self {
kind: None,
min: None,
max: None,
scale: None,
decimal_separator: '.',
}
}
pub fn with_kind(mut self, kind: NumberKind) -> Self {
self.kind = Some(kind);
self
}
pub fn with_min(mut self, min: impl Into<OwnedValue>) -> Self {
self.min = Some(min.into());
self
}
pub fn with_max(mut self, max: impl Into<OwnedValue>) -> Self {
self.max = Some(max.into());
self
}
pub fn with_scale(mut self, scale: u32) -> Self {
self.scale = Some(scale);
self
}
pub fn with_decimal_separator(mut self, separator: char) -> Self {
self.decimal_separator = separator;
self
}
pub fn kind(&self) -> Option<NumberKind> {
self.kind
}
pub fn min(&self) -> Option<&OwnedValue> {
self.min.as_ref()
}
pub fn max(&self) -> Option<&OwnedValue> {
self.max.as_ref()
}
pub fn scale(&self) -> Option<u32> {
self.scale
}
pub fn decimal_separator(&self) -> char {
self.decimal_separator
}
fn kind_for(&self, value: CellValue<'_>) -> NumberKind {
self.kind.unwrap_or(match value {
CellValue::Int(_) => NumberKind::Int,
#[cfg(feature = "decimal")]
CellValue::Decimal(_) => NumberKind::Decimal,
_ => NumberKind::Float,
})
}
fn may_start(&self, text: &str) -> bool {
text.chars().all(|ch| {
ch.is_ascii_digit()
|| matches!(ch, '-' | '+' | '.' | ',')
|| ch == self.decimal_separator
})
}
fn parse(&self, draft: &str, kind: NumberKind) -> Result<OwnedValue, EditError> {
let separator = self.decimal_separator;
let thousands = if separator == '.' { ',' } else { '.' };
let text: String = draft
.chars()
.filter(|ch| !ch.is_whitespace() && *ch != thousands && *ch != '\'')
.map(|ch| if ch == separator { '.' } else { ch })
.collect();
if text.is_empty() {
return Ok(OwnedValue::Empty);
}
let not_a_number = || EditError::new(t!("live_grid.edit.not_a_number"));
let is_plain = text
.trim_start_matches(['-', '+'])
.chars()
.all(|ch| ch.is_ascii_digit() || ch == '.')
&& text.matches('.').count() <= 1
&& text.chars().any(|ch| ch.is_ascii_digit());
if !is_plain {
return Err(not_a_number());
}
let decimals = text
.split_once('.')
.map_or(0, |(_, fraction)| fraction.trim_end_matches('0').len());
if let Some(scale) = self.scale
&& decimals > scale as usize
{
return Err(EditError::new(t!(
"live_grid.edit.too_many_decimals",
scale = scale
)));
}
let value = match kind {
NumberKind::Int if decimals > 0 => {
return Err(EditError::new(t!("live_grid.edit.not_a_whole_number")));
}
NumberKind::Int => {
let whole = text.split('.').next().unwrap_or_default();
OwnedValue::Int(whole.parse().map_err(|_| not_a_number())?)
}
NumberKind::Float => {
let value: f64 = text.parse().map_err(|_| not_a_number())?;
if !value.is_finite() {
return Err(not_a_number());
}
OwnedValue::Float(value)
}
#[cfg(feature = "decimal")]
NumberKind::Decimal => OwnedValue::Decimal(
rust_decimal::Decimal::from_str_exact(text.trim_start_matches('+'))
.map_err(|_| not_a_number())?,
),
};
let text_of = |limit: &OwnedValue| {
let mut out = String::new();
push_raw_text(limit.as_cell(), separator, &mut out);
out
};
if let Some(min) = &self.min
&& value.cmp_total(min).is_lt()
{
return Err(EditError::new(t!(
"live_grid.edit.below_min",
min = text_of(min)
)));
}
if let Some(max) = &self.max
&& value.cmp_total(max).is_gt()
{
return Err(EditError::new(t!(
"live_grid.edit.above_max",
max = text_of(max)
)));
}
Ok(value)
}
}
impl<R: GridRow> CellEditor<R> for NumberEditor {
fn open(&self, start: &EditorStart<'_, R>, window: &mut Window, cx: &mut App) -> EditorOpen {
if let EditInput::Typing(text) = start.input()
&& !self.may_start(text)
{
return EditorOpen::Refuse;
}
let kind = self.kind_for(start.value());
let text = initial_text(start, self.decimal_separator);
let mask = (self.decimal_separator == '.').then_some(MaskPattern::Number {
separator: None,
fraction: None,
});
let state = InputField::input_state(
&text,
|state| match mask {
Some(mask) => state.mask_pattern(mask),
None => state,
},
window,
cx,
);
let editor = self.clone();
EditorOpen::Field(Box::new(
InputField::new(
state,
text,
true,
Box::new(move |draft| editor.parse(draft, kind)),
)
.with_decimal_separator(self.decimal_separator),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn error(result: Result<OwnedValue, EditError>) -> String {
result.expect_err("refused").message().to_string()
}
#[test]
fn test_number_editor_parse() {
let editor = NumberEditor::new();
assert_eq!(
editor.parse("-12", NumberKind::Int),
Ok(OwnedValue::Int(-12))
);
assert_eq!(
editor.parse("-0.5", NumberKind::Float),
Ok(OwnedValue::Float(-0.5))
);
assert_eq!(editor.parse(" ", NumberKind::Float), Ok(OwnedValue::Empty));
assert_eq!(
editor.parse("1 234.50", NumberKind::Float),
Ok(OwnedValue::Float(1234.5))
);
assert_eq!(
editor.parse("1,234", NumberKind::Int),
Ok(OwnedValue::Int(1234))
);
assert_eq!(
error(editor.parse("1.5", NumberKind::Int)),
"Enter a whole number"
);
assert_eq!(
editor.parse("2.00", NumberKind::Int),
Ok(OwnedValue::Int(2))
);
for text in ["abc", "1e3", "inf", "NaN", "1.2.3", "-", "."] {
assert_eq!(
error(editor.parse(text, NumberKind::Float)),
"Enter a number",
"{text}"
);
}
let limited = NumberEditor::new()
.with_min(OwnedValue::Int(0))
.with_max(OwnedValue::Float(200.5))
.with_scale(2);
assert_eq!(
error(limited.parse("-1", NumberKind::Float)),
"Enter 0 or more"
);
assert_eq!(
error(limited.parse("201", NumberKind::Float)),
"Enter 200.5 or less"
);
assert_eq!(
error(limited.parse("1.234", NumberKind::Float)),
"Use at most 2 decimals"
);
assert_eq!(
limited.parse("1.230", NumberKind::Float),
Ok(OwnedValue::Float(1.23))
);
let comma = NumberEditor::new().with_decimal_separator(',');
assert_eq!(
comma.parse("1.234,5", NumberKind::Float),
Ok(OwnedValue::Float(1234.5))
);
assert_eq!(comma.decimal_separator(), ',');
assert!(editor.may_start("5") && editor.may_start("-") && editor.may_start("."));
assert!(!editor.may_start("a") && !editor.may_start("+a"));
assert_eq!(editor.kind_for(CellValue::Int(1)), NumberKind::Int);
assert_eq!(editor.kind_for(CellValue::Empty), NumberKind::Float);
assert_eq!(
editor
.clone()
.with_kind(NumberKind::Int)
.kind_for(CellValue::Float(1.)),
NumberKind::Int
);
assert_eq!(limited.scale(), Some(2));
assert_eq!(limited.min(), Some(&OwnedValue::Int(0)));
assert_eq!(limited.max(), Some(&OwnedValue::Float(200.5)));
assert_eq!(editor.kind(), None);
}
#[cfg(feature = "decimal")]
#[test]
fn test_number_editor_decimal() {
let editor = NumberEditor::new().with_scale(2);
let value = editor.parse("12.30", NumberKind::Decimal);
assert!(
matches!(&value, Ok(OwnedValue::Decimal(value)) if value.to_string() == "12.30"),
"{value:?}"
);
assert_eq!(
error(editor.parse("1.234", NumberKind::Decimal)),
"Use at most 2 decimals"
);
}
}