Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Parameter autocomplete matches anywhere in the name, prefix matches first (#2757) (#2849)

DuBearte7dfbaf2a2c3f
diff --git a/crates/engine/src/cmd/parameters.rs b/crates/engine/src/cmd/parameters.rs
index c15b57aca2..7808db7498 100644
--- a/crates/engine/src/cmd/parameters.rs
+++ b/crates/engine/src/cmd/parameters.rs
@@ -348,11 +348,16 @@ fn complete(s: &mut Session, p: &Value) -> Result<Value> {
let text = str_(p, "prefix").unwrap_or("");
// The identifier being typed: trailing letters, digits and underscores.
let word: String = text.chars().rev().take_while(|c| c.is_alphanumeric() || *c == '_').collect::<Vec<_>>().into_iter().rev().collect();
- let starts = |n: &str| word.is_empty() || n.to_ascii_lowercase().starts_with(&word.to_ascii_lowercase());
+ let lower = word.to_ascii_lowercase();
+ let starts = |n: &str| word.is_empty() || n.to_ascii_lowercase().starts_with(&lower);
+ // A parameter matches wherever the word appears in its name (`side` finds
+ // `cab_side_thickness`, #2757); functions, constants and units match from the start.
+ let within = |n: &str| word.is_empty() || n.to_ascii_lowercase().contains(&lower);
let mut out: Vec<Value> = Vec::new();
- let mut rows: Vec<solidcraft_doc::ParamRow> = s.doc.param_rows().into_iter().filter(|r| starts(&r.name)).collect();
- // Favourites, then user parameters, then the rest.
- rows.sort_by_key(|r| (!r.favorite, r.source != "user"));
+ let mut rows: Vec<solidcraft_doc::ParamRow> = s.doc.param_rows().into_iter().filter(|r| within(&r.name)).collect();
+ // Names that start with the word, then the rest; within each, favourites, then user
+ // parameters, then the others.
+ rows.sort_by_key(|r| (!starts(&r.name), !r.favorite, r.source != "user"));
for r in rows.into_iter().take(200) {
let v = r.value.map(|v| format!("{v:.4} {}", r.unit).trim().to_string()).unwrap_or_else(|| "error".into());
let mut detail = format!("{} = {v}", r.expression);
diff --git a/crates/engine/src/cmd/parameters_tests.rs b/crates/engine/src/cmd/parameters_tests.rs
index 422b3cfd6b..27bc318f98 100644
--- a/crates/engine/src/cmd/parameters_tests.rs
+++ b/crates/engine/src/cmd/parameters_tests.rs
@@ -212,6 +212,27 @@ fn favorites_comments_and_completion() {
assert!(c["suggestions"].as_array().unwrap().iter().any(|x| x["text"] == "atan2("), "{c}");
}
+/// #2757: a parameter is suggested for a word anywhere in its name, not only at its start;
+/// names that start with the word come first. Functions still match from the start.
+#[test]
+fn completion_matches_parameters_anywhere_in_their_name() {
+ let mut s = Session::default();
+ for (name, e) in [("cab_side_thickness", "18 mm"), ("side", "40 mm"), ("door_width", "300 mm")] {
+ run(&mut s, "parameters.add", json!({"name": name, "expression": e}));
+ }
+ let texts = |c: &Value| -> Vec<String> {
+ c["suggestions"].as_array().unwrap().iter().filter(|x| x["kind"] == "parameter").map(|x| x["text"].as_str().unwrap().to_string()).collect()
+ };
+ let c = run(&mut s, "parameters.complete", json!({"prefix": "2 * side"}));
+ assert_eq!(texts(&c), ["side", "cab_side_thickness"], "the name starting with it first: {c}");
+ let c = run(&mut s, "parameters.complete", json!({"prefix": "THICK"}));
+ assert_eq!(texts(&c), ["cab_side_thickness"], "any case: {c}");
+ assert!(texts(&run(&mut s, "parameters.complete", json!({"prefix": "xyz"}))).is_empty());
+ // Functions keep matching from the start: `in` is not `sin(`.
+ let c = run(&mut s, "parameters.complete", json!({"prefix": "in"}));
+ assert!(!c["suggestions"].as_array().unwrap().iter().any(|x| x["text"] == "sin("), "{c}");
+}
+
#[test]
fn csv_and_json_round_trip() {
let mut s = Session::default();
diff --git a/crates/ui-egui/src/params_dialog.rs b/crates/ui-egui/src/params_dialog.rs
index 654792d661..dd82d0d678 100644
--- a/crates/ui-egui/src/params_dialog.rs
+++ b/crates/ui-egui/src/params_dialog.rs
@@ -634,11 +634,12 @@ pub(crate) fn suggestions_for(ctx: &egui::Context, word: &str, explicit: bool) -
return Vec::new();
}
let w = word.to_ascii_lowercase();
- all.iter()
- .filter(|s| s.text.to_ascii_lowercase().starts_with(&w) && (explicit || s.text.trim_end_matches('(') != word))
- .take(8)
- .cloned()
- .collect()
+ let offered = |s: &&Suggestion| explicit || s.text.trim_end_matches('(') != word;
+ let starts = |s: &&Suggestion| s.text.to_ascii_lowercase().starts_with(&w);
+ // A parameter is also offered for a word anywhere in its name (`side` finds
+ // `cab_side_thickness`, #2757), after the names that start with it.
+ let within = |s: &&Suggestion| s.kind == "parameter" && !starts(s) && s.text.to_ascii_lowercase().contains(&w);
+ all.iter().filter(|s| starts(s) && offered(s)).chain(all.iter().filter(|s| within(s) && offered(s))).take(8).cloned().collect()
}
/// Offer keyboard and pointer completions under a focused expression box.
@@ -672,6 +673,22 @@ mod tests {
assert!(check(&mut app, "width + 1 deg", "mm").is_err());
}
+ /// #2757: typing part of a parameter's name offers it, not only its start; names that start
+ /// with the word come first, and functions still match from the start.
+ #[test]
+ fn a_parameter_is_offered_for_any_part_of_its_name() {
+ let ctx = egui::Context::default();
+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), Default::default());
+ for (name, e) in [("cab_side_thickness", "18 mm"), ("sides", "4")] {
+ let _ = app.run("parameters.add", json!({"name": name, "expression": e}));
+ }
+ publish(&mut app, &ctx);
+ let texts = |w: &str| suggestions(&ctx, w).into_iter().map(|s| s.text).collect::<Vec<_>>();
+ assert_eq!(texts("side"), ["sides", "cab_side_thickness"]);
+ assert_eq!(texts("Thick"), ["cab_side_thickness"]);
+ assert!(!texts("in").iter().any(|t| t == "sin("), "functions match from the start");
+ }
+
/// A favourite is listed twice, under Favorites and in its own table; typing into either of its
/// expression boxes enters each key once (#1767: `1.1` came out `1.11.1`).
#[test]