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

* claim: #2757

* fix(params): autocomplete offers a parameter for any part of its name (#2757)

Typing `side` now offers `cab_side_thickness`: parameters match the word
anywhere in their name, after those that start with it, both in the expression
boxes and in parameters.complete. Functions, constants and units still match
from the start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-20 20:39:14 +00:00
1 parent 1e0e8f78dc
commit 7dfbaf2a2c
3 files changed
+52 -9

No files matched your search

+9 -4
View File
@@ -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);
+21
View File
@@ -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();
+22 -5
View File
@@ -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]