Duplicate: number the copy from the shape count, not the count of distinct names

This commit is contained in:
Harrison Booth
2026-10-27 21:02:22 -05:00
parent fa0a3e9dee
commit ddd7d210ef
3 changed files with 34 additions and 8 deletions
+9 -4
View File
@@ -330,7 +330,7 @@ fn paste(s: &mut Session, p: &Value) -> Result<Value> {
&& !n.is_empty()
&& n.bytes().all(|b| b.is_ascii_digit())
{
sh.name = super::insert::first_free_name(base, &taken);
sh.name = super::insert::first_free_name(base, &taken, taken.len());
}
taken.insert(sh.name.clone());
if let Some(x) = sh.xfrm.as_mut() {
@@ -512,11 +512,11 @@ fn paste_text(s: &mut Session, p: &Value) -> Result<Value> {
/// PowerPoint does, and keeps a name the user chose. PowerPoint tells the two apart by comparing
/// the number with the shape's id; ids here are numbered across the deck, so any "{kind} {n}"
/// counts as automatic.
fn rename_automatic(sh: &mut Shape, taken: &mut std::collections::HashSet<String>) {
fn rename_automatic(sh: &mut Shape, taken: &mut std::collections::HashSet<String>, count: usize) {
let base = if sh.is_group() { "Group".to_string() } else { super::insert::auto_name_base(sh) };
let n = sh.name.strip_prefix(&base).and_then(|r| r.strip_prefix(' '));
if n.is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())) {
sh.name = super::insert::first_free_name(&base, taken);
sh.name = super::insert::first_free_name(&base, taken, count);
taken.insert(sh.name.clone());
}
}
@@ -547,10 +547,15 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
reid("edit.duplicate", doc, &mut copies)?;
// Every name on the slide, group members included, so a copy never repeats one.
let mut taken = std::collections::HashSet::new();
let mut count = 0;
deckcraft_model::walk(&list, &mut |sh, _| {
taken.insert(sh.name.clone());
count += 1;
});
walk_mut(&mut copies, &mut |c| {
rename_automatic(c, &mut taken, count);
count += 1;
});
walk_mut(&mut copies, &mut |c| rename_automatic(c, &mut taken));
for (c, b) in copies.iter_mut().zip(boxes) {
let mut x = b;
x.x += dx;
+5 -4
View File
@@ -172,9 +172,10 @@ pub(crate) fn auto_name_base(shape: &Shape) -> String {
}
/// "{base} n" with the first `n` from the shape count + 1 that no shape on the slide already uses,
/// so a gap left by a deleted shape never makes a second shape with the same name.
pub(crate) fn first_free_name(base: &str, taken: &std::collections::HashSet<String>) -> String {
(taken.len() + 1..).map(|n| format!("{base} {n}")).find(|c| !taken.contains(c)).unwrap_or_default()
/// so a gap left by a deleted shape never makes a second shape with the same name. `count` is the
/// number of shapes, which exceeds the number of names when two shapes share one.
pub(crate) fn first_free_name(base: &str, taken: &std::collections::HashSet<String>, count: usize) -> String {
(count + 1..).map(|n| format!("{base} {n}")).find(|c| !taken.contains(c)).unwrap_or_default()
}
pub(crate) fn add_shape(s: &mut Session, cmd: &str, mut shape: Shape, select: bool) -> Result<ShapeId> {
@@ -183,7 +184,7 @@ pub(crate) fn add_shape(s: &mut Session, cmd: &str, mut shape: Shape, select: bo
if shape.name.is_empty() {
let base = auto_name_base(&shape);
let taken = super::current_names(doc, sel);
shape.name = first_free_name(&base, &taken);
shape.name = first_free_name(&base, &taken, taken.len());
}
let id = shape.id;
let list = crate::shapes_mut(doc, sel).ok_or_else(|| bad("insert", "no slide"))?;