abstract translation code for #34
This commit is contained in:
@@ -62,41 +62,8 @@ impl Clipboard {
|
|||||||
|
|
||||||
if translate {
|
if translate {
|
||||||
if let Some(cell) = cell {
|
if let Some(cell) = cell {
|
||||||
match cell {
|
let trans = Clipboard::translate_cell(cell, self.source_cell, into.cursor());
|
||||||
// don't translate non-equations
|
into.set_cell_raw(idx, Some(trans));
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// cell doesn't exist, no need to translate
|
// cell doesn't exist, no need to translate
|
||||||
into.set_cell_raw::<CellType>(idx, None);
|
into.set_cell_raw::<CellType>(idx, None);
|
||||||
@@ -113,6 +80,44 @@ impl Clipboard {
|
|||||||
self.last_paste_cell = (cx, cy);
|
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.
|
/// Clones data from Grid into self.
|
||||||
/// Start and end don't have to be sorted in any sort of way. The function works with
|
/// Start and end don't have to be sorted in any sort of way. The function works with
|
||||||
/// any two points.
|
/// any two points.
|
||||||
|
|||||||
Reference in New Issue
Block a user