AutoSum: test the menu functions; keep the new editor method clear of #1056

Home › AutoSum ▾ Average proposes `=AVERAGE(range)` the same way (and so
will Formulas › AutoSum once it has the menu, #658). `point_last_argument`
moves to the end of `impl EditState`, so this PR no longer touches the
lines #1056 and #1838 change after `insert_ref`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kezmdor
2026-10-20 08:59:30 +02:00
co-authored by Claude Opus 5.5
parent fdad356fd2
commit 132bd4e9a8
2 changed files with 29 additions and 17 deletions
+17 -17
View File
@@ -110,23 +110,6 @@ impl EditState {
self.sync_caret = true;
}
/// Points and selects the argument of the call that ends the text, as AutoSum proposes it
/// (`=SUM(B1:B3)`): a click, drag, arrow key or typing replaces `B1:B3`. An arrow key moves
/// from the range's first cell, as in Excel: ↑ on `=SUM(A2:A4)` gives `=SUM(A1)`. With no
/// argument (`=SUM()`) the caret goes between the parentheses.
pub fn point_last_argument(&mut self) {
let Some(open) = self.text.rfind('(') else { return };
let Some(close) = self.text.len().checked_sub(1).filter(|&c| c > open && self.text.ends_with(')')) else { return };
let chars = |byte: usize| self.text.get(..byte).map_or(0, |t| t.chars().count());
let (start, end) = (open + 1, close);
self.caret = chars(end);
self.point = (start < end).then_some((start, end));
self.select_from = (start < end).then(|| chars(start));
self.point_cell = self.text.get(start..end).and_then(RangeRef::parse).map(|r| r.start);
self.caret_to_end = false;
self.sync_caret = true;
}
/// Updates function autocomplete for the word before the caret.
pub fn update_autocomplete(&mut self, names: &[String]) {
self.autocomplete.clear();
@@ -211,6 +194,23 @@ impl EditState {
}
None
}
/// Points and selects the argument of the call that ends the text, as AutoSum proposes it
/// (`=SUM(B1:B3)`): a click, drag, arrow key or typing replaces `B1:B3`. An arrow key moves
/// from the range's first cell, as in Excel: ↑ on `=SUM(A2:A4)` gives `=SUM(A1)`. With no
/// argument (`=SUM()`) the caret goes between the parentheses.
pub fn point_last_argument(&mut self) {
let Some(open) = self.text.rfind('(') else { return };
let Some(close) = self.text.len().checked_sub(1).filter(|&c| c > open && self.text.ends_with(')')) else { return };
let chars = |byte: usize| self.text.get(..byte).map_or(0, |t| t.chars().count());
let (start, end) = (open + 1, close);
self.caret = chars(end);
self.point = (start < end).then_some((start, end));
self.select_from = (start < end).then(|| chars(start));
self.point_cell = self.text.get(start..end).and_then(RangeRef::parse).map(|r| r.start);
self.caret_to_end = false;
self.sync_caret = true;
}
}
/// References in formula text with their byte spans and colour index (same reference text ⇒
+12
View File
@@ -79,6 +79,18 @@ fn autosum_on_one_cell_proposes_its_range_in_the_editor() {
assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().value(CellRef::parse("A4").unwrap()), Value::Number(6.0));
}
#[test]
fn the_autosum_menu_functions_propose_theirs_the_same_way() {
// Home › AutoSum ▾ Average (and Formulas › AutoSum ▾ once it has the menu, #658).
let mut h = harness("A4");
h.state_mut().run_or_alert("formulas.autoSum", json!({"function": "AVERAGE"}));
h.run_steps(2);
assert_eq!(editing(&h).as_deref(), Some("=AVERAGE(A1:A3)"));
pointer(&mut h, "A2", true);
pointer(&mut h, "A2", false);
assert_eq!(editing(&h).as_deref(), Some("=AVERAGE(A2)"));
}
#[test]
fn a_click_or_drag_replaces_the_proposed_range() {
let mut h = harness("A4");