diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index a3dd9ac37..e248fc13b 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -52,6 +52,15 @@ impl SheetApp { // Requests the command made (a dialog, a draft) belong to the window it left active. self.follow_active_window(); self.after_engine(); + if id == "formulas.autoSum" + && let Some(ed) = self.editor.as_mut() + && ed.text.starts_with('=') + && ed.text.ends_with("()") + { + ed.caret = ed.text.chars().count().saturating_sub(1); + ed.anchor = ed.caret; + ed.sync_caret = true; + } if id == "view.arrangeAll" && self.native_windows && let Ok(r) = &r diff --git a/crates/ui-egui/tests/it/formula_locale.rs b/crates/ui-egui/tests/it/formula_locale.rs index 78af96a1a..4b8ce0420 100644 --- a/crates/ui-egui/tests/it/formula_locale.rs +++ b/crates/ui-egui/tests/it/formula_locale.rs @@ -140,6 +140,15 @@ fn locale_preserves_text_cells_and_existing_entry_conveniences() { assert_eq!(app.editor.as_ref().unwrap().text, "=SUM(1,2)"); } +#[test] +fn empty_cell_autosum_places_caret_inside_parentheses() { + let mut app = app(); + app.run("formulas.autoSum", json!({"enter": false})).unwrap(); + let ed = app.editor.as_ref().unwrap(); + assert_eq!(ed.text, "=SUM()"); + assert_eq!(ed.caret, ed.text.chars().count() - 1); +} + #[test] /// Insert Function and AutoSum use `=SUMA(`, while user-supplied formula text is not translated twice. fn canonical_engine_templates_enter_the_selected_formula_language() {