mirror of
https://github.com/openai/codex.git
synced 2026-11-28 07:32:19 +00:00
Hang soft-break whitespace in the TUI composer (#39022)
## Why Keeping overflowing separators with the following word can add whitespace-only rows and make composer wrapping differ from the queued-input preview. ## What changed - Hang breakable whitespace from the preceding visual row at soft word breaks while preserving the underlying editable text. - Keep indentation, trailing whitespace, mandatory breaks, and nonbreaking spaces visible and editable according to their existing semantics. - Align cursor placement, scrolling, vertical navigation, masked rendering, and styled overlays with the visible wrapped content, including after resizes. ## Testing Add focused wrapping, rendering, cursor, editing, scrolling, navigation, Unicode separator, long whitespace-run, and resize coverage. GitOrigin-RevId: 7fbdb0a4bf33cfee20c4c19dca5aee74df4f892c
This commit is contained in:
1 parent
09bc28f348
commit
0841d9bba5
12 files changed
+572
-140
No files matched your search
@@ -11,8 +11,8 @@
|
||||
//! recent killed span.
|
||||
//!
|
||||
//! Wrapping also reserves a visible insertion point: full logical lines get continuation rows,
|
||||
//! and overflowing spaces wrap instead of moving the cursor outside the textarea. A partial
|
||||
//! whitespace continuation stays attached to the following word rather than becoming a blank row.
|
||||
//! and trailing spaces wrap instead of moving the cursor outside the textarea. At soft word
|
||||
//! breaks, interior separators hang off the preceding row without changing the editable text.
|
||||
|
||||
use crate::key_hint::KeyBindingListExt;
|
||||
use crate::key_hint::is_altgr;
|
||||
@@ -451,16 +451,13 @@ impl TextArea {
|
||||
}
|
||||
|
||||
let lines = self.wrapped_lines(area.width);
|
||||
let effective_scroll = self.effective_scroll(area.height, &lines, state.scroll);
|
||||
let i = Self::wrapped_line_index_by_start(&lines, self.cursor_pos)?;
|
||||
let ls = &lines[i];
|
||||
let col = display_width(&self.text[ls.start..self.cursor_pos])
|
||||
.min(usize::from(area.width.saturating_sub(1))) as u16;
|
||||
let effective_scroll = self.effective_scroll(area, &lines, state.scroll);
|
||||
let (i, col) = wrapping::cursor_position(&self.text, &lines, area.width, self.cursor_pos)?;
|
||||
let screen_row = i
|
||||
.saturating_sub(effective_scroll as usize)
|
||||
.try_into()
|
||||
.unwrap_or(0);
|
||||
Some((area.x + col, area.y + screen_row))
|
||||
Some((area.x + col as u16, area.y + screen_row))
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
@@ -472,13 +469,6 @@ impl TextArea {
|
||||
display_width(&self.text[bol..self.cursor_pos])
|
||||
}
|
||||
|
||||
fn wrapped_line_index_by_start(lines: &[Range<usize>], pos: usize) -> Option<usize> {
|
||||
// partition_point returns the index of the first element for which
|
||||
// the predicate is false, i.e. the count of elements with start <= pos.
|
||||
let idx = lines.partition_point(|r| r.start <= pos);
|
||||
if idx == 0 { None } else { Some(idx - 1) }
|
||||
}
|
||||
|
||||
fn move_to_display_col_on_line(
|
||||
&mut self,
|
||||
line_start: usize,
|
||||
@@ -1250,11 +1240,16 @@ impl TextArea {
|
||||
let cache_ref = self.wrap_cache.borrow();
|
||||
if let Some(cache) = cache_ref.as_ref() {
|
||||
let lines = &cache.lines;
|
||||
if let Some(idx) = Self::wrapped_line_index_by_start(lines, self.cursor_pos) {
|
||||
if let Some((idx, col)) =
|
||||
wrapping::cursor_position(&self.text, lines, cache.width, self.cursor_pos)
|
||||
{
|
||||
let cur_range = &lines[idx];
|
||||
let target_col = self.preferred_col.unwrap_or_else(|| {
|
||||
display_width(&self.text[cur_range.start..self.cursor_pos])
|
||||
});
|
||||
// A saved column can outlive a resize. Do not land in hanging whitespace
|
||||
// that is displayed on the following row.
|
||||
let target_col = self
|
||||
.preferred_col
|
||||
.unwrap_or(col)
|
||||
.min(usize::from(cache.width.saturating_sub(1)));
|
||||
if idx > 0 {
|
||||
let prev = &lines[idx - 1];
|
||||
let line_start = prev.start;
|
||||
@@ -1316,11 +1311,13 @@ impl TextArea {
|
||||
let cache_ref = self.wrap_cache.borrow();
|
||||
if let Some(cache) = cache_ref.as_ref() {
|
||||
let lines = &cache.lines;
|
||||
if let Some(idx) = Self::wrapped_line_index_by_start(lines, self.cursor_pos) {
|
||||
let cur_range = &lines[idx];
|
||||
let target_col = self.preferred_col.unwrap_or_else(|| {
|
||||
display_width(&self.text[cur_range.start..self.cursor_pos])
|
||||
});
|
||||
if let Some((idx, col)) =
|
||||
wrapping::cursor_position(&self.text, lines, cache.width, self.cursor_pos)
|
||||
{
|
||||
let target_col = self
|
||||
.preferred_col
|
||||
.unwrap_or(col)
|
||||
.min(usize::from(cache.width.saturating_sub(1)));
|
||||
if idx + 1 < lines.len() {
|
||||
let next = &lines[idx + 1];
|
||||
let line_start = next.start;
|
||||
@@ -1926,30 +1923,24 @@ impl TextArea {
|
||||
///
|
||||
/// - Cursor is always on screen.
|
||||
/// - No scrolling if content fits in the area.
|
||||
fn effective_scroll(
|
||||
&self,
|
||||
area_height: u16,
|
||||
lines: &[Range<usize>],
|
||||
current_scroll: u16,
|
||||
) -> u16 {
|
||||
fn effective_scroll(&self, area: Rect, lines: &[Range<usize>], current_scroll: u16) -> u16 {
|
||||
let total_lines = lines.len() as u16;
|
||||
if area_height >= total_lines {
|
||||
if area.height >= total_lines {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Where is the cursor within wrapped lines? Prefer assigning boundary positions
|
||||
// (where pos equals the start of a wrapped line) to that later line.
|
||||
let cursor_line_idx =
|
||||
Self::wrapped_line_index_by_start(lines, self.cursor_pos).unwrap_or(0) as u16;
|
||||
wrapping::cursor_position(&self.text, lines, area.width, self.cursor_pos)
|
||||
.map_or(0, |(row, _)| row) as u16;
|
||||
|
||||
let max_scroll = total_lines.saturating_sub(area_height);
|
||||
let max_scroll = total_lines.saturating_sub(area.height);
|
||||
let mut scroll = current_scroll.min(max_scroll);
|
||||
|
||||
// Ensure cursor is visible within [scroll, scroll + area_height)
|
||||
if cursor_line_idx < scroll {
|
||||
scroll = cursor_line_idx;
|
||||
} else if cursor_line_idx >= scroll + area_height {
|
||||
scroll = cursor_line_idx + 1 - area_height;
|
||||
} else if cursor_line_idx >= scroll + area.height {
|
||||
scroll = cursor_line_idx + 1 - area.height;
|
||||
}
|
||||
scroll
|
||||
}
|
||||
@@ -1974,7 +1965,7 @@ impl StatefulWidgetRef for &TextArea {
|
||||
|
||||
fn render_ref(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
||||
let lines = self.wrapped_lines(area.width);
|
||||
let scroll = self.effective_scroll(area.height, &lines, state.scroll);
|
||||
let scroll = self.effective_scroll(area, &lines, state.scroll);
|
||||
state.scroll = scroll;
|
||||
|
||||
let start = scroll as usize;
|
||||
@@ -1992,7 +1983,7 @@ impl TextArea {
|
||||
mask_char: char,
|
||||
) {
|
||||
let lines = self.wrapped_lines(area.width);
|
||||
let scroll = self.effective_scroll(area.height, &lines, state.scroll);
|
||||
let scroll = self.effective_scroll(area, &lines, state.scroll);
|
||||
state.scroll = scroll;
|
||||
|
||||
let start = scroll as usize;
|
||||
@@ -2013,7 +2004,7 @@ impl TextArea {
|
||||
highlights: &[(Range<usize>, Style)],
|
||||
) {
|
||||
let lines = self.wrapped_lines(area.width);
|
||||
let scroll = self.effective_scroll(area.height, &lines, state.scroll);
|
||||
let scroll = self.effective_scroll(area, &lines, state.scroll);
|
||||
state.scroll = scroll;
|
||||
|
||||
let start = scroll as usize;
|
||||
@@ -2031,31 +2022,40 @@ impl TextArea {
|
||||
base_style: Style,
|
||||
highlights: &[(Range<usize>, Style)],
|
||||
) {
|
||||
let element_style = base_style.fg(Color::Cyan);
|
||||
for (row, idx) in range.enumerate() {
|
||||
let r = &lines[idx];
|
||||
let y = area.y + row as u16;
|
||||
let line_range = r.start..r.end - 1;
|
||||
let visible = wrapping::visible_prefix(&self.text[r.start..r.end - 1], area.width);
|
||||
let line_range = r.start..r.start + visible.len();
|
||||
buf.set_style(Rect::new(area.x, y, area.width, 1), base_style);
|
||||
// Draw base line with the provided style.
|
||||
buf.set_stringn(
|
||||
area.x,
|
||||
y,
|
||||
text_for_display(&self.text[line_range.clone()]),
|
||||
text_for_display(visible),
|
||||
usize::from(area.width),
|
||||
base_style,
|
||||
);
|
||||
|
||||
// Overlay styled segments for elements that intersect this line.
|
||||
for elem in &self.elements {
|
||||
// Compute overlap with displayed slice.
|
||||
let overlap_start = elem.range.start.max(line_range.start);
|
||||
let overlap_end = elem.range.end.min(line_range.end);
|
||||
// Apply search highlights last so they remain visible over styled elements.
|
||||
let overlays = self
|
||||
.elements
|
||||
.iter()
|
||||
.map(|element| (&element.range, element_style))
|
||||
.chain(highlights.iter().map(|(range, style)| (range, *style)));
|
||||
for (overlay_range, style) in overlays {
|
||||
let overlap_start = overlay_range.start.max(line_range.start);
|
||||
let overlap_end = overlay_range.end.min(line_range.end);
|
||||
if overlap_start >= overlap_end {
|
||||
continue;
|
||||
}
|
||||
let styled = &self.text[overlap_start..overlap_end];
|
||||
let x_off = display_width(&self.text[line_range.start..overlap_start]) as u16;
|
||||
let style = base_style.fg(Color::Cyan);
|
||||
let x_off = display_width(&self.text[line_range.start..overlap_start]);
|
||||
if x_off >= usize::from(area.width) {
|
||||
continue;
|
||||
}
|
||||
let x_off = x_off as u16;
|
||||
buf.set_stringn(
|
||||
area.x + x_off,
|
||||
y,
|
||||
@@ -2064,25 +2064,6 @@ impl TextArea {
|
||||
style,
|
||||
);
|
||||
}
|
||||
|
||||
// Overlay render-only highlight ranges last so transient search highlighting remains
|
||||
// visible even when it intersects attachment placeholders or other styled elements.
|
||||
for (highlight_range, style) in highlights {
|
||||
let overlap_start = highlight_range.start.max(line_range.start);
|
||||
let overlap_end = highlight_range.end.min(line_range.end);
|
||||
if overlap_start >= overlap_end {
|
||||
continue;
|
||||
}
|
||||
let highlighted = &self.text[overlap_start..overlap_end];
|
||||
let x_off = display_width(&self.text[line_range.start..overlap_start]) as u16;
|
||||
buf.set_stringn(
|
||||
area.x + x_off,
|
||||
y,
|
||||
text_for_display(highlighted),
|
||||
usize::from(area.width.saturating_sub(x_off)),
|
||||
*style,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2098,8 +2079,8 @@ impl TextArea {
|
||||
for (row, idx) in range.enumerate() {
|
||||
let r = &lines[idx];
|
||||
let y = area.y + row as u16;
|
||||
let line_range = r.start..r.end - 1;
|
||||
let masked = self.text[line_range.clone()]
|
||||
let visible = wrapping::visible_prefix(&self.text[r.start..r.end - 1], area.width);
|
||||
let masked = visible
|
||||
.graphemes(/*is_extended*/ true)
|
||||
.flat_map(|grapheme| std::iter::repeat_n(mask_char, display_width(grapheme)))
|
||||
.collect::<String>();
|
||||
@@ -3646,7 +3627,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leading_space_after_full_line_stays_with_following_text() {
|
||||
fn separator_after_full_line_hangs_before_following_text() {
|
||||
let mut t = ta_with("abad a");
|
||||
t.set_cursor(t.text().len());
|
||||
let area = Rect::new(0, 0, /*width*/ 4, /*height*/ 2);
|
||||
@@ -3659,9 +3640,9 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(t.desired_height(area.width), 2);
|
||||
assert_eq!(t.cursor_pos(area), Some((2, 1)));
|
||||
assert_eq!(t.cursor_pos(area), Some((1, 1)));
|
||||
insta::assert_snapshot!(
|
||||
"textarea_leading_space_after_full_line_stays_with_following_text",
|
||||
"textarea_separator_after_full_line_hangs_before_following_text",
|
||||
format!("cursor: {:?}\n{}", t.cursor_pos(area), terminal.backend())
|
||||
);
|
||||
|
||||
@@ -3670,12 +3651,12 @@ mod tests {
|
||||
t.set_cursor(t.text().len());
|
||||
|
||||
assert_eq!(t.desired_height(area.width), 2);
|
||||
assert_eq!(t.cursor_pos(area), Some((2, 1)));
|
||||
assert_eq!(t.cursor_pos(area), Some((1, 1)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn breakable_unicode_space_stays_with_following_text() {
|
||||
fn breakable_unicode_space_hangs_before_following_text() {
|
||||
let mut t = ta_with("abad\u{3000}abcde");
|
||||
t.set_cursor(t.text().len());
|
||||
let area = Rect::new(0, 0, /*width*/ 4, /*height*/ 3);
|
||||
@@ -3688,9 +3669,9 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(t.desired_height(area.width), 3);
|
||||
assert_eq!(t.cursor_pos(area), Some((3, 2)));
|
||||
assert_eq!(t.cursor_pos(area), Some((1, 2)));
|
||||
insta::assert_snapshot!(
|
||||
"textarea_breakable_unicode_space_stays_with_following_text",
|
||||
"textarea_breakable_unicode_space_hangs_before_following_text",
|
||||
format!("cursor: {:?}\n{}", t.cursor_pos(area), terminal.backend())
|
||||
);
|
||||
}
|
||||
@@ -3706,18 +3687,19 @@ mod tests {
|
||||
}
|
||||
|
||||
let mut t = ta_with("abad abcde xy zz");
|
||||
assert_eq!(t.desired_height(area.width), 5);
|
||||
let area = Rect::new(0, 0, /*width*/ 4, /*height*/ 4);
|
||||
assert_eq!(t.desired_height(area.width), 4);
|
||||
for (cursor, expected) in [
|
||||
(4, (0, 1)),
|
||||
(5, (1, 1)),
|
||||
(8, (0, 2)),
|
||||
(9, (1, 2)),
|
||||
(10, (2, 2)),
|
||||
(11, (0, 3)),
|
||||
(12, (1, 3)),
|
||||
(13, (2, 3)),
|
||||
(14, (0, 4)),
|
||||
(16, (2, 4)),
|
||||
(5, (0, 1)),
|
||||
(8, (3, 1)),
|
||||
(9, (0, 2)),
|
||||
(10, (1, 2)),
|
||||
(11, (2, 2)),
|
||||
(12, (3, 2)),
|
||||
(13, (0, 3)),
|
||||
(14, (0, 3)),
|
||||
(16, (2, 3)),
|
||||
] {
|
||||
t.set_cursor(cursor);
|
||||
assert_eq!(t.cursor_pos(area), Some(expected));
|
||||
@@ -3741,8 +3723,8 @@ mod tests {
|
||||
fn reflow_keeps_fitting_remainder_on_current_row() {
|
||||
for (text, width, expected_height, expected_cursor) in [
|
||||
(" -", 1, 4, (0, 3)),
|
||||
("p a b", 3, 3, (2, 2)),
|
||||
("p a b", 5, 2, (4, 1)),
|
||||
("p a b", 3, 3, (0, 2)),
|
||||
("p a b", 5, 2, (3, 1)),
|
||||
(" aaaa a ", 3, 4, (1, 3)),
|
||||
] {
|
||||
let mut t = ta_with(text);
|
||||
@@ -3752,7 +3734,10 @@ mod tests {
|
||||
assert_eq!(t.desired_height(area.width), expected_height);
|
||||
assert_eq!(t.cursor_pos(area), Some(expected_cursor));
|
||||
for range in t.wrapped_lines(width).iter() {
|
||||
assert!(display_width(&text[range.start..range.end - 1]) <= usize::from(width));
|
||||
assert!(
|
||||
display_width(text[range.start..range.end - 1].trim_end_matches(' '))
|
||||
<= usize::from(width)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3793,15 +3778,15 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn space_runs_wrap_before_later_text() {
|
||||
fn space_runs_hang_before_later_text() {
|
||||
let mut t = ta_with("abad next");
|
||||
let area = Rect::new(0, 0, /*width*/ 4, /*height*/ 4);
|
||||
let area = Rect::new(0, 0, /*width*/ 4, /*height*/ 3);
|
||||
|
||||
assert_eq!(t.desired_height(area.width), 4);
|
||||
assert_eq!(t.desired_height(area.width), 3);
|
||||
|
||||
for (cursor, expected) in [(4, (0, 1)), (6, (2, 1)), (8, (0, 2)), (9, (1, 2))] {
|
||||
for cursor in 4..=9 {
|
||||
t.set_cursor(cursor);
|
||||
assert_eq!(t.cursor_pos(area), Some(expected));
|
||||
assert_eq!(t.cursor_pos(area), Some((0, 1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4200,7 +4185,7 @@ mod tests {
|
||||
fn wrapped_navigation_with_newlines_and_spaces() {
|
||||
// Include spaces and an explicit newline to exercise boundaries
|
||||
let mut t = ta_with("word1 word2\nword3");
|
||||
// Width 6 wraps "word1 " and " word2" before the newline.
|
||||
// Width 6 leaves both separators on the first visual row.
|
||||
let _ = t.desired_height(/*width*/ 6);
|
||||
|
||||
// Put the cursor at column 1 of "word2".
|
||||
@@ -4209,20 +4194,16 @@ mod tests {
|
||||
|
||||
// Up moves directly to the preceding visual line while preserving the visual column.
|
||||
t.move_cursor_up();
|
||||
assert_eq!(t.cursor(), 2);
|
||||
assert_eq!(t.cursor(), 1);
|
||||
|
||||
// Down returns to the same visual column of " word2".
|
||||
// Down returns to the same visual column of "word2".
|
||||
t.move_cursor_down();
|
||||
assert_eq!(t.cursor(), start_word2 + 1);
|
||||
|
||||
// A full line reserves an insertion row immediately before the explicit newline.
|
||||
t.move_cursor_down();
|
||||
assert_eq!(t.cursor(), t.text().find('\n').unwrap());
|
||||
|
||||
// Down again crosses the logical newline to the next visual line ("word3").
|
||||
// Down crosses the logical newline to the next visual line ("word3").
|
||||
t.move_cursor_down();
|
||||
let start_word3 = t.text().find("word3").unwrap();
|
||||
assert!(t.cursor() >= start_word3 && t.cursor() <= start_word3 + "word3".len());
|
||||
assert_eq!(t.cursor(), start_word3 + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user