This commit is contained in:
2026-01-22 15:46:17 -07:00
parent da5e4921d8
commit 4452d3210e
2 changed files with 33 additions and 14 deletions

View File

@@ -49,6 +49,7 @@ impl Clipboard {
// cursor // cursor
let (cx, cy) = into.cursor(); let (cx, cy) = into.cursor();
// iterate thru the clipbaord's cells
for (x, row) in self.clipboard.iter().enumerate() { for (x, row) in self.clipboard.iter().enumerate() {
for (y, cell) in row.iter().enumerate() { for (y, cell) in row.iter().enumerate() {
let idx = (x + cx, y + cy); let idx = (x + cx, y + cy);
@@ -58,7 +59,7 @@ impl Clipboard {
let trans = cell.translate_cell(self.source_cell, into.cursor()); let trans = cell.translate_cell(self.source_cell, into.cursor());
into.set_cell_raw(idx, Some(trans)); into.set_cell_raw(idx, Some(trans));
} else { } else {
// cell doesn't exist, no need to translate // The cell at this location doesn't exist (empty)
into.set_cell_raw::<CellType>(idx, None); into.set_cell_raw::<CellType>(idx, None);
} }
} else { } else {
@@ -349,4 +350,20 @@ fn copy_paste_y_locked_var() {
Mode::process_key(&mut app, 'p'); Mode::process_key(&mut app, 'p');
let c = app.grid.get_cell("B2").as_ref().expect("Just set it"); let c = app.grid.get_cell("B2").as_ref().expect("Just set it");
assert_eq!(c.to_string(), "=B$0"); assert_eq!(c.to_string(), "=B$0");
} }
#[test]
fn issue_47() {
let mut app = App::new();
app.grid.set_cell("A0", 4.to_string());
Mode::process_key(&mut app, 'j');
app.grid.set_cell("A1", "=math::log2(A0)".to_string());
app.mode = super::mode::Mode::Chord(Chord::new('y'));
Mode::process_key(&mut app, 'y');
Mode::process_key(&mut app, 'j');
Mode::process_key(&mut app, 'p');
let a = app.grid.get_cell("A2").as_ref().expect("Should've been set by paste");
assert_eq!(a.to_string(), "=math::log2(A1)");
}

View File

@@ -54,17 +54,20 @@ impl CellType {
} }
} }
/// `replace_fn` takes the string, the old value, and then the new value.
/// It can be thought of as `echo $1 | sed s/$2/$3/g`
pub fn custom_translate_cell(&self, from: (usize, usize), to: (usize, usize), replace_fn: impl Fn(&str, &str, &str) -> String) -> CellType { pub fn custom_translate_cell(&self, from: (usize, usize), to: (usize, usize), replace_fn: impl Fn(&str, &str, &str) -> String) -> CellType {
match self { match self {
// don't translate non-equations // don't translate non-equations
CellType::Number(_) | CellType::String(_) => return self.clone(), CellType::Number(_) | CellType::String(_) => return self.clone(),
CellType::Equation(eq) => { CellType::Equation(eq) => {
// extract all the variables // Populate the context
let ctx = ExtractionContext::new(); let ctx = ExtractionContext::new();
let _ = eval_with_context(eq, &ctx); let _ = eval_with_context(eq, &ctx);
let mut rolling = eq.clone(); let mut equation = eq.clone();
// translate standard vars A0 -> A1 // translate standard vars A0 -> A1
// extract all the variables
for old_var in ctx.dump_vars() { for old_var in ctx.dump_vars() {
let mut lock_x = false; let mut lock_x = false;
let mut lock_y = false; let mut lock_y = false;
@@ -76,20 +79,18 @@ impl CellType {
if locations[0] == 0 { if locations[0] == 0 {
// locking the X axis (A,B,C...) // locking the X axis (A,B,C...)
lock_x = true; lock_x = true;
} else if locations[0] < old_var.len() { } else {
// inside the string somewhere, gonna assume this means to lock Y (1,2,3...) // inside the string somewhere, gonna assume this means to lock Y (1,2,3...)
lock_y = true; lock_y = true;
} else {
// where tf is this dollar sign?
// (It's somewhere malformed, like A0$ or something)
} }
} }
2 => { 2 => {
// YOLO, lock both X & Y // Ignore this variable all together, effectively lockng X & Y
continue; // just pretend you never even saw this var continue;
} }
_ => { _ => {
// There are 0 or >2 "$" in this string.
//
// Could probably optimize the code or something so you only go over the string // Could probably optimize the code or something so you only go over the string
// once, instead of contains() then getting the indexes of where it is. // once, instead of contains() then getting the indexes of where it is.
// You could then put your no-$ code here. // You could then put your no-$ code here.
@@ -98,6 +99,7 @@ impl CellType {
} }
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) {
// Use i32s instead of usize in case of negative numbers
let (x1, y1) = from; let (x1, y1) = from;
let x1 = x1 as i32; let x1 = x1 as i32;
let y1 = y1 as i32; let y1 = y1 as i32;
@@ -120,6 +122,7 @@ impl CellType {
let alpha = Grid::num_to_char(dest_x); let alpha = Grid::num_to_char(dest_x);
let alpha = alpha.trim(); let alpha = alpha.trim();
// Persist the "$" locking
let new_var = if lock_x { let new_var = if lock_x {
format!("${alpha}{dest_y}") format!("${alpha}{dest_y}")
} else if lock_y { } else if lock_y {
@@ -128,15 +131,14 @@ impl CellType {
format!("{alpha}{dest_y}") format!("{alpha}{dest_y}")
}; };
// swap out vars // swap out vars
rolling = replace_fn(&rolling, &old_var, &new_var); equation = replace_fn(&equation, &old_var, &new_var);
// 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?
} }
} }
return rolling.into(); return equation.into();
} }
} }
} }