Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Support Shift-click to extend transcript selections (#47414)

Eric Traut39598ed17885
diff --git a/codex-rs/tui/src/transcript_view/input.rs b/codex-rs/tui/src/transcript_view/input.rs
index 1ce32a9f41..a116a2f9c3 100644
--- a/codex-rs/tui/src/transcript_view/input.rs
+++ b/codex-rs/tui/src/transcript_view/input.rs
@@ -1,5 +1,6 @@
//! Transcript gestures leave ordinary typing and composer editing with the existing input path.
//! Stationary link clicks open on release; dragging or scrolling keeps the gesture in selection.
+//! Shift-click extends the existing selection from its original text unit.
use crate::key_hint::KeyBindingListExt;
use crossterm::event::KeyCode;
@@ -353,6 +354,15 @@ impl TranscriptView {
.link_at(visible.row, event.column.saturating_sub(self.area.x))
.map(ViewAction::OpenLink);
}
+ if event.modifiers == KeyModifiers::SHIFT && self.has_selection_range() {
+ self.last_click = None;
+ self.extend_selection(event.column, event.row);
+ if let Some(selection) = &mut self.selection {
+ selection.dragging = true;
+ selection.pressed_link = None;
+ }
+ return Some(ViewAction::Changed);
+ }
let clicks =
crate::text_selection::click_count(&mut self.last_click, event.column, event.row);
if clicks >= 2
diff --git a/codex-rs/tui/src/transcript_view/input_tests.rs b/codex-rs/tui/src/transcript_view/input_tests.rs
index 0480b9b080..92e0719ce2 100644
--- a/codex-rs/tui/src/transcript_view/input_tests.rs
+++ b/codex-rs/tui/src/transcript_view/input_tests.rs
@@ -1,4 +1,4 @@
-//! Link activation shares pointer gestures with selection without opening after a drag or scroll.
+//! Link activation and selection extension share pointer gestures without losing selection anchors.
use super::*;
use crate::history_cell::AgentMarkdownCell;
@@ -24,6 +24,98 @@ fn mouse(kind: MouseEventKind, column: u16, row: u16) -> MouseEvent {
}
}
+#[test]
+fn shift_click_extends_a_double_clicked_word_in_both_directions() {
+ let (mut view, cells) = transcript("alpha beta gamma", /*width*/ 24);
+ let area = view.area;
+ let mut buffer = Buffer::empty(area);
+ for _ in 0..2 {
+ if let Some((at, ..)) = &mut view.last_click {
+ *at = std::time::Instant::now();
+ }
+ for kind in [
+ MouseEventKind::Down(MouseButton::Left),
+ MouseEventKind::Up(MouseButton::Left),
+ ] {
+ view.handle_mouse(mouse(kind, /*column*/ 11, /*row*/ 1), &cells);
+ view.render(area, &mut buffer, &cells);
+ }
+ }
+ assert_eq!(view.selected_text(&cells).as_deref(), Some("beta"));
+
+ let mut frames = Vec::new();
+ for (column, expected) in [(17, "beta gamma"), (5, "alpha beta"), (17, "beta gamma")] {
+ for kind in [
+ MouseEventKind::Down(MouseButton::Left),
+ MouseEventKind::Up(MouseButton::Left),
+ ] {
+ let action = view.handle_mouse(
+ MouseEvent {
+ modifiers: KeyModifiers::SHIFT,
+ ..mouse(kind, column, /*row*/ 1)
+ },
+ &cells,
+ );
+ assert!(matches!(action, Some(ViewAction::Changed)));
+ view.render(area, &mut buffer, &cells);
+ assert_eq!(view.selected_text(&cells).as_deref(), Some(expected));
+ }
+ frames.push(format!("{buffer:?}"));
+ }
+ // An ordinary click after extending still starts a fresh, empty selection.
+ for kind in [
+ MouseEventKind::Down(MouseButton::Left),
+ MouseEventKind::Up(MouseButton::Left),
+ ] {
+ view.handle_mouse(mouse(kind, /*column*/ 17, /*row*/ 1), &cells);
+ }
+ assert_eq!(view.selected_text(&cells), None);
+ insta::assert_snapshot!(frames.join("\n\n"));
+}
+
+#[test]
+fn shift_click_preserves_selection_units_and_allows_dragging() {
+ for (clicks, expected) in [(1, "beta gam"), (3, "alpha beta gamma")] {
+ let (mut view, cells) = transcript("alpha beta gamma", /*width*/ 24);
+ view.begin_selection(&cells, /*column*/ 10, /*row*/ 1, clicks);
+ view.handle_mouse(
+ mouse(
+ MouseEventKind::Drag(MouseButton::Left),
+ /*column*/ 14,
+ /*row*/ 1,
+ ),
+ &cells,
+ );
+ view.handle_mouse(
+ mouse(
+ MouseEventKind::Up(MouseButton::Left),
+ /*column*/ 14,
+ /*row*/ 1,
+ ),
+ &cells,
+ );
+ for kind in [
+ MouseEventKind::Down(MouseButton::Left),
+ MouseEventKind::Drag(MouseButton::Left),
+ MouseEventKind::Up(MouseButton::Left),
+ ] {
+ let column = if matches!(kind, MouseEventKind::Down(_)) {
+ 16
+ } else {
+ 18
+ };
+ view.handle_mouse(
+ MouseEvent {
+ modifiers: KeyModifiers::SHIFT,
+ ..mouse(kind, column, /*row*/ 1)
+ },
+ &cells,
+ );
+ }
+ assert_eq!(view.selected_text(&cells).as_deref(), Some(expected));
+ }
+}
+
#[test]
fn ordinary_click_opens_bare_and_markdown_links_on_release() {
for markdown in [
diff --git a/codex-rs/tui/src/transcript_view/selection.rs b/codex-rs/tui/src/transcript_view/selection.rs
index 62cea72f45..602af42546 100644
--- a/codex-rs/tui/src/transcript_view/selection.rs
+++ b/codex-rs/tui/src/transcript_view/selection.rs
@@ -115,6 +115,11 @@ impl TranscriptView {
let Some(mut selection) = self.selection.take() else {
return;
};
+ // A Shift-click starts a new pointer gesture while retaining the text anchor.
+ if !selection.dragging {
+ selection.pointer_origin_row = row;
+ selection.moved_vertically = false;
+ }
let cells = Arc::clone(&selection.snapshot.cells);
let range = selection.unit.range(layout.text(), end.offset);
let origin = selection.origin;
diff --git a/codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__input__tests__shift_click_extends_a_double_clicked_word_in_both_directions.snap b/codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__input__tests__shift_click_extends_a_double_clicked_word_in_both_directions.snap
new file mode 100644
index 0000000000..0a106ea488
--- /dev/null
+++ b/codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__input__tests__shift_click_extends_a_double_clicked_word_in_both_directions.snap
@@ -0,0 +1,63 @@
+---
+source: tui/src/transcript_view/input_tests.rs
+assertion_line: 73
+expression: "frames.join(\"\\n\\n\")"
+---
+Buffer {
+ area: Rect { x: 2, y: 1, width: 24, height: 8 },
+ content: [
+ "• alpha beta gamma ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ ],
+ styles: [
+ x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: DIM,
+ x: 2, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
+ x: 8, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
+ x: 18, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
+ ]
+}
+
+Buffer {
+ area: Rect { x: 2, y: 1, width: 24, height: 8 },
+ content: [
+ "• alpha beta gamma ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ ],
+ styles: [
+ x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: DIM,
+ x: 2, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
+ x: 12, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
+ ]
+}
+
+Buffer {
+ area: Rect { x: 2, y: 1, width: 24, height: 8 },
+ content: [
+ "• alpha beta gamma ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ " ",
+ ],
+ styles: [
+ x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: DIM,
+ x: 2, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
+ x: 8, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: REVERSED,
+ x: 18, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
+ ]
+}