Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Find/Change: first:true changes only the first match, not one per story

aionggf0f9a4619c33
diff --git a/crates/engine/src/cmd/find.rs b/crates/engine/src/cmd/find.rs
index e97df64fc..465dd6d0c 100644
--- a/crates/engine/src/cmd/find.rs
+++ b/crates/engine/src/cmd/find.rs
@@ -188,6 +188,10 @@ fn change(s: &mut Session, p: &Value) -> Result<Value> {
t.anchor = t.anchor.min(len);
t.focus = t.focus.min(len);
}
+ // `first` is the first match of the whole search, not of each story.
+ if first_only && count > 0 {
+ break;
+ }
}
Ok(json!({"count": count}))
})
@@ -251,6 +255,21 @@ mod tests {
assert!(s.doc().unwrap().selection.text.is_some());
}
+ #[test]
+ fn change_first_changes_one_match_in_the_whole_document() {
+ for grep in [false, true] {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ for y in [36, 336] {
+ s.execute("frame.create", &json!({"rect": [36, y, 300, y + 200], "content": "text", "text": "red and red"})).unwrap();
+ }
+ let r = s.execute("find.change", &json!({"find": "red", "change": "blue", "first": true, "grep": grep})).unwrap();
+ assert_eq!(r["count"], 1);
+ assert_eq!(s.execute("find.find", &json!({"find": "blue"})).unwrap().as_array().unwrap().len(), 1);
+ assert_eq!(s.execute("find.find", &json!({"find": "red"})).unwrap().as_array().unwrap().len(), 3);
+ }
+ }
+
#[test]
fn grep_in_the_documents_dialect() {
let mut s = Session::new();