Find and Replace: Find with other Find text starts at the first match

With no place to go on from, the dialog left out edit.findNext's from, so the search went on after the selected type object and passed over its matches: after Find selected a type object, changing the Find text skipped the matches in it. edit.findNext now starts at the first match for a null from, and the dialog sends one.
This commit is contained in:
dotCooCoo
2026-11-18 01:46:57 -08:00
parent d27a9e9466
commit efc36481b1
5 changed files with 34 additions and 10 deletions
+7 -5
View File
@@ -129,7 +129,7 @@ pub fn specs() -> Vec<CommandSpec> {
"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<Value> {
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<Value> {
// 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,
+4
View File
@@ -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]));
+2 -3
View File
@@ -204,9 +204,8 @@ fn remember(app: &mut VectorcraftApp, d: &Dialog) {
fn find(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<Value, String> {
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)?;
@@ -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
+4 -2
View File
@@ -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