diff --git a/crates/engine/src/cmd/typemenu.rs b/crates/engine/src/cmd/typemenu.rs index 85bc20926a..f9c6471812 100644 --- a/crates/engine/src/cmd/typemenu.rs +++ b/crates/engine/src/cmd/typemenu.rs @@ -129,7 +129,7 @@ pub fn specs() -> Vec { "Find Next", ["Edit"], None, - "{find, matchCase?: false, wholeWord?: false, backwards?: false, hiddenLayers?: false, lockedLayers?: false, from?: {id, start, end} (a match this command returned, or the range edit.replaceMatch returned)} find the next match of `find` in the type edit.findReplace reaches and select its type object → {id, start, end (the match's bytes in the object's plain text), index (its place among all the matches, from 1), count, selected} or {id: null, count: 0}. The search visits type objects from the top of the stacking order down and each object's matches in reading order; backwards visits the objects from the bottom up and each object's matches from the last. It goes on after `from` and, past the last match, starts again at the first. Without `from` it goes on after the first selected type object, and with no type selected it starts at the first match. A match on a hidden or locked layer is returned with selected false and the selection stays as it is. Nothing is recorded for undo.", + "{find, matchCase?: false, wholeWord?: false, backwards?: false, hiddenLayers?: false, lockedLayers?: false, from?: {id, start, end} (a match this command returned, or the range edit.replaceMatch returned)} find the next match of `find` in the type edit.findReplace reaches and select its type object → {id, start, end (the match's bytes in the object's plain text), index (its place among all the matches, from 1), count, selected} or {id: null, count: 0}. The search visits type objects from the top of the stacking order down and each object's matches in reading order; backwards visits the objects from the bottom up and each object's matches from the last. It goes on after `from` and, past the last match, starts again at the first. Without `from` it goes on after the first selected type object, and with no type selected, or with a null `from`, it starts at the first match. A match on a hidden or locked layer is returned with selected false and the selection stays as it is. Nothing is recorded for undo.", has_doc, find_next ), @@ -1856,10 +1856,12 @@ fn find_next(s: &mut Session, p: &Value) -> Result { let q = Query::of(p, C)?; let backwards = switch_or(p, "backwards", false, C)?; let (hidden, locked) = (switch_or(p, "hiddenLayers", false, C)?, switch_or(p, "lockedLayers", false, C)?); - // A `from` that is no object gives no `id`, and is refused as one without it. + // A `from` that is no object gives no `id`, and is refused as one without it. A null `from` + // starts at the first match, whatever is selected. let from = match p.get("from") { - None | Some(Value::Null) => None, - Some(v) => Some(Match::of(v, C)?), + None => None, + Some(Value::Null) => Some(None), + Some(v) => Some(Some(Match::of(v, C)?)), }; let st = s.doc()?; // The objects in the order the search visits them: from the top of the stacking order down, @@ -1885,7 +1887,7 @@ fn find_next(s: &mut Session, p: &Value) -> Result { // The search goes on from `from`, or from the whole of the first selected type object it // visits, so that the same call again reaches the next object. From anywhere else it starts // at the first match. - let from = from.or_else(|| { + let from = from.unwrap_or_else(|| { st.selection.objects.iter().filter(|id| order.contains(id)).find_map(|id| match &st.doc.node(*id)?.kind { NodeKind::Text(t) => Some(Match { id: *id, start: 0, end: t.runs.iter().map(|r| r.text.len()).sum() }), _ => None, diff --git a/crates/engine/src/tests/menucmds.rs b/crates/engine/src/tests/menucmds.rs index dbbf9e803d..9c03205ee7 100644 --- a/crates/engine/src/tests/menucmds.rs +++ b/crates/engine/src/tests/menucmds.rs @@ -2476,6 +2476,10 @@ fn find_next_steps_through_the_matches_in_both_directions() { // A `from` in an object the search does not visit starts at the first match. let r = s.execute("edit.findNext", &json!({"find": "cat", "from": {"id": 999_999, "start": 0, "end": 0}})).unwrap(); assert_eq!(found(&r), (top.0, 2, 5, 1, 3)); + // So does a null `from`, with a type object selected. + sel(&mut s, &[top]); + let r = s.execute("edit.findNext", &json!({"find": "cat", "from": null})).unwrap(); + assert_eq!(found(&r), (top.0, 2, 5, 1, 3)); // Nothing found: no match, and the selection stays. let r = s.execute("edit.findNext", &json!({"find": "zebra"})).unwrap(); assert_eq!((r, selected(&s)), (json!({"id": null, "count": 0}), vec![top])); diff --git a/crates/ui-egui/src/dialogs/find_replace.rs b/crates/ui-egui/src/dialogs/find_replace.rs index f56d07c722..8e229980e6 100644 --- a/crates/ui-egui/src/dialogs/find_replace.rs +++ b/crates/ui-egui/src/dialogs/find_replace.rs @@ -204,9 +204,8 @@ fn remember(app: &mut VectorcraftApp, d: &Dialog) { fn find(app: &mut VectorcraftApp, d: &mut Dialog) -> Result { let mut p = query(d); p["backwards"] = json!(d.bool("backwards")); - if let Some(at) = place(app, d) { - p["from"] = at.clone(); - } + // With no place, the search starts at the first match, not after the selected type object. + p["from"] = place(app, d).cloned().unwrap_or(Value::Null); d.fields.remove(FOUND); d.fields.remove(NOTE); let r = app.run(NEXT, p)?; diff --git a/crates/ui-egui/src/dialogs/find_replace/tests.rs b/crates/ui-egui/src/dialogs/find_replace/tests.rs index 59dbe4937b..1f2bb756ab 100644 --- a/crates/ui-egui/src/dialogs/find_replace/tests.rs +++ b/crates/ui-egui/src/dialogs/find_replace/tests.rs @@ -195,6 +195,23 @@ fn an_action_of_the_wrong_type_runs_no_action() { assert_ne!(matched(&app), at, "a string action still runs"); } +/// Find with other Find text starts again at the first match, the matches in the type object the +/// last Find selected included. +#[test] +fn find_with_other_text_starts_at_the_first_match() { + let (mut app, ids) = app_with(&["dog", "cat dog"]); + let (bottom, top) = (ids[0], ids[1]); + choose(&mut app); + set(&mut app, "find", json!("cat")); + crate::dialogs::confirm(&mut app).unwrap(); + assert_eq!((matched(&app), selected(&app)), (Some((top, 0, 3)), vec![top])); + set(&mut app, "find", json!("dog")); + crate::dialogs::confirm(&mut app).unwrap(); + assert_eq!((matched(&app), selected(&app)), (Some((top, 4, 7)), vec![top])); + crate::dialogs::confirm(&mut app).unwrap(); + assert_eq!(matched(&app), Some((bottom, 0, 3))); +} + /// The dialog stays open from match to match: Find goes to the next match and selects its /// object, Replace changes that match, Replace & Find changes it and goes on, Replace All changes /// the rest, and Done closes. Each Replace is one undo step, and so is Replace All; finding and a diff --git a/docs/mcp.md b/docs/mcp.md index accd19856e..1b0bcf1ef0 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9969,8 +9969,10 @@ recorded for undo. - These rules are VectorCraft's own. Inside a type object the matches come in reading order, or from the last with `backwards`. `from: {id, start, end}`, a match the command returned or the range `edit.replaceMatch` returned, is where the search goes on from; past the last match it starts again at the first. Without `from` it goes on after the - first selected type object, and with no type selected it starts at the first match. A match on a hidden or locked - layer comes back with `selected: false` and the selection stays as it is. A match stays inside one style run. + first selected type object, and with no type selected, or with `from: null`, it starts at the first match. The Find + and Replace dialog sends `from: null` when it has no place to go on from, as when its Find text or a switch changed + since its last match. A match on a hidden or locked layer comes back with `selected: false` and the selection stays + as it is. A match stays inside one style run. `edit.replaceMatch {find, replace?, matchCase?, wholeWord?, id, start, end}` replaces that one match with `replace` as one undo step and returns `{id, start, end, replaced}`, with `start` and `end` the replacement's bytes. Bytes that