From beade8b734732c069df6c9446afe162eebd2df56 Mon Sep 17 00:00:00 2001 From: Eduardo Garcia Date: Sun, 18 Oct 2026 12:32:47 -0300 Subject: [PATCH] #1528: a formula's references are outlined only on the sheet they belong to While pointing at another sheet, a reference without a sheet name (A1) was outlined on the sheet being shown instead of the sheet the edit started on. Co-Authored-By: Claude Sonnet 5.5 --- crates/ui-egui/src/editor.rs | 17 +++++++++++++++++ crates/ui-egui/src/grid.rs | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/editor.rs b/crates/ui-egui/src/editor.rs index 4aada2953..799109f89 100644 --- a/crates/ui-egui/src/editor.rs +++ b/crates/ui-egui/src/editor.rs @@ -238,6 +238,13 @@ pub fn formula_refs(text: &str) -> Vec<(usize, usize, RangeRef, Option, out } +/// Whether a reference of the formula being edited belongs on the sheet shown: a reference with a +/// sheet name is on that sheet, one without is on the sheet the edit started on, even while +/// pointing at another sheet. +pub fn ref_is_on_sheet(ref_sheet: Option<&str>, origin: &str, shown: &str) -> bool { + ref_sheet.unwrap_or(origin).eq_ignore_ascii_case(shown) +} + /// Excel-style AutoComplete: the one distinct text entry in the same column (contiguous data /// above and below) that starts with `typed` (case-insensitive). `None` when ambiguous. pub fn column_completion(sheet: &gridcraft_engine::model::Sheet, at: CellRef, typed: &str) -> Option { @@ -329,4 +336,14 @@ mod tests { assert_eq!(r[1].2.a1(), "B2:C3"); assert_eq!(r[3].3.as_deref(), Some("Sheet2")); } + + #[test] + fn refs_are_outlined_on_their_own_sheet_only() { + // Pointing at Sheet2 while the edit started on Sheet1: `A1` stays on Sheet1, `Sheet2!B2` shows on Sheet2. + assert!(ref_is_on_sheet(None, "Sheet1", "Sheet1")); + assert!(!ref_is_on_sheet(None, "Sheet1", "Sheet2")); + assert!(ref_is_on_sheet(Some("Sheet2"), "Sheet1", "Sheet2")); + assert!(!ref_is_on_sheet(Some("Sheet2"), "Sheet1", "Sheet1")); + assert!(ref_is_on_sheet(Some("sheet2"), "Sheet1", "Sheet2"), "sheet names compare ignoring case"); + } } diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 90a39cb06..272e81c21 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -1623,8 +1623,9 @@ pub(crate) fn paint_selection( if let Some(ed) = &app.editor && ed.is_formula() { + let origin = app.session.active().and_then(|d| d.wb.sheet(ed.sheet)).map_or(sh.name.as_str(), |o| o.name.as_str()); for (_, _, r, sheet, idx) in crate::editor::formula_refs(&ed.text) { - if sheet.as_deref().is_some_and(|s| !s.eq_ignore_ascii_case(&sh.name)) { + if !crate::editor::ref_is_on_sheet(sheet.as_deref(), origin, &sh.name) { continue; } if !range_in_pane(r, rows, cols) {