From 306ea9088d64806a88169c3036aa88a7842142ef Mon Sep 17 00:00:00 2001 From: Rushmore75 Date: Thu, 13 Nov 2025 12:46:09 -0700 Subject: [PATCH] abstract translation code for #34 --- src/app/clipboard.rs | 75 +++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/src/app/clipboard.rs b/src/app/clipboard.rs index bc32122..4eab934 100644 --- a/src/app/clipboard.rs +++ b/src/app/clipboard.rs @@ -62,41 +62,8 @@ impl Clipboard { if translate { if let Some(cell) = cell { - match cell { - // don't translate non-equations - CellType::Number(_) | CellType::String(_) => into.set_cell_raw(idx, Some(cell.clone())), - CellType::Equation(eq) => { - // extract all the variables - let ctx = ExtractionContext::new(); - let _ = eval_with_context(eq, &ctx); - - let mut rolling = eq.clone(); - // translate standard vars A0 -> A1 - for old_var in ctx.dump_vars() { - if let Some((src_x, src_y)) = Grid::parse_to_idx(&old_var) { - let (x1, y1) = self.source_cell; - let x1 = x1 as i32; - let y1 = y1 as i32; - let (x2, y2) = into.cursor(); - let x2 = x2 as i32; - let y2 = y2 as i32; - - let dest_x = (src_x as i32 + (x2 - x1)) as usize; - let dest_y = (src_y as i32 + (y2 - y1)) as usize; - - let alpha = Grid::num_to_char(dest_x); - let alpha = alpha.trim(); - let new_var = format!("{alpha}{dest_y}"); - - // swap out vars - rolling = rolling.replace(&old_var, &new_var); - } else { - // why you coping invalid stuff, nerd? - } - } - into.set_cell_raw(idx, Some(rolling)); - } - } + let trans = Clipboard::translate_cell(cell, self.source_cell, into.cursor()); + into.set_cell_raw(idx, Some(trans)); } else { // cell doesn't exist, no need to translate into.set_cell_raw::(idx, None); @@ -113,6 +80,44 @@ impl Clipboard { self.last_paste_cell = (cx, cy); } + fn translate_cell(cell: &CellType, from: (usize, usize), to: (usize, usize)) -> CellType { + match cell { + // don't translate non-equations + CellType::Number(_) | CellType::String(_) => return cell.clone(), + CellType::Equation(eq) => { + // extract all the variables + let ctx = ExtractionContext::new(); + let _ = eval_with_context(eq, &ctx); + + let mut rolling = eq.clone(); + // translate standard vars A0 -> A1 + for old_var in ctx.dump_vars() { + if let Some((src_x, src_y)) = Grid::parse_to_idx(&old_var) { + let (x1, y1) = from; + let x1 = x1 as i32; + let y1 = y1 as i32; + let (x2, y2) = to; + let x2 = x2 as i32; + let y2 = y2 as i32; + + let dest_x = (src_x as i32 + (x2 - x1)) as usize; + let dest_y = (src_y as i32 + (y2 - y1)) as usize; + + let alpha = Grid::num_to_char(dest_x); + let alpha = alpha.trim(); + let new_var = format!("{alpha}{dest_y}"); + + // swap out vars + rolling = rolling.replace(&old_var, &new_var); + } else { + // why you coping invalid stuff, nerd? + } + } + return rolling.into(); + } + } + } + /// Clones data from Grid into self. /// Start and end don't have to be sorted in any sort of way. The function works with /// any two points.