From b9324d91796fc91b53c7e4042de021dcc2cddafc Mon Sep 17 00:00:00 2001 From: kezmdor <15539254+kezmdor@users.noreply.github.com> Date: Fri, 23 Oct 2026 09:13:27 +0200 Subject: [PATCH] Formula entry: text that would read as a formula is edited with its apostrophe `text_needs_quote_prefix` still used the old rule (a sign followed by a letter or `(`), so text such as `+5+4` (typed as `'+5+4`, or read from a CSV or XLSX) was edited without its apostrophe and became a formula when confirmed unchanged. It now asks what `input_to_cell` asks: the entry starts a formula and parses as one. Text that stays text anyway (`--`) is still edited as it is. Found by @iluebbe in #2023. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/display.rs | 10 +++++++--- crates/engine/src/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/display.rs b/crates/engine/src/display.rs index aa8bdb22dd..ad94c808c1 100644 --- a/crates/engine/src/display.rs +++ b/crates/engine/src/display.rs @@ -41,12 +41,16 @@ fn text_needs_quote_prefix(t: &str, sys: gridcraft_core::date::DateSystem) -> bo if t.is_empty() { return false; } - let mut chars = t.chars(); - let (first, second) = (chars.next(), chars.next()); + let first = t.chars().next(); if matches!(first, Some('=' | '\'')) { return true; } - if matches!(first, Some('+' | '-')) && second.is_some_and(|c| c.is_ascii_alphabetic() || c == '(') { + // A `+` or `-` entry that reads as a formula becomes one (`+5+4`, `-abc`); one that doesn't + // stays text (`--`, `+49 170 1234567`), as `input_to_cell` decides it. + if matches!(first, Some('+' | '-')) + && gridcraft_core::parse::starts_formula(t) + && crate::cmd::edit::parse_typed_formula(t).is_ok_and(|e| crate::cmd::edit::check_arg_counts(&e).is_ok()) + { return true; } !matches!(gridcraft_core::parse::parse_input(t, sys).value, Value::Text(ref x) if x.as_ref() == t) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 285d64ce97..c10a39a9cd 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -267,6 +267,26 @@ fn a_leading_plus_or_minus_starts_a_formula() { assert_eq!(v(&s, "B1"), value, "{typed}"); assert!(!input(&s, "B1").is_some_and(|t| t.starts_with('=')), "{typed}"); } + // Text that would read as a formula is edited with its apostrophe, so confirming the editor + // unchanged keeps it text; text that wouldn't is edited as it is. + for (typed, shown) in [ + ("'+5+4", "'+5+4"), + ("'-5+4", "'-5+4"), + ("'+1-555-1234", "'+1-555-1234"), + ("'-abc", "'-abc"), + ("--", "--"), + ("+49 170 1234567", "+49 170 1234567"), + ] { + s.execute("cell.set", json!({"cell": "B2", "input": typed})).unwrap(); + let before = v(&s, "B2"); + let edit = { + let d = s.doc().unwrap(); + crate::display::edit_text(&d.wb, d.wb.active().unwrap(), CellRef::parse("B2").unwrap()) + }; + assert_eq!(edit, shown, "{typed}"); + s.execute("cell.set", json!({"cell": "B2", "input": edit})).unwrap(); + assert_eq!(v(&s, "B2"), before, "{typed} stays text after an unchanged edit"); + } // As observed in Excel for the web, an entry that doesn't read as a formula stays text, and // one that does but names nothing known is #NAME?. Only `=` entries are refused. for typed in ["--", "-----", "+5+", "+49 170 1234567"] {