mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Limit inline diff previews in the TUI (#41143)
## Why Large or minified diffs can require substantial work to allocate wrapped rows and syntax-highlighted spans in the inline patch view. ## What changed - Limit inline patch previews to 12 rendered rows across all changed files, while retaining file summaries and directing users to the transcript when content is omitted. - Preserve complete diff content in transcript and raw views. - Bound preview source scanning to 64 KiB so zero-width content cannot bypass the row budget. ## Testing Add coverage for added, deleted, updated, renamed, minified, narrow-width, wide-character, combining-character, and multi-file diffs, including preservation of complete transcript and raw output. GitOrigin-RevId: 1899129ab548771c29d226ff624549573fb2f688
This commit is contained in:
1 parent
694edc23b2
commit
a847c71a15
4 files changed
+488
-16
No files matched your search
+155
-16
@@ -41,6 +41,7 @@ use ratatui::style::Stylize;
|
|||||||
use ratatui::text::Line as RtLine;
|
use ratatui::text::Line as RtLine;
|
||||||
use ratatui::text::Span as RtSpan;
|
use ratatui::text::Span as RtSpan;
|
||||||
use ratatui::widgets::Paragraph;
|
use ratatui::widgets::Paragraph;
|
||||||
|
use ratatui::widgets::Wrap;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@@ -48,6 +49,8 @@ use std::path::PathBuf;
|
|||||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||||
use unicode_width::UnicodeWidthChar;
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
|
mod preview;
|
||||||
|
|
||||||
/// Replacement for a tab character in rendered diff content.
|
/// Replacement for a tab character in rendered diff content.
|
||||||
const TAB_REPLACEMENT: &str = " ";
|
const TAB_REPLACEMENT: &str = " ";
|
||||||
/// Display width of a tab character in columns.
|
/// Display width of a tab character in columns.
|
||||||
@@ -309,13 +312,25 @@ impl DiffSummary {
|
|||||||
impl Renderable for FileChange {
|
impl Renderable for FileChange {
|
||||||
fn render(&self, area: Rect, buf: &mut Buffer) {
|
fn render(&self, area: Rect, buf: &mut Buffer) {
|
||||||
let mut lines = vec![];
|
let mut lines = vec![];
|
||||||
render_change(self, &mut lines, area.width as usize, /*lang*/ None);
|
render_change(
|
||||||
|
self,
|
||||||
|
&mut lines,
|
||||||
|
area.width as usize,
|
||||||
|
/*lang*/ None,
|
||||||
|
usize::MAX,
|
||||||
|
);
|
||||||
Paragraph::new(lines).render(area, buf);
|
Paragraph::new(lines).render(area, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn desired_height(&self, width: u16) -> u16 {
|
fn desired_height(&self, width: u16) -> u16 {
|
||||||
let mut lines = vec![];
|
let mut lines = vec![];
|
||||||
render_change(self, &mut lines, width as usize, /*lang*/ None);
|
render_change(
|
||||||
|
self,
|
||||||
|
&mut lines,
|
||||||
|
width as usize,
|
||||||
|
/*lang*/ None,
|
||||||
|
usize::MAX,
|
||||||
|
);
|
||||||
lines.len() as u16
|
lines.len() as u16
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -354,7 +369,15 @@ pub(crate) fn create_diff_summary(
|
|||||||
wrap_cols: usize,
|
wrap_cols: usize,
|
||||||
) -> Vec<RtLine<'static>> {
|
) -> Vec<RtLine<'static>> {
|
||||||
let rows = collect_rows(changes);
|
let rows = collect_rows(changes);
|
||||||
render_changes_block(rows, wrap_cols, cwd)
|
render_changes_block(rows, wrap_cols, cwd, usize::MAX)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn create_diff_preview(
|
||||||
|
changes: &HashMap<PathBuf, FileChange>,
|
||||||
|
cwd: &Path,
|
||||||
|
wrap_cols: usize,
|
||||||
|
) -> Vec<RtLine<'static>> {
|
||||||
|
render_changes_block(collect_rows(changes), wrap_cols, cwd, preview::PREVIEW_ROWS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shared row for per-file presentation
|
// Shared row for per-file presentation
|
||||||
@@ -407,7 +430,12 @@ fn render_line_count_summary(added: usize, removed: usize) -> Vec<RtSpan<'static
|
|||||||
spans
|
spans
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_changes_block(rows: Vec<Row<'_>>, wrap_cols: usize, cwd: &Path) -> Vec<RtLine<'static>> {
|
fn render_changes_block(
|
||||||
|
rows: Vec<Row<'_>>,
|
||||||
|
wrap_cols: usize,
|
||||||
|
cwd: &Path,
|
||||||
|
mut remaining_rows: usize,
|
||||||
|
) -> Vec<RtLine<'static>> {
|
||||||
let mut out: Vec<RtLine<'static>> = Vec::new();
|
let mut out: Vec<RtLine<'static>> = Vec::new();
|
||||||
|
|
||||||
let render_path = |row: &Row<'_>| -> Vec<RtSpan<'static>> {
|
let render_path = |row: &Row<'_>| -> Vec<RtSpan<'static>> {
|
||||||
@@ -443,6 +471,7 @@ fn render_changes_block(rows: Vec<Row<'_>>, wrap_cols: usize, cwd: &Path) -> Vec
|
|||||||
}
|
}
|
||||||
out.push(RtLine::from(header_spans));
|
out.push(RtLine::from(header_spans));
|
||||||
|
|
||||||
|
let mut omitted = false;
|
||||||
for (idx, r) in rows.into_iter().enumerate() {
|
for (idx, r) in rows.into_iter().enumerate() {
|
||||||
// Insert a blank separator between file chunks (except before the first)
|
// Insert a blank separator between file chunks (except before the first)
|
||||||
if idx > 0 {
|
if idx > 0 {
|
||||||
@@ -459,6 +488,11 @@ fn render_changes_block(rows: Vec<Row<'_>>, wrap_cols: usize, cwd: &Path) -> Vec
|
|||||||
out.push(RtLine::from(header));
|
out.push(RtLine::from(header));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if remaining_rows == 0 && (omitted || r.added > 0 || r.removed > 0) {
|
||||||
|
omitted = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// For renames, use the destination extension for highlighting — the
|
// For renames, use the destination extension for highlighting — the
|
||||||
// diff content reflects the new file, not the old one.
|
// diff content reflects the new file, not the old one.
|
||||||
let lang_path = r.move_path.unwrap_or(r.path);
|
let lang_path = r.move_path.unwrap_or(r.path);
|
||||||
@@ -466,10 +500,47 @@ fn render_changes_block(rows: Vec<Row<'_>>, wrap_cols: usize, cwd: &Path) -> Vec
|
|||||||
let mut lines = vec![];
|
let mut lines = vec![];
|
||||||
let prefix = " ";
|
let prefix = " ";
|
||||||
let content_width = wrap_cols.saturating_sub(prefix.len());
|
let content_width = wrap_cols.saturating_sub(prefix.len());
|
||||||
render_change(r.change, &mut lines, content_width, lang.as_deref());
|
omitted |= render_change(
|
||||||
out.extend(prefix_lines(lines, prefix.into(), prefix.into()));
|
r.change,
|
||||||
|
&mut lines,
|
||||||
|
content_width,
|
||||||
|
lang.as_deref(),
|
||||||
|
remaining_rows,
|
||||||
|
);
|
||||||
|
for line in prefix_lines(lines, prefix.into(), prefix.into()) {
|
||||||
|
if remaining_rows != usize::MAX {
|
||||||
|
let rows = Paragraph::new(line.clone())
|
||||||
|
.wrap(Wrap { trim: false })
|
||||||
|
.line_count(wrap_cols as u16);
|
||||||
|
if rows > remaining_rows {
|
||||||
|
out.extend(preview::rendered_prefix(
|
||||||
|
line,
|
||||||
|
wrap_cols as u16,
|
||||||
|
remaining_rows as u16,
|
||||||
|
));
|
||||||
|
remaining_rows = 0;
|
||||||
|
omitted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
remaining_rows -= rows;
|
||||||
|
}
|
||||||
|
out.push(line);
|
||||||
|
}
|
||||||
|
if omitted {
|
||||||
|
remaining_rows = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if omitted {
|
||||||
|
out.push(
|
||||||
|
format!(
|
||||||
|
" … Diff preview limited ({}).",
|
||||||
|
crate::ui_consts::TRANSCRIPT_HINT
|
||||||
|
)
|
||||||
|
.dim()
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -486,14 +557,27 @@ fn render_change(
|
|||||||
out: &mut Vec<RtLine<'static>>,
|
out: &mut Vec<RtLine<'static>>,
|
||||||
width: usize,
|
width: usize,
|
||||||
lang: Option<&str>,
|
lang: Option<&str>,
|
||||||
) {
|
max_rows: usize,
|
||||||
|
) -> bool {
|
||||||
|
let mut omitted = false;
|
||||||
let style_context = current_diff_render_style_context();
|
let style_context = current_diff_render_style_context();
|
||||||
match change {
|
match change {
|
||||||
FileChange::Add { content } => {
|
FileChange::Add { content } => {
|
||||||
// Pre-highlight the entire file content as a whole.
|
|
||||||
let syntax_lines = lang.and_then(|l| highlight_code_to_styled_spans(content, l));
|
|
||||||
let line_number_width = line_number_width(content.lines().count());
|
let line_number_width = line_number_width(content.lines().count());
|
||||||
for (i, raw) in content.lines().enumerate() {
|
let end = if max_rows == usize::MAX {
|
||||||
|
content.len()
|
||||||
|
} else {
|
||||||
|
preview::visible_byte_count(
|
||||||
|
content.split_inclusive('\n'),
|
||||||
|
width.saturating_sub(line_number_width + 2),
|
||||||
|
max_rows,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let visible_content = &content[..end];
|
||||||
|
omitted = visible_content.len() < content.len();
|
||||||
|
let syntax_lines =
|
||||||
|
lang.and_then(|l| highlight_code_to_styled_spans(visible_content, l));
|
||||||
|
for (i, raw) in visible_content.lines().enumerate() {
|
||||||
let syn = syntax_lines.as_ref().and_then(|sl| sl.get(i));
|
let syn = syntax_lines.as_ref().and_then(|sl| sl.get(i));
|
||||||
if let Some(spans) = syn {
|
if let Some(spans) = syn {
|
||||||
out.extend(push_wrapped_diff_line_inner_with_theme_and_color_level(
|
out.extend(push_wrapped_diff_line_inner_with_theme_and_color_level(
|
||||||
@@ -523,9 +607,21 @@ fn render_change(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
FileChange::Delete { content } => {
|
FileChange::Delete { content } => {
|
||||||
let syntax_lines = lang.and_then(|l| highlight_code_to_styled_spans(content, l));
|
|
||||||
let line_number_width = line_number_width(content.lines().count());
|
let line_number_width = line_number_width(content.lines().count());
|
||||||
for (i, raw) in content.lines().enumerate() {
|
let end = if max_rows == usize::MAX {
|
||||||
|
content.len()
|
||||||
|
} else {
|
||||||
|
preview::visible_byte_count(
|
||||||
|
content.split_inclusive('\n'),
|
||||||
|
width.saturating_sub(line_number_width + 2),
|
||||||
|
max_rows,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let visible_content = &content[..end];
|
||||||
|
omitted = visible_content.len() < content.len();
|
||||||
|
let syntax_lines =
|
||||||
|
lang.and_then(|l| highlight_code_to_styled_spans(visible_content, l));
|
||||||
|
for (i, raw) in visible_content.lines().enumerate() {
|
||||||
let syn = syntax_lines.as_ref().and_then(|sl| sl.get(i));
|
let syn = syntax_lines.as_ref().and_then(|sl| sl.get(i));
|
||||||
if let Some(spans) = syn {
|
if let Some(spans) = syn {
|
||||||
out.extend(push_wrapped_diff_line_inner_with_theme_and_color_level(
|
out.extend(push_wrapped_diff_line_inner_with_theme_and_color_level(
|
||||||
@@ -600,6 +696,9 @@ fn render_change(
|
|||||||
let line_number_width = line_number_width(max_line_number);
|
let line_number_width = line_number_width(max_line_number);
|
||||||
let mut is_first_hunk = true;
|
let mut is_first_hunk = true;
|
||||||
for h in patch.hunks() {
|
for h in patch.hunks() {
|
||||||
|
if out.len() >= max_rows {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if !is_first_hunk {
|
if !is_first_hunk {
|
||||||
let spacer = format!("{:width$} ", "", width = line_number_width.max(1));
|
let spacer = format!("{:width$} ", "", width = line_number_width.max(1));
|
||||||
let spacer_span = RtSpan::styled(
|
let spacer_span = RtSpan::styled(
|
||||||
@@ -614,11 +713,46 @@ fn render_change(
|
|||||||
}
|
}
|
||||||
is_first_hunk = false;
|
is_first_hunk = false;
|
||||||
|
|
||||||
|
let visible_bytes = if max_rows == usize::MAX {
|
||||||
|
usize::MAX
|
||||||
|
} else {
|
||||||
|
preview::visible_byte_count(
|
||||||
|
h.lines().iter().map(|line| match line {
|
||||||
|
diffy::Line::Insert(text)
|
||||||
|
| diffy::Line::Delete(text)
|
||||||
|
| diffy::Line::Context(text) => *text,
|
||||||
|
}),
|
||||||
|
width.saturating_sub(line_number_width + 2),
|
||||||
|
max_rows.saturating_sub(out.len()),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let visible_lines: Vec<_> = h
|
||||||
|
.lines()
|
||||||
|
.iter()
|
||||||
|
.scan(visible_bytes, |remaining, line| {
|
||||||
|
if *remaining == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let text = match line {
|
||||||
|
diffy::Line::Insert(text)
|
||||||
|
| diffy::Line::Delete(text)
|
||||||
|
| diffy::Line::Context(text) => *text,
|
||||||
|
};
|
||||||
|
let end = (*remaining).min(text.len());
|
||||||
|
*remaining -= end;
|
||||||
|
omitted |= end < text.len();
|
||||||
|
Some(match line {
|
||||||
|
diffy::Line::Insert(_) => diffy::Line::Insert(&text[..end]),
|
||||||
|
diffy::Line::Delete(_) => diffy::Line::Delete(&text[..end]),
|
||||||
|
diffy::Line::Context(_) => diffy::Line::Context(&text[..end]),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
// Highlight each hunk as a single block so syntect parser
|
// Highlight each hunk as a single block so syntect parser
|
||||||
// state is preserved across consecutive lines.
|
// state is preserved across consecutive lines.
|
||||||
let hunk_syntax_lines = diff_lang.and_then(|language| {
|
let hunk_syntax_lines = diff_lang.and_then(|language| {
|
||||||
let hunk_text: String = h
|
let hunk_text: String = visible_lines
|
||||||
.lines()
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|line| match line {
|
.map(|line| match line {
|
||||||
diffy::Line::Insert(text)
|
diffy::Line::Insert(text)
|
||||||
@@ -627,12 +761,12 @@ fn render_change(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let syntax_lines = highlight_code_to_styled_spans(&hunk_text, language)?;
|
let syntax_lines = highlight_code_to_styled_spans(&hunk_text, language)?;
|
||||||
(syntax_lines.len() == h.lines().len()).then_some(syntax_lines)
|
(syntax_lines.len() == visible_lines.len()).then_some(syntax_lines)
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut old_ln = h.old_range().start();
|
let mut old_ln = h.old_range().start();
|
||||||
let mut new_ln = h.new_range().start();
|
let mut new_ln = h.new_range().start();
|
||||||
for (line_idx, l) in h.lines().iter().enumerate() {
|
for (line_idx, l) in visible_lines.iter().enumerate() {
|
||||||
let syntax_spans = hunk_syntax_lines
|
let syntax_spans = hunk_syntax_lines
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|syntax_lines| syntax_lines.get(line_idx));
|
.and_then(|syntax_lines| syntax_lines.get(line_idx));
|
||||||
@@ -739,10 +873,15 @@ fn render_change(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if omitted || visible_lines.len() < h.lines().len() {
|
||||||
|
omitted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
omitted
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Format a path for display relative to the current working directory when
|
/// Format a path for display relative to the current working directory when
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
//! Bounds inline diff work before allocating wrapped rows or syntax spans.
|
||||||
|
|
||||||
|
use super::TAB_WIDTH;
|
||||||
|
use ratatui::buffer::Buffer;
|
||||||
|
use ratatui::buffer::CellWidth;
|
||||||
|
use ratatui::layout::Rect;
|
||||||
|
use ratatui::text::Line;
|
||||||
|
use ratatui::text::Span;
|
||||||
|
use ratatui::widgets::Paragraph;
|
||||||
|
use ratatui::widgets::Widget;
|
||||||
|
use ratatui::widgets::Wrap;
|
||||||
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
|
pub(super) const PREVIEW_ROWS: usize = 12;
|
||||||
|
const MAX_PREVIEW_BYTES: usize = 64 * 1024;
|
||||||
|
|
||||||
|
/// Counts a UTF-8 source prefix within a pre-render character-width budget.
|
||||||
|
/// The caller also limits final viewport rows after styling and wrapping.
|
||||||
|
/// Include the start of an oversized line, stopping its scan at either budget.
|
||||||
|
/// The byte ceiling also bounds zero-width content that consumes no columns.
|
||||||
|
/// Lines must retain their terminators so the count can slice the source.
|
||||||
|
pub(super) fn visible_byte_count<'a>(
|
||||||
|
lines: impl Iterator<Item = &'a str>,
|
||||||
|
content_width: usize,
|
||||||
|
max_rows: usize,
|
||||||
|
) -> usize {
|
||||||
|
let content_width = content_width.max(/*other*/ 1);
|
||||||
|
let mut remaining_rows = max_rows;
|
||||||
|
let mut count = 0;
|
||||||
|
for line in lines {
|
||||||
|
if remaining_rows == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let mut rows = 1;
|
||||||
|
let mut col = 0;
|
||||||
|
let end = line.floor_char_boundary(MAX_PREVIEW_BYTES - count);
|
||||||
|
let visible = &line[..end];
|
||||||
|
for (offset, ch) in visible.trim_end_matches(['\r', '\n']).char_indices() {
|
||||||
|
let width = ch.width().unwrap_or(if ch == '\t' { TAB_WIDTH } else { 0 });
|
||||||
|
if col > 0 && col + width > content_width {
|
||||||
|
rows += 1;
|
||||||
|
col = 0;
|
||||||
|
}
|
||||||
|
if rows > remaining_rows {
|
||||||
|
return count + offset;
|
||||||
|
}
|
||||||
|
col += width;
|
||||||
|
}
|
||||||
|
remaining_rows -= rows;
|
||||||
|
count += visible.len();
|
||||||
|
if end < line.len() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Keep a partial line using the viewport renderer's own grapheme and word
|
||||||
|
/// wrapping. Only the remaining preview rows are allocated; full views bypass it.
|
||||||
|
pub(super) fn rendered_prefix(
|
||||||
|
line: Line<'static>,
|
||||||
|
width: u16,
|
||||||
|
max_rows: u16,
|
||||||
|
) -> Vec<Line<'static>> {
|
||||||
|
let style = line.style;
|
||||||
|
let area = Rect::new(/*x*/ 0, /*y*/ 0, width, max_rows);
|
||||||
|
let mut buffer = Buffer::empty(area);
|
||||||
|
Paragraph::new(line)
|
||||||
|
.wrap(Wrap { trim: false })
|
||||||
|
.render(area, &mut buffer);
|
||||||
|
buffer
|
||||||
|
.content
|
||||||
|
.chunks(usize::from(width.max(/*other*/ 1)))
|
||||||
|
.map(|cells| {
|
||||||
|
let end = cells
|
||||||
|
.iter()
|
||||||
|
.rposition(|cell| cell.symbol() != " ")
|
||||||
|
.map_or(/*default*/ 0, |index| index + 1);
|
||||||
|
let mut spans = Vec::new();
|
||||||
|
let mut column = 0;
|
||||||
|
while column < end {
|
||||||
|
let cell = &cells[column];
|
||||||
|
spans.push(Span::styled(cell.symbol().to_string(), cell.style()));
|
||||||
|
// Buffer cells following a wide grapheme are continuation cells.
|
||||||
|
column += usize::from(cell.cell_width().max(/*other*/ 1));
|
||||||
|
}
|
||||||
|
Line::from(spans).style(style)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
//! Patch summaries and image-tool transcript helpers.
|
//! Patch summaries and image-tool transcript helpers.
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::diff_render::create_diff_preview;
|
||||||
use codex_utils_path_uri::LegacyAppPathString;
|
use codex_utils_path_uri::LegacyAppPathString;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -11,6 +12,10 @@ pub(crate) struct PatchHistoryCell {
|
|||||||
|
|
||||||
impl HistoryCell for PatchHistoryCell {
|
impl HistoryCell for PatchHistoryCell {
|
||||||
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
|
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
|
||||||
|
create_diff_preview(&self.changes, &self.cwd, width as usize)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transcript_lines(&self, width: u16) -> Vec<Line<'static>> {
|
||||||
create_diff_summary(&self.changes, &self.cwd, width as usize)
|
create_diff_summary(&self.changes, &self.cwd, width as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,244 @@ fn test_cwd() -> PathBuf {
|
|||||||
std::env::temp_dir()
|
std::env::temp_dir()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_preview_shows_minified_prefix_but_preserves_full_views() {
|
||||||
|
let changes = HashMap::from([(
|
||||||
|
PathBuf::from("metrics.json"),
|
||||||
|
FileChange::Add {
|
||||||
|
content: format!("[{}0]", "123,".repeat(/*n*/ 10_000)),
|
||||||
|
},
|
||||||
|
)]);
|
||||||
|
let cwd = test_cwd();
|
||||||
|
let cell = new_patch_event(changes.clone(), &cwd);
|
||||||
|
|
||||||
|
insta::assert_snapshot!(render_lines(&cell.display_lines(/*width*/ 80)).join("\n"), @"
|
||||||
|
• Added metrics.json (+1 -0)
|
||||||
|
1 +[123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
|
||||||
|
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,1
|
||||||
|
23,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,12
|
||||||
|
3,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123
|
||||||
|
,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
|
||||||
|
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,1
|
||||||
|
23,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,12
|
||||||
|
3,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123
|
||||||
|
,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
|
||||||
|
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,1
|
||||||
|
23,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,12
|
||||||
|
3,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123
|
||||||
|
… Diff preview limited (ctrl + t to view transcript).
|
||||||
|
");
|
||||||
|
assert_eq!(
|
||||||
|
cell.transcript_lines(/*width*/ 80),
|
||||||
|
create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cell.raw_lines(),
|
||||||
|
plain_lines(create_diff_summary(&changes, &cwd, RAW_DIFF_SUMMARY_WIDTH)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_preview_limits_add_delete_and_update_content() {
|
||||||
|
for content in [
|
||||||
|
"value\n".repeat(/*n*/ 20),
|
||||||
|
format!("short\n{}\ntail\n", "界123\t".repeat(/*n*/ 100)),
|
||||||
|
] {
|
||||||
|
for change in [
|
||||||
|
FileChange::Add {
|
||||||
|
content: content.clone(),
|
||||||
|
},
|
||||||
|
FileChange::Delete {
|
||||||
|
content: content.clone(),
|
||||||
|
},
|
||||||
|
FileChange::Update {
|
||||||
|
unified_diff: diffy::create_patch("", &content).to_string(),
|
||||||
|
move_path: Some(PathBuf::from("renamed.txt")),
|
||||||
|
},
|
||||||
|
FileChange::Update {
|
||||||
|
unified_diff: diffy::create_patch(&content, "").to_string(),
|
||||||
|
move_path: None,
|
||||||
|
},
|
||||||
|
FileChange::Update {
|
||||||
|
unified_diff: diffy::DiffOptions::new()
|
||||||
|
.set_context_len(/*context_len*/ 20)
|
||||||
|
.create_patch(&content, &format!("{content}new\n"))
|
||||||
|
.to_string(),
|
||||||
|
move_path: None,
|
||||||
|
},
|
||||||
|
] {
|
||||||
|
let changes = HashMap::from([(PathBuf::from("values.txt"), change)]);
|
||||||
|
let cwd = test_cwd();
|
||||||
|
let cell = new_patch_event(changes.clone(), &cwd);
|
||||||
|
let full = create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80);
|
||||||
|
let mut expected = full[..13].to_vec();
|
||||||
|
expected.push(
|
||||||
|
" … Diff preview limited (ctrl + t to view transcript)."
|
||||||
|
.dim()
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
assert_eq!(cell.display_lines(/*width*/ 80), expected);
|
||||||
|
assert_eq!(cell.transcript_lines(/*width*/ 80), full);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_preview_shares_content_budget_and_keeps_file_summaries() {
|
||||||
|
let changes = HashMap::from([
|
||||||
|
(
|
||||||
|
PathBuf::from("a.txt"),
|
||||||
|
FileChange::Add {
|
||||||
|
content: "a\n".repeat(/*n*/ 11) + &"©️ ".repeat(/*n*/ 36),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
PathBuf::from("b.txt"),
|
||||||
|
FileChange::Add {
|
||||||
|
content: "b\n".repeat(/*n*/ 10),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
PathBuf::from("c.txt"),
|
||||||
|
FileChange::Add {
|
||||||
|
content: "c\n".into(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
let cell = new_patch_event(changes, &test_cwd());
|
||||||
|
insta::assert_snapshot!(render_lines(&cell.display_lines(/*width*/ 80)).join("\n"), @"
|
||||||
|
• Edited 3 files (+23 -0)
|
||||||
|
└ a.txt (+12 -0)
|
||||||
|
1 +a
|
||||||
|
2 +a
|
||||||
|
3 +a
|
||||||
|
4 +a
|
||||||
|
5 +a
|
||||||
|
6 +a
|
||||||
|
7 +a
|
||||||
|
8 +a
|
||||||
|
9 +a
|
||||||
|
10 +a
|
||||||
|
11 +a
|
||||||
|
12 +©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️ ©️
|
||||||
|
|
||||||
|
└ b.txt (+10 -0)
|
||||||
|
|
||||||
|
└ c.txt (+1 -0)
|
||||||
|
… Diff preview limited (ctrl + t to view transcript).
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_preview_budgets_wrapped_rows_at_narrow_widths() {
|
||||||
|
for content in [
|
||||||
|
"0123456789".repeat(/*n*/ 1_000),
|
||||||
|
"ガ".repeat(/*n*/ 1_000),
|
||||||
|
"0123456789\n".repeat(/*n*/ 20),
|
||||||
|
format!("{}\n", "界\tvalue".repeat(/*n*/ 100)).repeat(/*n*/ 20),
|
||||||
|
format!("{}\n", "\u{a9}\u{fe0f}".repeat(/*n*/ 50)).repeat(/*n*/ 20),
|
||||||
|
] {
|
||||||
|
let changes = HashMap::from([(PathBuf::from("values.txt"), FileChange::Add { content })]);
|
||||||
|
let cell = new_patch_event(changes, &test_cwd());
|
||||||
|
for width in [8, 16, 40, 80] {
|
||||||
|
let lines = cell.display_lines(width);
|
||||||
|
let body = lines[1..lines.len() - 1].to_vec();
|
||||||
|
let rows = Paragraph::new(body)
|
||||||
|
.wrap(Wrap { trim: false })
|
||||||
|
.line_count(width);
|
||||||
|
assert_eq!(rows, 12);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_preview_preserves_small_diffs_and_exact_budget() {
|
||||||
|
for count in [1, 12] {
|
||||||
|
let mut changes = HashMap::from([(
|
||||||
|
PathBuf::from("small.rs"),
|
||||||
|
FileChange::Add {
|
||||||
|
content: "let value = 1;\n".repeat(count),
|
||||||
|
},
|
||||||
|
)]);
|
||||||
|
changes.extend([
|
||||||
|
(
|
||||||
|
PathBuf::from("z-empty"),
|
||||||
|
FileChange::Add {
|
||||||
|
content: String::new(),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
PathBuf::from("z-renamed"),
|
||||||
|
FileChange::Update {
|
||||||
|
unified_diff: String::new(),
|
||||||
|
move_path: Some(PathBuf::from("renamed")),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
let cwd = test_cwd();
|
||||||
|
let cell = new_patch_event(changes.clone(), &cwd);
|
||||||
|
assert_eq!(
|
||||||
|
cell.display_lines(/*width*/ 80),
|
||||||
|
create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn patch_preview_caps_zero_width_source_before_later_content() {
|
||||||
|
let make_changes: [fn(String) -> FileChange; 3] = [
|
||||||
|
|content| FileChange::Add { content },
|
||||||
|
|content| FileChange::Delete { content },
|
||||||
|
|content| FileChange::Update {
|
||||||
|
unified_diff: diffy::create_patch("", &content).to_string(),
|
||||||
|
move_path: None,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let cwd = test_cwd();
|
||||||
|
let content = format!("head\nxy{}\ntail\n", "\u{301}".repeat(/*n*/ 40_000));
|
||||||
|
let mut summaries = Vec::new();
|
||||||
|
for make_change in make_changes {
|
||||||
|
let changes = HashMap::from([
|
||||||
|
(PathBuf::from("a.txt"), make_change(content.clone())),
|
||||||
|
(PathBuf::from("b.txt"), make_change("later\n".into())),
|
||||||
|
]);
|
||||||
|
let cell = new_patch_event(changes.clone(), &cwd);
|
||||||
|
let preview = render_lines(&cell.display_lines(/*width*/ 80)).join("\n");
|
||||||
|
// The next two-byte combining mark straddles the 64 KiB boundary.
|
||||||
|
assert_eq!(preview.matches('\u{301}').count(), 32_764);
|
||||||
|
summaries.push(preview.replace('\u{301}', ""));
|
||||||
|
assert_eq!(
|
||||||
|
cell.transcript_lines(/*width*/ 80),
|
||||||
|
create_diff_summary(&changes, &cwd, /*wrap_cols*/ 80)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
insta::assert_snapshot!(summaries.join("\n\n"), @"
|
||||||
|
• Edited 2 files (+4 -0)
|
||||||
|
└ a.txt (+3 -0)
|
||||||
|
1 +head
|
||||||
|
2 +xy
|
||||||
|
|
||||||
|
└ b.txt (+1 -0)
|
||||||
|
… Diff preview limited (ctrl + t to view transcript).
|
||||||
|
|
||||||
|
• Edited 2 files (+0 -4)
|
||||||
|
└ a.txt (+0 -3)
|
||||||
|
1 -head
|
||||||
|
2 -xy
|
||||||
|
|
||||||
|
└ b.txt (+0 -1)
|
||||||
|
… Diff preview limited (ctrl + t to view transcript).
|
||||||
|
|
||||||
|
• Edited 2 files (+4 -0)
|
||||||
|
└ a.txt (+3 -0)
|
||||||
|
1 +head
|
||||||
|
2 +xy
|
||||||
|
|
||||||
|
└ b.txt (+1 -0)
|
||||||
|
… Diff preview limited (ctrl + t to view transcript).
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn streaming_agent_tail_blank_line_uses_one_viewport_row() {
|
fn streaming_agent_tail_blank_line_uses_one_viewport_row() {
|
||||||
let cell = StreamingAgentTailCell::new(
|
let cell = StreamingAgentTailCell::new(
|
||||||
|
|||||||
Reference in new issue
Block a user