abstract translation code for #34

This commit is contained in:
2025-11-13 12:46:09 -07:00
parent 65f18c9abf
commit 306ea9088d

View File

@@ -62,9 +62,28 @@ impl Clipboard {
if translate { if translate {
if let Some(cell) = cell { if let Some(cell) = cell {
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::<CellType>(idx, None);
}
} else {
// translate = false
into.set_cell_raw::<CellType>(idx, cell.clone());
}
}
}
let (lx, ly) = self.last_paste_cell;
self.momentum = (cx as i32 - lx as i32, cy as i32 - ly as i32);
self.last_paste_cell = (cx, cy);
}
fn translate_cell(cell: &CellType, from: (usize, usize), to: (usize, usize)) -> CellType {
match cell { match cell {
// don't translate non-equations // don't translate non-equations
CellType::Number(_) | CellType::String(_) => into.set_cell_raw(idx, Some(cell.clone())), CellType::Number(_) | CellType::String(_) => return cell.clone(),
CellType::Equation(eq) => { CellType::Equation(eq) => {
// extract all the variables // extract all the variables
let ctx = ExtractionContext::new(); let ctx = ExtractionContext::new();
@@ -74,10 +93,10 @@ impl Clipboard {
// translate standard vars A0 -> A1 // translate standard vars A0 -> A1
for old_var in ctx.dump_vars() { for old_var in ctx.dump_vars() {
if let Some((src_x, src_y)) = Grid::parse_to_idx(&old_var) { if let Some((src_x, src_y)) = Grid::parse_to_idx(&old_var) {
let (x1, y1) = self.source_cell; let (x1, y1) = from;
let x1 = x1 as i32; let x1 = x1 as i32;
let y1 = y1 as i32; let y1 = y1 as i32;
let (x2, y2) = into.cursor(); let (x2, y2) = to;
let x2 = x2 as i32; let x2 = x2 as i32;
let y2 = y2 as i32; let y2 = y2 as i32;
@@ -94,23 +113,9 @@ impl Clipboard {
// why you coping invalid stuff, nerd? // why you coping invalid stuff, nerd?
} }
} }
into.set_cell_raw(idx, Some(rolling)); return rolling.into();
} }
} }
} else {
// cell doesn't exist, no need to translate
into.set_cell_raw::<CellType>(idx, None);
}
} else {
// translate = false
into.set_cell_raw::<CellType>(idx, cell.clone());
}
}
}
let (lx, ly) = self.last_paste_cell;
self.momentum = (cx as i32 - lx as i32, cy as i32 - ly as i32);
self.last_paste_cell = (cx, cy);
} }
/// Clones data from Grid into self. /// Clones data from Grid into self.