From 1569394173dc140f9eef29e13757b8ca9a567cc8 Mon Sep 17 00:00:00 2001 From: alehel Date: Mon, 23 Nov 2026 13:07:13 +0100 Subject: [PATCH] sheets: a pending cut ends when a sheet edit puts another sheet in its place A cut names its sheet by its place among the tabs, so after inserting, deleting or moving a sheet before it, a paste took cells and rules from whichever sheet had that place. The cut now ends when its place holds another sheet (or none); a cut whose sheet kept its place stays, and a copy reads its own snapshot and stays. --- crates/engine/src/cmd/sheet/tabs.rs | 24 ++++++++++++++++-- crates/engine/src/tests.rs | 39 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/sheet/tabs.rs b/crates/engine/src/cmd/sheet/tabs.rs index 77852566c5..732ab6fce4 100644 --- a/crates/engine/src/cmd/sheet/tabs.rs +++ b/crates/engine/src/cmd/sheet/tabs.rs @@ -29,9 +29,26 @@ pub(super) fn insert_sheet(s: &mut Session, p: &Json) -> Result { Ok(at) })?; d.sheet_selections.insert(idx.min(d.sheet_selections.len()), crate::Selection::default()); + end_cut(s); Ok(json!({"index": idx, "name": name})) } +/// A pending cut names its sheet by its place among the tabs, so when inserting, deleting or +/// moving a sheet puts another sheet (or none) in that place, the cut ends, rather than letting a +/// paste take cells and rules from whichever sheet has the place now. A copy reads its own +/// snapshot and stays, and so does a cut whose sheet kept its place. +fn end_cut(s: &mut Session) { + let Ok(d) = s.doc() else { return }; + let moved = s.clipboard.as_ref().is_some_and(|c| { + let then = c.wb.sheet(c.sheet).map(|sh| sh.uid.get()); + let now = d.wb.sheet(c.sheet).map(|sh| sh.uid.get()); + c.cut && c.doc_uid == d.uid && (then.is_none() || then != now) + }); + if moved { + s.clipboard = None; + } +} + fn edit_doc(d: &mut crate::DocState, f: impl FnOnce(&mut Ctx) -> Result) -> Result { crate::cmd::commit(d, f) } @@ -71,6 +88,7 @@ pub(super) fn delete_sheet(s: &mut Session, p: &Json) -> Result { if i < d.sheet_selections.len() { d.sheet_selections.remove(i); } + end_cut(s); ok() } @@ -115,7 +133,7 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result { let i = target_sheet(s, p)?; let copy = bool_param(p, "copy").unwrap_or(false); let to = p.get("to").and_then(Json::as_u64).map(|v| v as usize).ok_or_else(|| bad("sheet.move", "missing `to`"))?; - edit(s, |cx| { + let out = edit(s, |cx| { let n = cx.wb.sheets.len(); let Some(src) = cx.wb.sheets.get(i).cloned() else { return Err(bad("sheet.move", "no such sheet")) }; if copy { @@ -216,7 +234,9 @@ pub(super) fn move_sheet(s: &mut Session, p: &Json) -> Result { } cx.structural = true; Ok(Json::Null) - }) + })?; + end_cut(s); + Ok(out) } pub(super) fn tab_color(s: &mut Session, p: &Json) -> Result { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index cdb36ea323..c1aa2fbc76 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7569,6 +7569,45 @@ fn a_cut_is_not_transposed_or_tiled() { assert_eq!(s.doc().unwrap().wb.sheets[0].cond_formats[0].ranges.iter().map(|r| r.a1()).collect::>(), vec!["E1:G1".to_string()]); } +/// A cut names its sheet by its place among the tabs: inserting, deleting or moving a sheet that +/// puts another sheet in that place ends it, so a paste never takes cells (and rules) from +/// whichever sheet has that place now. A copy stays, and so does a cut whose sheet kept its place. +#[test] +fn a_cut_ends_when_the_tabs_change() { + let mut s = s(); + s.execute("home.insertSheet", json!({"name": "Other", "before": 1})).unwrap(); + s.execute("range.setValues", json!({"range": "Sheet1!A1", "values": [[1]]})).unwrap(); + s.execute("range.setValues", json!({"range": "Other!A1", "values": [[7]]})).unwrap(); + let at = |s: &Session, sheet: &str, a1: &str| { + let d = s.doc().unwrap(); + d.wb.sheet(d.wb.sheet_index(sheet).unwrap()).unwrap().value(CellRef::parse(a1).unwrap()) + }; + for step in [json!(["sheet.move", {"sheet": 0, "to": 1}]), json!(["home.insertSheet", {"name": "New", "before": 0}])] { + s.execute("sheet.activate", json!({"sheet": s.doc().unwrap().wb.sheet_index("Sheet1").unwrap()})).unwrap(); + s.execute("edit.cut", json!({"range": "A1"})).unwrap(); + s.execute(step[0].as_str().unwrap(), step[1].clone()).unwrap(); + assert!(s.clipboard.is_none(), "{step}"); + let other = s.doc().unwrap().wb.sheet_index("Other").unwrap(); + s.execute("sheet.activate", json!({"sheet": other})).unwrap(); + assert!(s.execute("edit.paste", json!({"at": "C1"})).is_err(), "{step}"); + assert_eq!((at(&s, "Sheet1", "A1"), at(&s, "Other", "A1")), (Value::Number(1.0), Value::Number(7.0)), "{step}"); + } + let new = s.doc().unwrap().wb.sheet_index("New").unwrap(); + s.execute("sheet.activate", json!({"sheet": s.doc().unwrap().wb.sheet_index("Sheet1").unwrap()})).unwrap(); + s.execute("edit.cut", json!({"range": "A1"})).unwrap(); + s.execute("home.deleteSheet", json!({"sheet": new})).unwrap(); + assert!(s.clipboard.is_none()); + // A copy stays, and so does a cut whose sheet keeps its place. + s.execute("edit.copy", json!({"range": "A1"})).unwrap(); + s.execute("home.insertSheet", json!({"name": "More", "before": 0})).unwrap(); + assert!(s.clipboard.is_some()); + let last = s.doc().unwrap().wb.sheets.len(); + s.execute("sheet.activate", json!({"sheet": s.doc().unwrap().wb.sheet_index("Sheet1").unwrap()})).unwrap(); + s.execute("edit.cut", json!({"range": "A1"})).unwrap(); + s.execute("home.insertSheet", json!({"name": "After", "before": last})).unwrap(); + assert!(s.clipboard.is_some()); +} + /// A cut pasted into a row the filter hides would skip every cell it writes, so the cells would /// leave the sheet with the cut: it is refused and nothing changes. A visible row takes it. #[test]