From 1f138bc34ab99142b9fafca0729ea6be2d20e97f Mon Sep 17 00:00:00 2001 From: alehel Date: Sat, 14 Nov 2026 10:50:26 +0100 Subject: [PATCH] engine: review fixes for the reference rewrite (CodeRabbit) A cut moves the rules its cells hold when it is pasted, not the clipboard snapshot's: a rule cleared after the cut came back at the destination. A table rename leaves a bare name in a workbook name alone when a workbook-level defined name of that name exists, as it does in cells. --- crates/engine/src/cmd/edit/paste_special.rs | 7 +++-- crates/engine/src/cmd/insert.rs | 6 +++-- crates/engine/src/tests.rs | 30 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index cadabcfd9..d16ac9feb 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -84,6 +84,9 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { }; let mut moved_formulas = std::collections::BTreeMap::new(); let mut moved_pictures = std::collections::BTreeMap::new(); + // A cut moves the rules its cells hold now, not the clipboard's: rules cleared or added + // since the cut would otherwise come back or be lost. + let live_source = if cut_move { cx.wb.sheet(clip.sheet).cloned() } else { None }; if cut_move { // Remove the source first (contents and formats), and fix references to it. let source = cx.sheet_mut(clip.sheet)?; @@ -372,8 +375,8 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { } } } - if cut_move { - move_cut_rules(cx, src_sheet, (clip.sheet, dest_sheet), src, at)?; + if let Some(live) = &live_source { + move_cut_rules(cx, live, (clip.sheet, dest_sheet), src, at)?; // References elsewhere to the moved block now point to its new place. let e = if dest_sheet == clip.sheet { gridcraft_formula::adjust::Edit::Move { from: src, to_row: at.row, to_col: at.col } diff --git a/crates/engine/src/cmd/insert.rs b/crates/engine/src/cmd/insert.rs index 25826e092..64b5568c5 100644 --- a/crates/engine/src/cmd/insert.rs +++ b/crates/engine/src/cmd/insert.rs @@ -554,12 +554,14 @@ fn table_rename(s: &mut Session, p: &Json) -> Result { t.name = name.clone(); } // Every reference to the table follows the rename. A bare name that is also a defined - // name where it is held resolves to the defined name, so it is not the table. + // name where it is held resolves to the defined name, so it is not the table; in a + // workbook name, that is a workbook-level defined name. let shadowed: Vec = (0..cx.wb.sheets.len()).map(|i| cx.wb.name(&old, i).is_some()).collect(); + let shadowed_in_book = cx.wb.names.iter().any(|n| n.scope.is_none() && n.name.eq_ignore_ascii_case(&old)); let sheet_of: std::collections::HashMap = cx.wb.sheets.iter().enumerate().map(|(i, s)| (s.name.to_ascii_lowercase(), i)).collect(); let rename = |sheet: Option, e: gridcraft_formula::Expr| { - let shadow = sheet.is_some_and(|i| shadowed.get(i).copied().unwrap_or(false)); + let shadow = sheet.map_or(shadowed_in_book, |i| shadowed.get(i).copied().unwrap_or(false)); e.map(&mut |x| match x { gridcraft_formula::Expr::Struct(mut st) if st.table.eq_ignore_ascii_case(&old) => { st.table = name.clone(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 90d9e6a83..8ad0eb51d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2028,6 +2028,20 @@ fn table_rename_keeps_a_defined_name_with_the_same_name() { assert_eq!((v(&s, "E1"), v(&s, "F1")), (Value::Number(100.0), Value::Number(7.0))); } +/// A workbook name that reads a same-named workbook name keeps reading it when the table is renamed. +#[test] +fn table_rename_keeps_a_workbook_name_reading_a_same_named_name() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [["Qty"], [7]]})).unwrap(); + s.execute("cell.set", json!({"cell": "D1", "input": "100"})).unwrap(); + s.execute("insert.table", json!({"range": "A1:A2", "header": true, "name": "Data"})).unwrap(); + s.execute("formulas.defineName", json!({"name": "Data", "refersTo": "=Sheet1!$D$1"})).unwrap(); + s.execute("formulas.defineName", json!({"name": "Twice", "refersTo": "=Data*2"})).unwrap(); + s.execute("table.rename", json!({"table": "Data", "name": "Later"})).unwrap(); + let twice = s.doc().unwrap().wb.names.iter().find(|n| n.name == "Twice").map(|n| n.formula.clone()); + assert_eq!(twice.as_deref(), Some("Data*2")); +} + #[test] fn table_names_are_limited_to_255_characters() { let mut s = s(); @@ -6916,6 +6930,22 @@ fn rules_moved_whole_or_left_in_place_keep_their_formulas() { assert_eq!(cf(&s, two).last().map(String::as_str), Some("bar MIN(Two!K1:K10,Sheet1!C1:C10)")); } +/// A cut moves the rules its cells hold when it is pasted, not those they held when it was cut. +#[test] +fn a_cut_moves_the_rules_its_cells_hold_at_paste() { + let mut s = s(); + s.execute("home.conditionalFormat", json!({"range": "A1:A3", "rule": {"type": "expression", "formula": "=A1>0"}, "style": {"fill": "#FFC7CE"}})) + .unwrap(); + s.execute("edit.cut", json!({"range": "A1:A3"})).unwrap(); + s.execute("home.clearRules", json!({"whole": true})).unwrap(); + s.execute("data.validation", json!({"range": "A1:A3", "type": "custom", "formula1": "=A1>0"})).unwrap(); + s.execute("edit.paste", json!({"at": "D1"})).unwrap(); + let sh = &s.doc().unwrap().wb.sheets[0]; + assert!(sh.cond_formats.is_empty(), "a cleared rule came back: {:?}", sh.cond_formats); + let dv: Vec = sh.validations.iter().flat_map(|v| v.ranges.iter().map(|r| r.a1())).collect(); + assert_eq!(dv, vec!["D1:D3".to_string()]); +} + /// Moving a column out of a wider rule takes its part of the rule with it, as a cut does: every /// cell, moved or not, still checks itself. #[test]