From 8d2481a7481cb3bef81f5091f8e7f20abe98e4bb Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Thu, 12 Nov 2026 21:53:35 -0500 Subject: [PATCH] craft-math: an empty shown n-ary limit survives linear text (port of wordcraft#6085) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit to_linear wrote a limit only when it wasn't empty, so half-typed ∑_ (an empty, shown lower limit) came out as ∑▒() and read back with both limits hidden. A shown limit is now written even when empty, as _() or ^(). Ported from storytold/wordcraft#6085 (issue wordcraft#6076): its change to math.rs and its tests/linear_empty_limits.rs, with only paths and crate names changed. --- crates/math/src/math.rs | 5 +++-- crates/math/tests/linear_empty_limits.rs | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 crates/math/tests/linear_empty_limits.rs diff --git a/crates/math/src/math.rs b/crates/math/src/math.rs index 08d488b11..11f07f6b4 100644 --- a/crates/math/src/math.rs +++ b/crates/math/src/math.rs @@ -634,11 +634,12 @@ fn lin_node(n: &MNode, s: &mut String, depth: usize) { } MNode::Nary { chr, sub_hide, sup_hide, sub, sup, e, .. } => { s.push(*chr); - if !sub_hide && !sub.is_empty() { + // A shown limit is written even when empty (`_()`): left out, it reads back hidden (#6076). + if !sub_hide { s.push('_'); lin_script(sub, s, depth); } - if !sup_hide && !sup.is_empty() { + if !sup_hide { s.push('^'); lin_script(sup, s, depth); } diff --git a/crates/math/tests/linear_empty_limits.rs b/crates/math/tests/linear_empty_limits.rs new file mode 100644 index 000000000..7fccb879e --- /dev/null +++ b/crates/math/tests/linear_empty_limits.rs @@ -0,0 +1,24 @@ +//! An n-ary operator's empty but shown limit survives linear text (#6076): half-typed `∑_` has an +//! empty lower limit that is shown, and `to_linear` writes it as `_()`. Left out, it reads back as a +//! hidden limit. + +use craft_math::math::{MNode, MRun, parse_linear, to_linear}; + +#[test] +fn an_empty_shown_limit_prints_back_the_same() { + for s in ["∑_", "∑^", "∫_0^▒x", "∑_()^n▒i"] { + let n = parse_linear(s); + assert_eq!(parse_linear(&to_linear(&n)), n, "{s} → {}", to_linear(&n)); + } +} + +#[test] +fn hidden_and_filled_limits_print_as_before() { + let nary = |sub_hide, sup_hide, sub: &str, sup: &str| { + let arg = |t: &str| if t.is_empty() { Vec::new() } else { vec![MNode::Run(MRun::new(t))] }; + vec![MNode::Nary { chr: '∑', lim_loc: None, grow: false, sub_hide, sup_hide, sub: arg(sub), sup: arg(sup), e: arg("x") }] + }; + assert_eq!(to_linear(&nary(true, true, "", "")), "∑▒x"); + assert_eq!(to_linear(&nary(false, false, "i", "n")), "∑_i^n▒x"); + assert_eq!(to_linear(&nary(false, true, "", "")), "∑_()▒x"); +}