Find and Replace Text: no panic searching a non-type active layer (#703) (#762)

With allLayers: false the search keeps only the active layer; when that
layer isn't a type layer the list is empty, and the find loop indexed
layers[0] on it (find, change and changeFind). Read the layer with
get() and stop when there is none, so the command finds nothing and
returns {"found": null}, as it does when nothing matches.

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
yangandClaude Opus 5.5 authored and GitHub committed 2026-10-07 16:42:29 -04:00
1 parent 6103da5278
commit fee9ff9613
2 files changed
+25 -1

No files matched your search

+2 -1
View File
@@ -730,7 +730,8 @@ fn find_replace(s: &mut Session, p: &Value) -> Result<Value> {
let cursor = s.edit_state.find_cursor;
let start = cursor.and_then(|(id, _, _)| layers.iter().position(|l| *l == id)).unwrap_or(0);
for k in 0..=layers.len() {
let id = layers[(start + k) % layers.len().max(1)];
// `allLayers: false` with a non-type layer active leaves no layers to search (#703).
let Some(&id) = layers.get((start + k) % layers.len().max(1)) else { break };
let Some(text) = text_of(&doc, id) else { continue };
let ms = find_matches(&text, &find, case, whole);
let hit = match (k, cursor) {
+23
View File
@@ -303,3 +303,26 @@ fn find_and_replace_across_type_layers() {
assert_ne!(f2["found"]["layer"], f2["changed"]["layer"]);
assert!(s.execute("edit.findAndReplaceText", json!({"find": ""})).is_err());
}
#[test]
fn find_in_the_active_layer_finds_nothing_when_it_is_not_type() {
// #703: `allLayers: false` with a raster layer active leaves nothing to search.
let mut s = session(8);
let id = s.execute("type.create", json!({"text": "abc def", "x": 2, "y": 12})).unwrap()["layer"].as_u64().unwrap();
s.execute("layer.new.layer", json!({"name": "raster"})).unwrap();
let steps = s.active().unwrap().history.entries().len();
for action in ["find", "change", "changeFind"] {
for forward in [true, false] {
let r =
s.execute("edit.findAndReplaceText", json!({"find": "abc", "replace": "x", "allLayers": false, "action": action, "forward": forward})).unwrap();
assert!(r["found"].is_null() && r["changed"].is_null(), "{action} {forward}: {r}");
}
}
let r = s.execute("edit.findAndReplaceText", json!({"find": "abc", "replace": "x", "allLayers": false})).unwrap();
assert_eq!(r["count"].as_u64(), Some(0));
assert_eq!(s.active().unwrap().history.entries().len(), steps);
// With the type layer active, the same search finds it.
s.execute("layer.select", json!({"layer": id})).unwrap();
let r = s.execute("edit.findAndReplaceText", json!({"find": "abc", "allLayers": false, "action": "find"})).unwrap();
assert_eq!((r["found"]["layer"].as_u64(), r["found"]["text"].as_str()), (Some(id), Some("abc")));
}