solve failing test
All checks were successful
Test Rust project / test (ubuntu-latest, stable) (push) Successful in 53s

copy and pasting functions with ranges as arguments now works
This commit is contained in:
2026-02-06 11:31:41 -07:00
parent 045d1d6554
commit d983995e8f
3 changed files with 62 additions and 7 deletions

View File

@@ -384,4 +384,15 @@ fn copy_paste_range_in_function() {
let a = app.grid.get_cell("C0").as_ref().expect("Should've been set by paste"); let a = app.grid.get_cell("C0").as_ref().expect("Should've been set by paste");
assert_eq!(a.to_string(), "=sum(B:B)"); assert_eq!(a.to_string(), "=sum(B:B)");
// now copy the range the other direction
app.grid.mv_cursor_to(2, 0);
app.mode = super::mode::Mode::Chord(Chord::new('y'));
Mode::process_key(&mut app, 'y');
app.grid.mv_cursor_to(1, 1);
Mode::process_key(&mut app, 'p');
let a = app.grid.get_cell("B1").as_ref().expect("Should've been set by paste");
assert_eq!(a.to_string(), "=sum(A:A)");
} }

View File

@@ -393,12 +393,22 @@ impl Grid {
} }
} }
pub fn char_to_idx(i: &str) -> usize {
let x_idx = i
.chars()
.filter(|f| f.is_alphabetic())
.enumerate()
.map(|(idx, c)| ((c.to_ascii_lowercase() as usize).saturating_sub(97)) + (26 * idx))
.fold(0, |a, b| a + b);
x_idx
}
/// Parse values in the format of A0, C10 ZZ99, etc, and /// Parse values in the format of A0, C10 ZZ99, etc, and
/// turn them into an X,Y index. /// turn them into an X,Y index.
pub fn parse_to_idx(i: &str) -> Option<(usize, usize)> { pub fn parse_to_idx(i: &str) -> Option<(usize, usize)> {
let i = i.replace('$', ""); let i = i.replace('$', "");
let chars = i.chars().take_while(|c| c.is_alphabetic()).collect::<Vec<char>>(); let chars = i.chars().take_while(|c| c.is_alphabetic()).collect::<String>();
let nums = i.chars().skip(chars.len()).take_while(|c| c.is_numeric()).collect::<String>(); let nums = i.chars().skip(chars.len()).take_while(|c| c.is_numeric()).collect::<String>();
// At least half the arguments are gone // At least half the arguments are gone
@@ -407,11 +417,7 @@ impl Grid {
} }
// get the x index from the chars // get the x index from the chars
let x_idx = chars let x_idx = Self::char_to_idx(&chars);
.iter()
.enumerate()
.map(|(idx, c)| (c.to_ascii_lowercase() as usize - 97) + (26 * idx))
.fold(0, |a, b| a + b);
// get the y index from the numbers // get the y index from the numbers
if let Ok(y_idx) = nums.parse::<usize>() { if let Ok(y_idx) = nums.parse::<usize>() {
@@ -591,6 +597,7 @@ fn alphanumeric_indexing() {
assert_eq!(Grid::parse_to_idx("A"), None); assert_eq!(Grid::parse_to_idx("A"), None);
assert_eq!(Grid::parse_to_idx(":"), None); assert_eq!(Grid::parse_to_idx(":"), None);
assert_eq!(Grid::parse_to_idx("="), None); assert_eq!(Grid::parse_to_idx("="), None);
assert_eq!(Grid::parse_to_idx("A:A"), None);
assert_eq!(Grid::num_to_char(0).trim(), "A"); assert_eq!(Grid::num_to_char(0).trim(), "A");
assert_eq!(Grid::num_to_char(25).trim(), "Z"); assert_eq!(Grid::num_to_char(25).trim(), "Z");

View File

@@ -73,7 +73,10 @@ impl CellType {
let mut lock_y = false; let mut lock_y = false;
if old_var.contains('$') { if old_var.contains('$') {
let locations = old_var.char_indices().filter(|(_, c)| *c == '$').map(|(i, _)| i).collect::<Vec<usize>>(); let locations = old_var
.char_indices()
.filter(|(_, c)| *c == '$').map(|(i, _)| i)
.collect::<Vec<usize>>();
match locations.len() { match locations.len() {
1 => { 1 => {
if locations[0] == 0 { if locations[0] == 0 {
@@ -136,6 +139,40 @@ impl CellType {
// rolling = rolling.replace(&old_var, &new_var); // rolling = rolling.replace(&old_var, &new_var);
} else { } else {
// why you coping invalid stuff, nerd? // why you coping invalid stuff, nerd?
//
// could be copying a range
if old_var.contains(':') {
let parts = old_var.split(':').collect::<Vec<&str>>();
// This means the var was formatted as X:X
if parts.len() == 2 {
// how far is the movement?
let dx = to.0 as i32 - from.0 as i32;
let range_start = parts[0];
let range_end = parts[1];
// get the letters as numbers
let xs = Grid::char_to_idx(range_start) as i32;
let xe = Grid::char_to_idx(range_end) as i32;
// apply movement
let mut new_range_start = xs+dx;
let mut new_range_end = xe+dx;
// bottom out at 0
if new_range_start < 0 {
new_range_start = 0;
}
if new_range_end < 0 {
new_range_end = 0;
}
// convert the index back into a letter and then submit it
let start = Grid::num_to_char(new_range_start as usize);
let end = Grid::num_to_char(new_range_end as usize);
equation = replace_fn(&equation, &old_var, &format!("{}:{}", start.trim(), end.trim()));
}
}
} }
} }
return equation.into(); return equation.into();