From e420f7e998a1b97f73129548cd7f676e7897f5db Mon Sep 17 00:00:00 2001 From: Brian Hung Date: Tue, 22 Jul 2025 15:27:52 -0700 Subject: [PATCH] fix intermediate rows cols --- base/src/expressions/parser/stringify.rs | 36 ++++++++++++------- base/src/test/test_actions.rs | 46 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/base/src/expressions/parser/stringify.rs b/base/src/expressions/parser/stringify.rs index b1bbbc2..62d4c12 100644 --- a/base/src/expressions/parser/stringify.rs +++ b/base/src/expressions/parser/stringify.rs @@ -172,10 +172,18 @@ pub(crate) fn stringify_reference( if sheet_index == *sheet { if row == *move_row { row += *delta; - } else if (*delta > 0 && row > *move_row && row <= *move_row + *delta) - || (*delta < 0 && row < *move_row && row >= *move_row + *delta) - { - row -= *delta; + } else if *delta > 0 { + // Moving the row downwards + if row > *move_row && row <= *move_row + *delta { + // Intermediate rows move up by one position + row -= 1; + } + } else if *delta < 0 { + // Moving the row upwards + if row < *move_row && row >= *move_row + *delta { + // Intermediate rows move down by one position + row += 1; + } } } } @@ -187,14 +195,18 @@ pub(crate) fn stringify_reference( if sheet_index == *sheet { if column == *move_column { column += *delta; - } else if (*delta > 0 - && column > *move_column - && column <= *move_column + *delta) - || (*delta < 0 - && column < *move_column - && column >= *move_column + *delta) - { - column -= *delta; + } else if *delta > 0 { + // Moving the column to the right + if column > *move_column && column <= *move_column + *delta { + // Intermediate columns move left by one position + column -= 1; + } + } else if *delta < 0 { + // Moving the column to the left + if column < *move_column && column >= *move_column + *delta { + // Intermediate columns move right by one position + column += 1; + } } } } diff --git a/base/src/test/test_actions.rs b/base/src/test/test_actions.rs index 99fe81c..7af268c 100644 --- a/base/src/test/test_actions.rs +++ b/base/src/test/test_actions.rs @@ -734,5 +734,51 @@ fn test_move_row_height() { assert_eq!(model.get_row_height(sheet, 3), Ok(original_row4_height)); } +/// Moving a row down by two positions should shift formulas on intermediate +/// rows by only one (the row that gets skipped), not by the full delta ‒ this +/// guards against the regression fixed in the RowMove displacement logic. +#[test] +fn test_row_move_down_two_updates_intermediate_refs_by_one() { + let mut model = new_empty_model(); + populate_table(&mut model); + // Set up formulas to verify intermediate rows shift by 1 (not full delta). + model._set("E3", "=G3"); // target row + model._set("E4", "=G4"); // intermediate row + model._set("E5", "=SUM(G3:J3)"); + model.evaluate(); + + // Move row 3 down by two positions (row 3 -> row 5) + assert!(model.move_row_action(0, 3, 2).is_ok()); + model.evaluate(); + + // Assert that references for the moved row and intermediate row are correct. + assert_eq!(model._get_formula("E3"), "=G3"); + assert_eq!(model._get_formula("E5"), "=G5"); + assert_eq!(model._get_formula("E4"), "=SUM(G5:J5)"); +} + +/// Moving a column right by two positions should shift formulas on +/// intermediate columns by only one, ensuring the ColumnMove displacement +/// logic handles multi-position moves correctly. +#[test] +fn test_column_move_right_two_updates_intermediate_refs_by_one() { + let mut model = new_empty_model(); + populate_table(&mut model); + // Set up formulas to verify intermediate columns shift by 1 (not full delta). + model._set("E3", "=$G3"); // target column + model._set("E4", "=$H3"); // intermediate column + model._set("E5", "=SUM($G3:$J7)"); + model.evaluate(); + + // Move column G (7) right by two positions (G -> I) + assert!(model.move_column_action(0, 7, 2).is_ok()); + model.evaluate(); + + // Assert that references for moved and intermediate columns are correct. + assert_eq!(model._get_formula("E3"), "=$I3"); + assert_eq!(model._get_formula("E4"), "=$G3"); + assert_eq!(model._get_formula("E5"), "=SUM($I3:$J7)"); +} + // A B C D E F G H I J K L M N O P Q R // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18