mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
Expand keyboard navigation and selection in the transcript (#57221)
## What changed - Add word motions (`w`, `b`, `e`), logical line boundaries, page and half-page motions, and jumps to the transcript's beginning and end. Honor configured pager bindings for navigation. - Add whole-line selection with `V` and active-end swapping with `o`. - Adopt existing mouse selections when entering select mode, preserving selection units and endpoint bounds so keyboard motions can adjust the range. ## Testing Add regression tests for word motion across entries, reverse whole-line selection, empty-line boundaries, configured pager motion, final-grapheme selection, endpoint swapping, and caret placement after copying. GitOrigin-RevId: f0cd05900f2a7a15840777cee4d50659a2c8b134
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
//! Basic cursor motions and marked-range derivation for keyboard transcript selection.
|
||||
//! Cursor motions and marked-range derivation for keyboard transcript selection.
|
||||
|
||||
use super::*;
|
||||
use crossterm::event::KeyCode;
|
||||
@@ -13,7 +13,9 @@ impl TranscriptView {
|
||||
let Some(mut mode) = self.select_mode else {
|
||||
return;
|
||||
};
|
||||
self.cancel_beginning();
|
||||
if !matches!(motion, CursorMotion::Beginning) {
|
||||
self.cancel_beginning();
|
||||
}
|
||||
let Some(selection) = self.selection.as_ref() else {
|
||||
return;
|
||||
};
|
||||
@@ -39,6 +41,32 @@ impl TranscriptView {
|
||||
mode.preferred_column = column;
|
||||
self.move_select_rows(cells, cursor, rows, column)
|
||||
}
|
||||
CursorMotion::WordForward => {
|
||||
mode.preferred_column = None;
|
||||
self.move_select_word(
|
||||
cells, cursor, /*backward*/ false, /*to_end*/ false,
|
||||
)
|
||||
}
|
||||
CursorMotion::WordBackward => {
|
||||
mode.preferred_column = None;
|
||||
self.move_select_word(cells, cursor, /*backward*/ true, /*to_end*/ false)
|
||||
}
|
||||
CursorMotion::WordEnd => {
|
||||
mode.preferred_column = None;
|
||||
self.move_select_word(cells, cursor, /*backward*/ false, /*to_end*/ true)
|
||||
}
|
||||
CursorMotion::LineStart | CursorMotion::LineEnd => {
|
||||
mode.preferred_column = None;
|
||||
self.move_select_line(cells, cursor, matches!(motion, CursorMotion::LineEnd))
|
||||
}
|
||||
CursorMotion::Beginning | CursorMotion::End => {
|
||||
mode.preferred_column = None;
|
||||
if matches!(motion, CursorMotion::Beginning) && self.history.has_unloaded_history()
|
||||
{
|
||||
self.jump_to_beginning(cells);
|
||||
}
|
||||
self.select_boundary(cells, matches!(motion, CursorMotion::End))
|
||||
}
|
||||
};
|
||||
let Some(next) = next else {
|
||||
self.select_mode = Some(mode);
|
||||
@@ -46,6 +74,7 @@ impl TranscriptView {
|
||||
};
|
||||
if let Some(mark) = &mut mode.mark {
|
||||
mark.active = next;
|
||||
mark.active_edges = None;
|
||||
} else if let Some(selection) = &mut self.selection {
|
||||
selection.set_keyboard_range(next, next);
|
||||
}
|
||||
@@ -140,6 +169,96 @@ impl TranscriptView {
|
||||
Some(cursor)
|
||||
}
|
||||
|
||||
fn move_select_word(
|
||||
&mut self,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
cursor: Anchor,
|
||||
backward: bool,
|
||||
to_end: bool,
|
||||
) -> Option<Anchor> {
|
||||
let mut index = self.resolve(cells, cursor);
|
||||
let origin_index = index;
|
||||
loop {
|
||||
let layout = self.layout(cells, index)?;
|
||||
let text = layout.text();
|
||||
let offset = if index == origin_index {
|
||||
text.floor_char_boundary(cursor.offset.min(text.len()))
|
||||
} else if backward {
|
||||
text.len()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let mut words = text
|
||||
.split_word_bound_indices()
|
||||
.filter(|(_, word)| word.chars().any(char::is_alphanumeric));
|
||||
let found = if backward {
|
||||
words
|
||||
.rfind(|(start, _)| *start < offset)
|
||||
.map(|(start, _)| start)
|
||||
} else if to_end {
|
||||
words
|
||||
.filter_map(|(start, word)| {
|
||||
text[..start + word.len()]
|
||||
.grapheme_indices(/*is_extended*/ true)
|
||||
.next_back()
|
||||
.map(|(offset, _)| offset)
|
||||
})
|
||||
.find(|end| index != origin_index || *end > offset)
|
||||
} else {
|
||||
words
|
||||
.find(|(start, _)| {
|
||||
index != origin_index
|
||||
|| *start > offset
|
||||
|| (cursor.row_bias > 0 && *start == offset)
|
||||
})
|
||||
.map(|(start, _)| start)
|
||||
};
|
||||
if let Some(offset) = found {
|
||||
return Some(Anchor {
|
||||
key: self.entry_key(cells, index),
|
||||
index,
|
||||
offset,
|
||||
row_bias: 0,
|
||||
});
|
||||
}
|
||||
if backward {
|
||||
index = index.checked_sub(1)?;
|
||||
} else {
|
||||
index += 1;
|
||||
self.layout(cells, index)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn move_select_line(
|
||||
&mut self,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
cursor: Anchor,
|
||||
end: bool,
|
||||
) -> Option<Anchor> {
|
||||
let index = self.resolve(cells, cursor);
|
||||
let layout = self.layout(cells, index)?;
|
||||
let range = SelectionUnit::Line.range(layout.text(), cursor.offset);
|
||||
let offset = if end {
|
||||
let end = range.end.saturating_sub(usize::from(
|
||||
range.end > range.start
|
||||
&& layout.text().as_bytes().get(range.end - 1) == Some(&b'\n'),
|
||||
));
|
||||
layout.text()[range.start..end]
|
||||
.grapheme_indices(/*is_extended*/ true)
|
||||
.next_back()
|
||||
.map_or(range.start, |(offset, _)| range.start + offset)
|
||||
} else {
|
||||
range.start
|
||||
};
|
||||
Some(Anchor {
|
||||
key: self.entry_key(cells, index),
|
||||
index,
|
||||
offset,
|
||||
row_bias: 0,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn select_boundary(
|
||||
&mut self,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
@@ -185,21 +304,56 @@ impl TranscriptView {
|
||||
let cells = snapshot.as_deref().unwrap_or(cells);
|
||||
let after_anchor = self.after_grapheme(cells, mark.anchor);
|
||||
let after_active = self.after_grapheme(cells, mark.active);
|
||||
let origin = match mark.unit {
|
||||
_ if mark.anchor_edges.is_some() => mark.anchor_edges,
|
||||
SelectionUnit::Character => Some((mark.anchor, after_anchor)),
|
||||
SelectionUnit::Word | SelectionUnit::Line => {
|
||||
self.selection_bounds(cells, mark.anchor, mark.unit)
|
||||
}
|
||||
};
|
||||
let Some(origin) = origin else { return };
|
||||
let active_line = (mark.unit == SelectionUnit::Line)
|
||||
.then(|| self.selection_bounds(cells, mark.active, SelectionUnit::Line))
|
||||
.flatten();
|
||||
let Some(mut selection) = self.selection.take() else {
|
||||
return;
|
||||
};
|
||||
let (start, end) =
|
||||
if self.anchor_order(cells, mark.anchor) <= self.anchor_order(cells, mark.active) {
|
||||
(mark.anchor, after_active)
|
||||
} else {
|
||||
(after_anchor, mark.active)
|
||||
};
|
||||
selection.set_keyboard_marked_range(
|
||||
start,
|
||||
end,
|
||||
(mark.anchor, after_anchor),
|
||||
SelectionUnit::Character,
|
||||
);
|
||||
let anchor_order = self.anchor_order(cells, mark.anchor);
|
||||
let active_order = self.anchor_order(cells, mark.active);
|
||||
let forward = anchor_order <= active_order;
|
||||
let (start, end) = match mark.unit {
|
||||
SelectionUnit::Character | SelectionUnit::Word => {
|
||||
if forward {
|
||||
(
|
||||
origin.0,
|
||||
mark.active_edges.map_or(after_active, |edges| edges.1),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
mark.active_edges.map_or(mark.active, |edges| edges.0),
|
||||
origin.1,
|
||||
)
|
||||
}
|
||||
}
|
||||
SelectionUnit::Line => {
|
||||
let Some(active) = active_line else {
|
||||
self.selection = Some(selection);
|
||||
return;
|
||||
};
|
||||
if forward {
|
||||
(
|
||||
origin.0,
|
||||
mark.active_edges.map_or(active.1, |edges| edges.1),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
mark.active_edges.map_or(active.0, |edges| edges.0),
|
||||
origin.1,
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
selection.set_keyboard_marked_range(start, end, origin, mark.unit);
|
||||
self.pin_selection_range(cells, &mut selection);
|
||||
self.selection = Some(selection);
|
||||
}
|
||||
@@ -224,7 +378,57 @@ impl TranscriptView {
|
||||
}
|
||||
}
|
||||
|
||||
fn anchor_order(&self, cells: &[Arc<dyn HistoryCell>], anchor: Anchor) -> (usize, usize) {
|
||||
pub(super) fn before_grapheme(
|
||||
&mut self,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
anchor: Anchor,
|
||||
) -> Anchor {
|
||||
self.move_select_character(cells, anchor, KeyCode::Left)
|
||||
.unwrap_or(anchor)
|
||||
}
|
||||
|
||||
pub(super) fn selection_endpoint_bounds(
|
||||
&mut self,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
anchor: Anchor,
|
||||
unit: SelectionUnit,
|
||||
) -> Option<(Anchor, Anchor)> {
|
||||
if unit == SelectionUnit::Character {
|
||||
return Some((anchor, self.after_grapheme(cells, anchor)));
|
||||
}
|
||||
self.selection_bounds(cells, anchor, unit)
|
||||
}
|
||||
|
||||
fn selection_bounds(
|
||||
&mut self,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
anchor: Anchor,
|
||||
unit: SelectionUnit,
|
||||
) -> Option<(Anchor, Anchor)> {
|
||||
let index = self.resolve(cells, anchor);
|
||||
let layout = self.layout(cells, index)?;
|
||||
let range = unit.range(layout.text(), anchor.offset);
|
||||
Some((
|
||||
Anchor {
|
||||
key: self.entry_key(cells, index),
|
||||
index,
|
||||
offset: range.start,
|
||||
row_bias: 0,
|
||||
},
|
||||
Anchor {
|
||||
key: self.entry_key(cells, index),
|
||||
index,
|
||||
offset: range.end,
|
||||
row_bias: 0,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn anchor_order(
|
||||
&self,
|
||||
cells: &[Arc<dyn HistoryCell>],
|
||||
anchor: Anchor,
|
||||
) -> (usize, usize) {
|
||||
(self.resolve(cells, anchor), anchor.offset)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user