Merge pull request #9614 from dotCooCoo/fix/find-replace-matches-after-revert

Forget the previous text match after Revert
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-26 21:33:27 -08:00
commit e43fb18f65
2 files changed
+99 -15

No files matched your search

+30 -15
View File
@@ -14,10 +14,9 @@
//! closed, and opens the dialog when there has been none.
//!
//! The walk lives in `UiState::last_find`: the Find text and the switches of the last request, its
//! `replace` text and `backwards`, the place it reached in `from` (`{id, start, end}`), the `uid` of
//! that document in `doc` and the match found in `found` (`{index, count}`). A place in a document
//! other than the active one, or reached with other Find text or switches, is not used: the search
//! starts again. [`pump`] runs the dialog's requests once per frame.
//! `replace` text and `backwards`, the place it reached in `from` (`{id, start, end}`), the drawing's
//! UID and Revert counter in `doc`, and the match in `found` (`{index, count}`). Changing the
//! loaded drawing, Find text, or switches restarts the search. [`pump`] runs the requests each frame.
use craft::text::find::FindOptions;
use craft::text::ui::find::{FindCheck, FindReplaceDialog, FindRequest, FindTab, ReplaceButtons};
@@ -56,9 +55,18 @@ pub fn menu_params() -> Value {
json!({"find": null})
}
/// The `uid` of the active document.
fn active_uid(app: &VectorcraftApp) -> Option<u64> {
app.session.active().map(|d| d.uid)
/// The active drawing and its loaded generation.
fn active_drawing(app: &VectorcraftApp) -> Option<(u64, u64)> {
app.session.active().map(|d| (d.uid, d.reverts))
}
fn saved_drawing(last: &Map<String, Value>) -> Option<(u64, u64)> {
let [uid, reverts] = last.get("doc")?.as_array()?.as_slice() else { return None };
Some((uid.as_u64()?, reverts.as_u64()?))
}
fn same_drawing(last: &Map<String, Value>, drawing: (u64, u64)) -> bool {
saved_drawing(last) == Some(drawing)
}
/// The match found belongs to an open dialog: the last search keeps its place, not the match.
@@ -127,13 +135,20 @@ pub fn find_next(app: &mut VectorcraftApp) -> Result<Value, String> {
return open(app, &Map::new());
};
forget_found(app);
let uid = active_uid(app);
let drawing = active_drawing(app);
if let Some(o) = last.as_object_mut() {
let saved = saved_drawing(o);
o.remove("replace");
o.remove("found");
// A place in another document is no place in this one.
if uid.is_none() || o.remove("doc").and_then(|d| d.as_u64()) != uid {
o.remove("from");
o.remove("doc");
if saved != drawing || drawing.is_none() {
let restored = saved.zip(drawing).is_some_and(|((old_uid, _), (uid, _))| old_uid == uid);
if restored {
// An explicit start ignores the whole text frame currently selected.
o.insert("from".into(), Value::Null);
} else {
o.remove("from");
}
}
}
let r = app.run(NEXT, last)?;
@@ -143,7 +158,7 @@ pub fn find_next(app: &mut VectorcraftApp) -> Result<Value, String> {
}
if let Some(l) = app.ui.last_find.as_mut() {
l["from"] = json!({ "id": r["id"], "start": r["start"], "end": r["end"] });
l["doc"] = json!(uid);
l["doc"] = json!(drawing);
}
Ok(r)
}
@@ -178,9 +193,9 @@ fn form(d: &FindReplaceDialog) -> Form {
/// The place the last search reached, `{id, start, end}`, while its document is the active one and
/// the Find text and the switches are `query`, those it was reached with.
fn place<'a>(app: &VectorcraftApp, last: &'a Value, query: &Value) -> Option<&'a Value> {
let uid = active_uid(app)?;
let drawing = active_drawing(app)?;
let last = last.as_object()?;
let same = last.get("doc").and_then(Value::as_u64) == Some(uid) && query_in(last) == *query;
let same = same_drawing(last, drawing) && query_in(last) == *query;
same.then(|| last.get("from")).flatten().filter(|at| at.is_object())
}
@@ -204,7 +219,7 @@ fn remember(app: &mut VectorcraftApp, form: &Form, at: Option<Value>, found: Opt
last["backwards"] = json!(form.backwards);
if let Some(at) = at {
last["from"] = at;
last["doc"] = json!(active_uid(app));
last["doc"] = json!(active_drawing(app));
}
if let Some(found) = found {
last["found"] = found;
@@ -97,6 +97,48 @@ fn selected(p: &Panel) -> Vec<u64> {
p.app().session.active().unwrap().selection.objects.iter().map(|id| id.0).collect()
}
#[test]
fn a_cached_match_cannot_replace_text_restored_by_revert() {
let (mut app, ids) = app_with(&["Needle Needle"]);
let id = ids[0];
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string())));
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string())));
let dir = vectorcraft_testkit::TempDir::new("find-replace-reverted-match");
app.run("file.saveAs", json!({"path": dir.join("text.vectorcraft")})).unwrap();
app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 10, "height": 10})).unwrap();
app.run("select.none", json!({})).unwrap();
let st = app.session.active().unwrap();
let version = (st.uid, st.reverts);
let mut p = Panel::new(app, draw);
choose(&p);
set(&p, "find", json!("Needle"));
set(&p, "replace", json!("Changed"));
p.click("Find Next");
assert_eq!(matched(&p), Some((id, 0, 6)));
p.app().run("file.revert", json!({"confirmed": true})).unwrap();
{
let app = p.app();
let st = app.session.active().unwrap();
assert_eq!(st.uid, version.0);
assert_ne!(st.reverts, version.1);
}
let before = undo_steps(&p);
let form = with_dialog(&p, |d| super::form(d));
// A Replace action dispatched before the dialog has refreshed must still check the match.
super::replace(&mut p.app(), &form, false).unwrap();
assert_eq!(plain(&p, id), "Needle Needle");
assert_eq!(undo_steps(&p), before);
pump(&p);
assert_eq!(matched(&p), None);
assert!(message(&p).is_empty());
p.click("Find Next");
assert_eq!(matched(&p), Some((id, 0, 6)));
p.click("Replace");
assert_eq!(plain(&p, id), "Changed Needle");
}
/// The Edit menu's rows for Find and Replace, Check Spelling (in its Spelling submenu) and Edit Custom Dictionary
/// are enabled, and invoking each puts that dialog on the host.
#[test]
@@ -374,3 +416,30 @@ fn a_search_place_stays_in_its_document_and_given_params_win() {
choose(&p);
assert_eq!((field(&p, "find"), field(&p, "matchCase")), (json!("cat"), json!(true)));
}
#[test]
fn edit_find_next_starts_at_the_first_restored_match_with_that_frame_selected() {
let (mut app, ids) = app_with(&["cat in the back", "cat cat in front"]);
let first = ids[1];
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string())));
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string())));
let dir = vectorcraft_testkit::TempDir::new("menu-find-next-revert");
app.run("file.saveAs", json!({"path": dir.join("text.vectorcraft")})).unwrap();
app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 10, "height": 10})).unwrap();
let mut p = Panel::new(app, draw);
choose(&p);
set(&p, "find", json!("cat"));
p.click("Find Next");
assert_eq!(matched(&p), Some((first, 0, 3)));
p.click("Find Next");
assert_eq!(matched(&p), Some((first, 4, 7)));
p.click("Close");
p.app().run("file.revert", json!({"confirmed": true})).unwrap();
p.app().run("select.set", json!({"ids": [first]})).unwrap();
let before = undo_steps(&p);
choose_find_next(&p);
assert_eq!(from(&p), json!({"id": first, "start": 0, "end": 3}));
assert_eq!(selected(&p), vec![first]);
assert_eq!(undo_steps(&p), before);
}