SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Expressions: Enter after `pi mm` keeps the unit, and the list's state follows the text (#8862) (#8871)
austin62jakec32f6b60eb4e
diff --git a/crates/ui-egui/src/expression_complete.rs b/crates/ui-egui/src/expression_complete.rsindex 7be658b0d9..b759b93611 100644--- a/crates/ui-egui/src/expression_complete.rs+++ b/crates/ui-egui/src/expression_complete.rs@@ -22,10 +22,15 @@ struct Popup {/// A row is selected, so Enter and Tab complete it. A popup opened on a word that is/// itself a complete name (`width`) starts unarmed: it only shows the longer names, Enter/// and Tab reach the editor as if it were not there, and an arrow key arms it (#3663). A- /// unit means something only after a number, where no popup opens, so a unit typed anywhere- /// else (`rad` on the way to `radius`) is a prefix like any other and the popup is armed.+ /// unit means something only after a value: after a number no popup opens, after a name or+ /// constant (`pi mm`) it is a complete name as above (#8862). A unit typed anywhere else+ /// (`rad` on the way to `radius`) is a prefix like any other and the popup is armed./// A press on a row inserts it whether or not the list is armed.armed: bool,+ /// A key armed the list (an arrow, or Ctrl+Space). Otherwise `armed` is worked out again+ /// every frame: the names it depends on are gathered only once a box has the focus, and+ /// the text before the word can change under it (`20 mm` replaced by `pi mm`).+ keyed: bool,}fn popup_id() -> egui::Id {@@ -45,7 +50,7 @@ pub(crate) fn begin_frame(ctx: &egui::Context) {});popup.explicit = true;popup.dismissed = false;- popup.armed = true;+ (popup.armed, popup.keyed) = (true, true);ctx.data_mut(|d| d.insert_temp(popup_id(), popup.clone()));}let escape_blurred = ctx.input(|i| i.key_pressed(Key::Escape)) && ctx.memory(|m| m.focused().is_none() && m.had_focus_last_frame(popup.editor));@@ -73,8 +78,8 @@ pub(crate) fn begin_frame(ctx: &egui::Context) {&& *modifiers == egui::Modifiers::NONE{match key {- Key::ArrowDown if !popup.armed => (popup.armed, popup.selected) = (true, 0),- Key::ArrowUp if !popup.armed => (popup.armed, popup.selected) = (true, popup.count - 1),+ Key::ArrowDown if !popup.armed => (popup.armed, popup.keyed, popup.selected) = (true, true, 0),+ Key::ArrowUp if !popup.armed => (popup.armed, popup.keyed, popup.selected) = (true, true, popup.count - 1),Key::ArrowDown => popup.selected = (popup.selected + 1) % popup.count,Key::ArrowUp => popup.selected = (popup.selected + popup.count - 1) % popup.count,// Unarmed, the typed word is complete: Enter submits it and Tab moves on.@@ -140,9 +145,13 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Stri// Dismissed suggestions stay closed until this box is typed in: another control rewriting// its text (a slider dragged while the box keeps the focus) or a moved caret is no reason.let dismissed = previous.as_ref().is_some_and(|p| p.dismissed) && !resp.changed();- // A word that is already a complete name opens unarmed: Enter submits it as typed. A unit- // without a number before it (`rad`, half-way to `radius`) is a prefix, so it stays armed.- let armed = !exact_token(&ctx, &word) || unit_token(&ctx, &word);+ // What the word follows, blanks aside.+ let before = text.chars().take(start).filter(|c| !c.is_whitespace()).last();+ // A word that is already a complete name opens unarmed: Enter submits it as typed. A unit is+ // a unit after a value it multiplies, a name or constant as much as a number (`pi mm`, #8862);+ // anywhere else (`rad`, half-way to `radius`) it is a prefix, so it stays armed.+ let after_value = before.is_some_and(|c| c.is_alphanumeric() || matches!(c, '_' | '.' | ')'));+ let armed = !exact_token(&ctx, &word) || (unit_token(&ctx, &word) && !after_value);let mut popup = previous.filter(|p| p.word == word && p.cursor == cursor).unwrap_or(Popup {editor: resp.id,frame: 0,@@ -155,14 +164,16 @@ pub(crate) fn complete(ui: &mut egui::Ui, resp: &egui::Response, text: &mut Striexplicit: false,rows: Vec::new(),armed,+ keyed: false,});+ popup.armed = popup.keyed || armed;// Never over a control being dragged: the drag dismisses them.if ctx.dragged_id().is_some_and(|id| id != resp.id) {popup.dismissed = true;}// A unit right after a number (`2 in`, `10 mm`, the form every dialog starts its sizes in)// is a unit, not the start of a name: no popup there, so nothing sits over the OK button.- let number_before = text.chars().take(start).filter(|c| !c.is_whitespace()).last().is_some_and(|c| c.is_ascii_digit() || c == '.' || c == ')');+ let number_before = before.is_some_and(|c| c.is_ascii_digit() || c == '.' || c == ')');let list = if !popup.explicit && number_before && unit_token(&ctx, &popup.word) {Vec::new()} else {@@ -546,6 +557,50 @@ mod tests {assert_eq!(e.text, "2 * mm_wall");}+ /// A unit after a name or constant multiplies it, as after a number: `pi mm` is π mm and+ /// `pi rad` half a turn. The longer names are listed unarmed, so Enter submits what was+ /// typed, and Down then Enter still completes `mm_wall` or `radius` (#8862).+ #[test]+ fn a_unit_after_a_name_submits_as_typed() {+ for (text, longer) in [("pi mm", "pi mm_wall"), ("pi rad", "pi radius")] {+ for arrow in [false, true] {+ let mut e = Editor::new(text);+ e.app.run("parameters.add", json!({"name":"mm_wall","expression":"3 mm"})).unwrap();+ e.frame(Vec::new());+ assert_eq!(e.popup(), Some((1, false)), "{text}");+ if arrow {+ e.key(Key::ArrowDown);+ }+ e.key(Key::Enter);+ if arrow {+ assert_eq!(e.text, longer);+ assert!(e.accepted && e.leaked.is_empty(), "{text}");+ } else {+ assert_eq!(e.text, text);+ assert!(!e.accepted && e.leaked.contains(&Key::Enter), "{text}");+ }+ }+ }+ }++ /// Whether Enter completes follows the text as it is now, also when the word and the caret+ /// stay where they were: `pi*mm` replaced by `pi mm` in one go (a paste over the selected+ /// text) leaves `mm` at the same place, and Enter must then submit `pi mm` (#8862).+ #[test]+ fn replacing_the_text_before_a_unit_rearms_the_list() {+ let mut e = Editor::new("pi*mm");+ e.app.run("parameters.add", json!({"name":"mm_wall","expression":"3 mm"})).unwrap();+ e.frame(Vec::new());+ assert_eq!(e.popup(), Some((1, true)), "after `*` a unit is a prefix");+ e.frame(vec![egui::Event::Key { key: Key::A, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND }]);+ e.frame(vec![egui::Event::Text("pi mm".into())]);+ assert_eq!(e.cursor(), 5);+ assert_eq!(e.popup(), Some((1, false)));+ e.key(Key::Enter);+ assert_eq!(e.text, "pi mm");+ assert!(!e.accepted && e.leaked.contains(&Key::Enter));+ }+/// Typing a name letter by letter passes through words that happen to be units: `ra` lists/// `radius` selected, and `rad` must keep it selected, so Enter completes the name instead/// of submitting one radian into a length (found testing #3663).