From 182546007423f715a6b8ffe00d48ab3ea673c750 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 7 Feb 2026 19:30:08 -0700 Subject: [PATCH] add failing test --- src/app/logic/calc.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ src/app/logic/ctx.rs | 18 ++-------------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/app/logic/calc.rs b/src/app/logic/calc.rs index 7036d05..b3bca0d 100644 --- a/src/app/logic/calc.rs +++ b/src/app/logic/calc.rs @@ -270,9 +270,11 @@ impl Grid { } } } + pub fn insert_row_below(&mut self, (x, y): (usize, usize)) { self.insert_row_above((x,y+1)); } + pub fn insert_column_before(&mut self, (insertion_x, _y): (usize, usize)) { let mut v = Vec::with_capacity(LEN); for _ in 0..LEN { @@ -289,6 +291,7 @@ impl Grid { // add 1 because of the insertion if arg_x < insertion_x { rolling.to_owned() } else { rolling.replace(old, new) } } else { + // FIXME could be a range unimplemented!("Invalid variable wanted to be translated") } }) @@ -298,6 +301,7 @@ impl Grid { } } } + pub fn insert_column_after(&mut self, (x, y): (usize, usize)) { self.insert_column_before((x + 1, y)); } @@ -393,6 +397,20 @@ impl Grid { } } + pub fn range_as_indices(range: &str) -> Option<(usize, usize)> { + let v = range.split(':').collect::>(); + if v.len() == 2 { + let start_col = v[0]; + let end_col = v[1]; + + let start_idx = Grid::char_to_idx(start_col); + let end_idx = Grid::char_to_idx(end_col); + + return Some((start_idx, end_idx)) + } + None + } + pub fn char_to_idx(i: &str) -> usize { let x_idx = i .chars() @@ -1006,6 +1024,36 @@ fn insert_col_before_3() { assert_eq!(cell.to_string(), "=B0*B1"); } +#[test] +fn insert_col_before_static_range() { + let mut grid = Grid::new(); + + grid.set_cell("A0", 2.); + grid.set_cell("A1", 2.); + grid.set_cell("B0", "=sum(A:A)".to_string()); + + grid.mv_cursor_to(0, 1); + grid.insert_column_before(grid.cursor()); + + let cell = grid.get_cell("C0").as_ref().expect("Just set it"); + assert_eq!(cell.to_string(), "=sum(A:A)"); +} + +#[test] +fn insert_col_before_move_range() { + let mut grid = Grid::new(); + + grid.set_cell("B0", 2.); + grid.set_cell("B1", 2.); + grid.set_cell("A0", "=sum(B:B)".to_string()); + + grid.mv_cursor_to(0, 0); + grid.insert_column_after(grid.cursor()); + + let cell = grid.get_cell("A0").as_ref().expect("Just set it"); + assert_eq!(cell.to_string(), "=sum(C:C)"); +} + #[test] fn insert_row_above_1() { let mut grid = Grid::new(); diff --git a/src/app/logic/ctx.rs b/src/app/logic/ctx.rs index b41c773..508521a 100644 --- a/src/app/logic/ctx.rs +++ b/src/app/logic/ctx.rs @@ -16,24 +16,10 @@ pub struct CallbackContext<'a> { impl<'a> CallbackContext<'a> { fn expand_range(&self, range: &str) -> Option> { - let v = range.split(':').collect::>(); - if v.len() == 2 { - let start_col = v[0]; - let end_col = v[1]; - - let as_index = |s: &str| { - s.char_indices() - // .filter(|f| f.1 as u8 >= 97) // prevent sub with overflow errors - .map(|(idx, c)| ((c.to_ascii_lowercase() as usize).saturating_sub(97)) + (26 * idx)) - .fold(0, |a, b| a + b) - }; - - let start_idx = as_index(start_col); - let end_idx = as_index(end_col); - + if let Some((start, end)) = Grid::range_as_indices(range) { let mut buf = Vec::new(); - for x in start_idx..=end_idx { + for x in start..=end { for y in 0..=self.variables.max_y_at_x(x) { if let Some(s) = self.variables.get_cell_raw(x, y) { buf.push(s);