From dc040322a353c4d3b300be26aa6d744abb4727d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 25 Nov 2026 11:33:13 -0500 Subject: [PATCH 1/3] feat(ui): the Script pane counts the selected characters next to Ln/Col, and one helper reads and sets a text box's cursor (#2939) --- crates/ui-egui/src/analysispane.rs | 57 ++++++++++++++++++++---------- crates/ui-egui/src/completion.rs | 6 ++-- crates/ui-egui/src/fieldmenu.rs | 2 +- crates/ui-egui/src/widgets.rs | 13 +++++++ 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index df9e83d1db..70c85f2180 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -2300,13 +2300,20 @@ fn script(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, fit: bool) { // `out.cursor_range` is None while the box isn't focused. let size = vec2(ui.available_width(), script_cursor_height(ui)); ui.allocate_ui_with_layout(size, egui::Layout::right_to_left(egui::Align::Center), |ui| { - // The row keeps only what's drawn in it: its height, with no label in it yet. + // An empty row takes no height: without this the buttons below move when the first cursor + // appears. ui.set_min_height(size.y); - let cursor = egui::widgets::text_edit::TextEditState::load(ui.ctx(), box_id).and_then(|s| s.cursor.char_range()); - if let Some(range) = cursor { + if let Some(range) = crate::widgets::cursor_range(ui.ctx(), box_id) { let (line, col) = line_col(&app.workflow.script, range.primary.index.0); - let at = crate::i18n::fmt("Ln {line}, Col {col}", &[("line", &line.to_string()), ("col", &col.to_string())]); - ui.label(egui::RichText::new(at).small().weak()).on_hover_text(tl!("The cursor's line and column")); + let selected = range.primary.index.0.abs_diff(range.secondary.index.0); + let (line, col, n) = (line.to_string(), col.to_string(), selected.to_string()); + let place = if selected > 0 { + crate::i18n::fmt("Ln {line}, Col {col} ({n} selected)", &[("line", &line), ("col", &col), ("n", &n)]) + } else { + crate::i18n::fmt("Ln {line}, Col {col}", &[("line", &line), ("col", &col)]) + }; + ui.label(egui::RichText::new(place).small().weak()) + .on_hover_text(tl!("The cursor's line and column, and how many characters are selected")); } }); ui.horizontal(|ui| { @@ -2448,9 +2455,7 @@ fn paint_stopped(ui: &Ui, out: &egui::text_edit::TextEditOutput, at: StoppedAt) /// on the next frame (as it lays the box out: [`paint_stopped`]'s caller). fn go_to_stopped(ui: &Ui, box_id: egui::Id, index: usize) { use egui::text::{CCursor, CCursorRange}; - let mut state = egui::widgets::text_edit::TextEditState::load(ui.ctx(), box_id).unwrap_or_default(); - state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(index)))); - state.store(ui.ctx(), box_id); + crate::widgets::set_cursor_range(ui.ctx(), box_id, CCursorRange::one(CCursor::new(index))); ui.memory_mut(|m| m.request_focus(box_id)); ui.data_mut(|d| d.insert_temp(box_id.with("go_to_stopped"), true)); } @@ -3126,18 +3131,14 @@ mod tests { .build_ui_state(|ui, app: &mut RustGisApp| script_pane(app, ui, &Tokens::light()), app); h.run(); let box_id = egui::Id::unique("script_text"); - let cursor = |h: &egui_kittest::Harness<'_, RustGisApp>| { - egui::widgets::text_edit::TextEditState::load(&h.ctx, box_id).and_then(|s| s.cursor.char_range()).map(|r| r.primary.index.0) - }; + let cursor = |h: &egui_kittest::Harness<'_, RustGisApp>| crate::widgets::cursor_range(&h.ctx, box_id).map(|r| r.primary.index.0); assert_ne!(cursor(&h), Some(7 + 13 + 23)); h.get_by_label(&e).click(); h.run(); assert_eq!(cursor(&h), Some(7 + 13 + 23), "line 3, column 24"); assert!(h.ctx.memory(|m| m.has_focus(box_id))); // From the keyboard: the error takes the focus (Tab), and Enter goes there too. - let mut state = egui::widgets::text_edit::TextEditState::load(&h.ctx, box_id).unwrap_or_default(); - state.cursor.set_char_range(Some(egui::text::CCursorRange::one(egui::text::CCursor::new(0)))); - state.store(&h.ctx, box_id); + crate::widgets::set_cursor_range(&h.ctx, box_id, egui::text::CCursorRange::one(egui::text::CCursor::new(0))); h.get_by_label(&e).focus(); h.run(); assert_eq!(cursor(&h), Some(0), "moved away first"); @@ -3193,15 +3194,17 @@ mod tests { .with_size(vec2(300.0, 800.0)) .build_ui_state(|ui, app: &mut RustGisApp| script_pane(app, ui, &Tokens::light()), app); h.run(); + // Any label of the row. The exact text is pinned below, once the box has a cursor. assert!(h.query_by_label_contains("Ln ").is_none(), "no cursor yet"); let no_cursor = h.get_by_label("Clear").rect(); let box_id = egui::Id::unique("script_text"); let set = |h: &mut egui_kittest::Harness<'_, RustGisApp>, range: CCursorRange| { - let mut state = egui::widgets::text_edit::TextEditState::load(&h.ctx, box_id).unwrap_or_default(); - state.cursor.set_char_range(Some(range)); - state.store(&h.ctx, box_id); + crate::widgets::set_cursor_range(&h.ctx, box_id, range); h.run(); }; + // The first position, once the box has a cursor: no selection, so no count. + set(&mut h, CCursorRange::one(CCursor::new(0))); + assert!(h.query_by_label("Ln 1, Col 1").is_some(), "the first position"); // On the wrapped line, past its first row: one line, its column from the line's start. set(&mut h, CCursorRange::one(CCursor::new(13 + 250))); assert!(h.query_by_label("Ln 2, Col 251").is_some(), "on a wrapped line"); @@ -3224,7 +3227,25 @@ mod tests { assert!(h.query_by_label("Ln 1, Col 6").is_some(), "typed"); // A selection made backwards, from line 2 up to line 1: where its caret is, not its end. set(&mut h, CCursorRange { primary: CCursor::new(1), secondary: CCursor::new(14 + 4), h_pos: None }); - assert!(h.query_by_label("Ln 1, Col 2").is_some(), "a selection's caret"); + assert!( + h.query_by_label("Ln 1, Col 2 (17 selected)").is_some(), + "a selection's caret, its count to the other end, a line break in it" + ); + let label = h.get_by_label("Ln 1, Col 2 (17 selected)").rect(); + let clear = h.get_by_label("Clear").rect(); + assert!( + !label.intersects(clear) && label.bottom() <= clear.top(), + "the longer label still clear of the buttons: {label:?} {clear:?}" + ); + // Forwards, within a line; then all of it. + set(&mut h, CCursorRange { primary: CCursor::new(5), secondary: CCursor::new(1), h_pos: None }); + assert!(h.query_by_label("Ln 1, Col 6 (4 selected)").is_some(), "forwards"); + let script = h.state().workflow.script.clone(); + let all = script.chars().count(); + let last_line = script.rsplit('\n').next().map_or(0, |l| l.chars().count()); + set(&mut h, CCursorRange { primary: CCursor::new(all), secondary: CCursor::new(0), h_pos: None }); + let lines = script.matches('\n').count() + 1; + assert!(h.query_by_label(&format!("Ln {lines}, Col {} ({all} selected)", last_line + 1)).is_some(), "everything"); } /// Details… opens the last run in a window of its own, each line folded: a result the pane cuts diff --git a/crates/ui-egui/src/completion.rs b/crates/ui-egui/src/completion.rs index 5a26d500e7..f69a91e35c 100644 --- a/crates/ui-egui/src/completion.rs +++ b/crates/ui-egui/src/completion.rs @@ -9,7 +9,7 @@ //! is each language's. use egui::text::CCursor; -use egui::widgets::text_edit::{TextEditOutput, TextEditState}; +use egui::widgets::text_edit::TextEditOutput; use egui::{Id, Key, Modifiers, Ui}; use std::collections::HashMap; use std::sync::Arc; @@ -309,7 +309,7 @@ impl Asked { if !ui.memory(|m| m.has_focus(id)) || !ui.input_mut(|i| i.consume_key(Modifiers::CTRL, Key::Space)) { return false; } - let at = TextEditState::load(ui.ctx(), id).and_then(|s| s.cursor.char_range()).map(|r| r.primary.index.0); + let at = crate::widgets::cursor_range(ui.ctx(), id).map(|r| r.primary.index.0); self.0 = at.map(|at| (egui::util::hash(text), at)); true } @@ -1059,7 +1059,7 @@ pub(crate) fn before(ui: &Ui, box_id: Id, text: &str) -> Option { if !ui.memory(|m| m.has_focus(box_id)) { return None; } - let at = TextEditState::load(ui.ctx(), box_id)?.cursor.char_range().filter(|r| r.is_empty())?.primary.index.0; + let at = crate::widgets::cursor_range(ui.ctx(), box_id).filter(|r| r.is_empty())?.primary.index.0; Some(Before { at, prev: at.checked_sub(1).and_then(|i| char_at(text, i)), len: text.len() }) } diff --git a/crates/ui-egui/src/fieldmenu.rs b/crates/ui-egui/src/fieldmenu.rs index 0a6341d2a4..e642d00155 100644 --- a/crates/ui-egui/src/fieldmenu.rs +++ b/crates/ui-egui/src/fieldmenu.rs @@ -109,7 +109,7 @@ pub fn play(app: &mut RustGisApp, ctx: &Context) { if ctx.input(|i| i.pointer.button_pressed(PointerButton::Secondary)) { let hovered: Vec = ctx.interaction_snapshot(|s| s.hovered.iter().copied().collect()); menu.kept = hovered.into_iter().find_map(|id| { - let range = TextEditState::load(ctx, id)?.cursor.char_range()?; + let range = crate::widgets::cursor_range(ctx, id)?; (!range.is_empty()).then_some((id, range)) }); } diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 91945e5656..56c5db9efe 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -139,6 +139,19 @@ pub(crate) fn changed(r: &Response) -> bool { r.changed() && !escaped(r) } +/// The cursor (and selection) of the text box `id`, in characters; `None` before it has one. +pub(crate) fn cursor_range(ctx: &egui::Context, id: egui::Id) -> Option { + egui::widgets::text_edit::TextEditState::load(ctx, id)?.cursor.char_range() +} + +/// Puts the cursor (and selection) of the text box `id` at `range`, keeping what else its state +/// holds (the undo history). +pub(crate) fn set_cursor_range(ctx: &egui::Context, id: egui::Id, range: egui::text::CCursorRange) { + let mut state = egui::widgets::text_edit::TextEditState::load(ctx, id).unwrap_or_default(); + state.cursor.set_char_range(Some(range)); + state.store(ctx, id); +} + pub fn text_w(ui: &Ui, s: &str, font: egui::FontId) -> f32 { ui.ctx().fonts_mut(|f| f.layout_no_wrap(s.to_string(), font, egui::Color32::WHITE).size().x) } From f9cf60a91d8f9b11d0e1e766358617f82387ae99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 25 Nov 2026 12:09:55 -0500 Subject: [PATCH 2/3] test(ui): a CRLF line break counts two in the Script pane's selected count (#2939) --- crates/ui-egui/src/analysispane.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 70c85f2180..43a9177620 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -3246,6 +3246,10 @@ mod tests { set(&mut h, CCursorRange { primary: CCursor::new(all), secondary: CCursor::new(0), h_pos: None }); let lines = script.matches('\n').count() + 1; assert!(h.query_by_label(&format!("Ln {lines}, Col {} ({all} selected)", last_line + 1)).is_some(), "everything"); + // A script loaded from a Windows file: the count is the cursor's, so the line break is two. + h.state_mut().workflow.script = "a\r\nb".into(); + set(&mut h, CCursorRange { primary: CCursor::new(4), secondary: CCursor::new(0), h_pos: None }); + assert!(h.query_by_label("Ln 2, Col 2 (4 selected)").is_some(), "a CRLF line break counts two"); } /// Details… opens the last run in a window of its own, each line folded: a result the pane cuts From 423d9052c63fad1191f6a9a76634b5ab139d9a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 25 Nov 2026 12:21:36 -0500 Subject: [PATCH 3/3] test(ui): the expression and script completion tests set the cursor through the shared helper (#2939) --- crates/ui-egui/src/exprcomplete.rs | 16 ++++------------ crates/ui-egui/src/scriptcomplete.rs | 4 +--- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/crates/ui-egui/src/exprcomplete.rs b/crates/ui-egui/src/exprcomplete.rs index 43ad6953c5..05cd001ee8 100644 --- a/crates/ui-egui/src/exprcomplete.rs +++ b/crates/ui-egui/src/exprcomplete.rs @@ -1217,9 +1217,7 @@ mod tests { h.run(); h.ctx.memory_mut(|m| m.request_focus(id)); let at = |h: &mut egui_kittest::Harness<'_, String>, i: usize| { - let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default(); - state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(i)))); - state.store(&h.ctx, id); + crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::one(CCursor::new(i))); h.run(); }; let shows = |h: &egui_kittest::Harness<'_, String>, sig: &str| h.query_by_role_and_label(Role::Label, sig).is_some(); @@ -1241,9 +1239,7 @@ mod tests { // Text selected in the call, then the cursor back in it: still closed. h.key_press(Key::Escape); h.run(); - let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default(); - state.cursor.set_char_range(Some(CCursorRange::two(CCursor::new(6), CCursor::new(7)))); - state.store(&h.ctx, id); + crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::two(CCursor::new(6), CCursor::new(7))); h.run(); at(&mut h, 7); assert!(!shows(&h, round), "a selection in the call keeps it closed"); @@ -1493,9 +1489,7 @@ mod tests { let mut h = boxed(id, Some(ds()), &start); h.run(); h.ctx.memory_mut(|m| m.request_focus(id)); - let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default(); - state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(start.chars().count())))); - state.store(&h.ctx, id); + crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::one(CCursor::new(start.chars().count()))); h.run(); h.event(egui::Event::Text("o".into())); h.run(); @@ -1604,9 +1598,7 @@ mod tests { let mut h = boxed(id, None, "x"); h.run(); h.ctx.memory_mut(|m| m.request_focus(id)); - let mut state = TextEditState::load(&h.ctx, id).unwrap_or_default(); - state.cursor.set_char_range(Some(CCursorRange::one(CCursor::new(1)))); - state.store(&h.ctx, id); + crate::widgets::set_cursor_range(&h.ctx, id, CCursorRange::one(CCursor::new(1))); h.run(); h.event(egui::Event::Text("(".into())); h.run(); diff --git a/crates/ui-egui/src/scriptcomplete.rs b/crates/ui-egui/src/scriptcomplete.rs index dfa2d2eceb..cab877c95e 100644 --- a/crates/ui-egui/src/scriptcomplete.rs +++ b/crates/ui-egui/src/scriptcomplete.rs @@ -2883,10 +2883,8 @@ mod tests { assert!(h.query_by_role_and_label(Role::Label, "round(x[, places])").is_some(), "another command's call"); // Straight back into the first command's braces (a click there). let id = egui::Id::unique("script_text"); - let mut state = egui::widgets::text_edit::TextEditState::load(&h.ctx, id).unwrap_or_default(); let back = egui::text::CCursorRange::one(egui::text::CCursor::new("labels.style {".chars().count())); - state.cursor.set_char_range(Some(back)); - state.store(&h.ctx, id); + crate::widgets::set_cursor_range(&h.ctx, id, back); h.run(); h.get_by_label(&styled); }