Accept a leading = in expressions, so a fillet radius can be =d3 (#4681) (#4686)

* Sketch fillet radius: accept a leading = in an expression (#4681)

* A second = after a name is still not a definition
This commit is contained in:
Steve Jason
2026-10-23 13:44:48 +00:00
committed by GitHub
parent a147a7c083
commit a9c3c8bb8a
2 changed files with 32 additions and 4 deletions
+10 -4
View File
@@ -341,6 +341,12 @@ fn lex_spans(s: &str) -> Result<Vec<(Tok, usize, usize)>, DocError> {
let mut out = Vec::new();
// Parentheses open at this point: a comma outside them can only be a decimal comma.
let mut depth = 0usize;
// A single leading `=` marks the text as a formula (`=d3`, #4681) and is not part of it.
if let Some(k) = cs.iter().position(|(_, c)| !c.is_whitespace())
&& cs.get(k).is_some_and(|(_, c)| *c == '=')
{
i = k + 1;
}
while let Some(&(_, c)) = cs.get(i) {
if c.is_whitespace() {
i += 1;
@@ -564,7 +570,9 @@ fn is_name_shaped(s: &str) -> bool {
pub fn definition(s: &str) -> Option<(&str, &str)> {
let (name, rhs) = s.split_once('=')?;
let name = name.trim();
is_name_shaped(name).then(|| (name, rhs.trim()))
let rhs = rhs.trim();
// `w == 3` is not a definition: a second `=` would read as a formula marker (#4681).
(is_name_shaped(name) && !rhs.starts_with('=')).then_some((name, rhs))
}
/// The length unit of a design with these units: `mm` unless they name another length unit.
@@ -1252,8 +1260,6 @@ mod tests {
("szerokość = 20", "szerokość", "20"),
// Reserved names split too: the parameter check names the reason.
("mm = 5", "mm", "5"),
// A second `=` stays in the expression, which then fails to evaluate.
("w == 3", "w", "= 3"),
("w = a = 3", "w", "a = 3"),
// An empty expression is the caller's error to report.
("w =", "w", ""),
@@ -1262,7 +1268,7 @@ mod tests {
for (text, name, rhs) in splits {
assert_eq!(definition(text), Some((name, rhs)), "{text:?}");
}
for text in ["20", "20 mm", "width / 2", "= 20", "=", "", "2w = 3", "a b = 3", "a-b = 3", "w.x = 3", "(w) = 3", "-w = 3"] {
for text in ["20", "20 mm", "width / 2", "= 20", "=", "", "2w = 3", "a b = 3", "a-b = 3", "w.x = 3", "(w) = 3", "-w = 3", "w == 3"] {
assert_eq!(definition(text), None, "{text:?}");
}
}
@@ -1871,3 +1871,25 @@ fn fillet_refuses_an_offsets_corners_until_its_association_is_broken() {
assert!((sk.radius(ai).unwrap() - 2.0).abs() < 1e-7);
}
}
/// #4681: a fillet radius typed as `=name` (a leading equals sign, as in a spreadsheet) is the
/// expression after the sign, so it can follow a parameter.
#[test]
fn fillet_radius_accepts_a_leading_equals_sign() {
let mut s = new_sketch();
run(&mut s, "sketch.line", json!({"points": [[0, 0], [50, 0], [50, 30], [0, 30]], "closed": true, "ids": ["a", "b", "c", "d"]}));
let first = run(&mut s, "sketch.fillet", json!({"point": "a.end", "radius": 4}));
let pname = first["param"].as_str().unwrap().to_string();
let second = run(&mut s, "sketch.fillet", json!({"point": "b.end", "radius": format!("={pname}")}));
let sk = sketch(&s);
let arc = ids(&second["curves"])[0].clone();
let ai = sk.curve_index(&arc).unwrap();
assert!((sk.radius(ai).unwrap() - 4.0).abs() < 1e-7);
// Changing the parameter moves both fillets.
run(&mut s, "parameters.change", json!({"name": pname, "expression": "6 mm"}));
let sk = sketch(&s);
let ai = sk.curve_index(&arc).unwrap();
assert!((sk.radius(ai).unwrap() - 6.0).abs() < 1e-7);
let plain = run(&mut s, "sketch.fillet", json!({"point": "c.end", "radius": "= 2 mm"}));
assert!(plain["curves"].is_array());
}