Clear: remove a compound path emptied through an emptied inner one

Clear and the scripting tracing guard checked only the direct compound
parents of the removed objects, once each. A compound path whose
members were a removed path and an inner compound path the same clear
emptied stayed behind empty, or went, depending on the order of the
ids, and the guard could miss it.

- emptied_compounds walks up through compound ancestors until nothing
  changes and returns the emptied compound paths sorted by id; Clear
  and clearing_makes_tracing both use it.
- Clear with direct-selected anchors checks the compound path above
  each one it removes again.
This commit is contained in:
dotCooCoo committed 2026-12-04 02:11:55 -08:00
1 parent 2accb21996
commit cccd31c3aa
5 files changed
+90 -20

No files matched your search

+1 -1
View File
@@ -885,7 +885,7 @@ pub use colorcmds::{Backdrop, contrast_measure, contrast_pick_backdrop};
/// write a resolution and a number of bytes.
pub use docinfo::{CATEGORIES, OBJECT_LABELS, byte_size, ppi_number};
pub use docmenu::guide_indexes;
pub(crate) use edit::{Unrecorded, artboard_tool_picks, settle_interaction, typing_in_progress};
pub(crate) use edit::{Unrecorded, artboard_tool_picks, emptied_compounds, settle_interaction, typing_in_progress};
pub(crate) use object::bounds_rewrite;
pub(crate) use object::{keep_aligned_in, refresh_pixel_aligned};
/// The rules `object.scale`, `object.reflect` and `object.shear` check their params with.
+26 -10
View File
@@ -1363,17 +1363,12 @@ fn clear(s: &mut Session, p: &Value) -> Result<Value> {
}
s.edit("Clear", |d, sel| {
let clipped = clipped_ancestors(d, ids.iter().copied());
let parents: Vec<_> = if keep_empty {
Vec::new()
} else {
ids.iter().filter_map(|id| d.parent_of(*id).filter(|p| matches!(d.node(*p).map(|n| &n.kind), Some(NodeKind::Compound { .. })))).collect()
};
let emptied = if keep_empty { Vec::new() } else { emptied_compounds(d, &ids) };
// A compound path inside another emptied one leaves with it.
let outer: Vec<NodeId> =
emptied.iter().copied().filter(|p| !d.ancestry(*p).unwrap_or_default().iter().rev().skip(1).any(|a| emptied.binary_search(a).is_ok())).collect();
d.remove_many(&ids)?;
for p in parents {
if d.node(p).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())) {
d.remove(p)?;
}
}
d.remove_many(&outer)?;
if with_guides {
let gone = sel.guides.clone();
d.retain_guides(sel, |g| !g.id.is_some_and(|id| gone.contains(&id)));
@@ -1390,6 +1385,27 @@ fn clear(s: &mut Session, p: &Value) -> Result<Value> {
ok()
}
/// The compound paths that deleting the objects `ids` together leaves with no members, sorted by
/// id: a compound path counts when each of its members is deleted or is such a compound path, so
/// a compound path holding only an emptied inner one is emptied too.
pub(crate) fn emptied_compounds(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {
let mut gone: BTreeSet<NodeId> = ids.iter().copied().collect();
let mut emptied = BTreeSet::new();
loop {
let parents: BTreeSet<NodeId> = gone.iter().filter_map(|i| d.parent_of(*i)).filter(|p| !gone.contains(p)).collect();
let found: BTreeSet<NodeId> = parents
.into_iter()
.filter(|p| d.node(*p).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. })))
.filter(|p| d.children(Some(*p)).is_some_and(|k| k.iter().all(|n| gone.contains(&n.id))))
.collect();
if found.is_empty() {
return emptied.into_iter().collect();
}
gone.extend(found.iter().copied());
emptied.extend(found);
}
}
/// Does Clear remove direct-selected anchors without opening their paths (`keepJoined` param, else
/// General › Delete Removes Anchor Points and Keeps Paths Joined)? Noted in the journal, so a
/// replay removes them the same way whatever the preference is at the time. A saved step without `keepJoined`
+56
View File
@@ -400,3 +400,59 @@ fn selected_roots_are_found_once_for_each_document_and_selection() {
selected_roots(&s).unwrap();
assert_eq!(roots_found() - before, 2, "an edit finds the roots once again");
}
/// Clear removes a compound path whose members are cleared paths and a compound path the clear
/// empties, whichever order the ids come in, and the tracing guard's set counts both as deleted.
#[test]
fn clear_removes_a_compound_path_emptied_through_an_emptied_inner_one() {
for reversed in [false, true] {
let mut s = new_doc();
let outer_paths = [(); 2].map(|()| rect(&mut s));
let (outer, outer_members) = compound(&mut s, &outer_paths);
let inner_paths = [(); 2].map(|()| rect(&mut s));
let (inner, inner_members) = compound(&mut s, &inner_paths);
s.edit("Nest", |d, _| {
let taken = d.remove_many(&[inner])?;
d.insert(Some(outer), 1, (*taken[0]).clone())?;
Ok(())
})
.unwrap();
let mut ids: Vec<NodeId> = outer_members.iter().chain(&inner_members).copied().collect();
if reversed {
ids.reverse();
}
let d = &s.doc().unwrap().doc;
assert_eq!(d.parent_of(inner), Some(outer), "the inner compound path is a member of the outer one");
let want: Vec<NodeId> = BTreeSet::from([outer, inner]).into_iter().collect();
assert_eq!(emptied_compounds(d, &ids), want, "reversed: {reversed}");
run(&mut s, "edit.clear", json!({"ids": ids.iter().map(|id| id.0).collect::<Vec<_>>()}));
let d = &s.doc().unwrap().doc;
assert!(d.node(inner).is_none(), "the emptied inner compound path goes (reversed: {reversed})");
assert!(d.node(outer).is_none(), "the outer compound path it leaves empty goes too (reversed: {reversed})");
}
}
/// With a direct-selected anchor of another path in the selection, Clear still removes the outer
/// compound path that an emptied inner one leaves empty.
#[test]
fn clear_with_direct_anchors_removes_a_compound_path_emptied_through_an_emptied_inner_one() {
let mut s = new_doc();
let outer_paths = [(); 2].map(|()| rect(&mut s));
let (outer, outer_members) = compound(&mut s, &outer_paths);
let inner_paths = [(); 2].map(|()| rect(&mut s));
let (inner, inner_members) = compound(&mut s, &inner_paths);
let other = rect(&mut s);
s.edit("Nest", |d, _| {
let taken = d.remove_many(&[inner])?;
d.insert(Some(outer), 1, (*taken[0]).clone())?;
Ok(())
})
.unwrap();
let whole: Vec<NodeId> = outer_members.iter().chain(&inner_members).copied().collect();
select_direct(&mut s, &whole, &[(other, two_anchors())]);
run(&mut s, "edit.clear", json!({}));
let d = &s.doc().unwrap().doc;
assert!(d.node(inner).is_none(), "the emptied inner compound path goes");
assert!(d.node(outer).is_none(), "the outer compound path it leaves empty goes too");
assert!(d.node(other).is_some(), "the path with anchors picked keeps its other sides");
}
+5 -1
View File
@@ -1998,9 +1998,13 @@ pub(crate) fn delete_anchors_joined(s: &mut Session, p: &Value, keep_joined: boo
}
}
emptied.finish(d)?;
for p in compounds {
// A compound path left empty by an inner one that went is checked again after it.
let mut compounds = compounds;
while let Some(p) = compounds.pop_first() {
if d.node(p).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())) {
let up = d.parent_of(p).filter(|u| matches!(d.node(*u).map(|n| &n.kind), Some(NodeKind::Compound { .. })));
d.remove(p)?;
compounds.extend(up);
}
}
super::edit::release_empty_clips(d, clipped);
+2 -8
View File
@@ -3055,16 +3055,10 @@ fn makes_tracing(d: &Document, parent: NodeId, id: NodeId, at: Option<usize>) ->
/// Whether deleting the objects `ids` together would leave an image first in an ordinary group
/// named as a tracing, which would make that group one. `edit.clear` also deletes a compound path
/// it leaves empty, so such a compound path counts as deleted.
/// it leaves empty, nested ones included, so such a compound path counts as deleted.
pub(super) fn clearing_makes_tracing(d: &Document, ids: &[NodeId]) -> bool {
let mut gone: HashSet<NodeId> = ids.iter().copied().collect();
let compounds: HashSet<NodeId> =
ids.iter().filter_map(|i| d.parent_of(*i)).filter(|p| matches!(d.node(*p).map(|n| &n.kind), Some(NodeKind::Compound { .. }))).collect();
for c in compounds {
if d.children(Some(c)).is_some_and(|k| k.iter().all(|n| gone.contains(&n.id))) {
gone.insert(c);
}
}
gone.extend(crate::cmd::emptied_compounds(d, ids));
let parents: HashSet<NodeId> = gone.iter().filter_map(|i| d.parent_of(*i)).filter(|p| !gone.contains(p)).collect();
parents.into_iter().filter(|p| named_as_tracing(d, *p)).any(|p| {
d.children(Some(p)).and_then(|c| c.iter().find(|n| !gone.contains(&n.id)).map(|n| matches!(n.kind, NodeKind::Image(_)))).unwrap_or(false)