Skip to content

Commit d27c916

Browse files
committed
Improve expanded inline diff gutters and wrapping
1 parent 968d2fd commit d27c916

2 files changed

Lines changed: 156 additions & 2 deletions

File tree

‎crates/jcode-tui/src/tui/ui_messages.rs‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4092,6 +4092,79 @@ fn inline_diff_header(
40924092
}
40934093
let mut header = Line::from(header);
40944094
if let Some(path) = file_path {
4095+
/// Keep expanded source lines inside their frame. Wrap the styled source, not
4096+
/// the gutter, so continuation rows cannot masquerade as another changed line.
4097+
fn expanded_diff_row(
4098+
line: &ParsedDiffLine,
4099+
file_ext: Option<&str>,
4100+
number_width: usize,
4101+
width: usize,
4102+
) -> Vec<Line<'static>> {
4103+
let dim = Style::default().fg(dim_color());
4104+
let color = if line.kind == DiffLineKind::Add {
4105+
diff_add_color()
4106+
} else {
4107+
diff_del_color()
4108+
};
4109+
let sign = if line.kind == DiffLineKind::Add {
4110+
"+"
4111+
} else {
4112+
"-"
4113+
};
4114+
let number = line.prefix.trim().trim_end_matches(['+', '-']);
4115+
let gutter = vec![
4116+
Span::styled(format!("│ {number:>number_width$} "), dim),
4117+
Span::styled(format!("{sign} "), Style::default().fg(color)),
4118+
];
4119+
let gutter_width = number_width + 5;
4120+
// Leave room for a wide source character even on very narrow terminals.
4121+
if width < gutter_width + 2 {
4122+
return vec![super::truncate_line_with_ellipsis_to_width(
4123+
&Line::from(gutter),
4124+
width,
4125+
)];
4126+
}
4127+
let content_width = width - gutter_width;
4128+
let mut rows = Vec::new();
4129+
let mut spans = gutter;
4130+
let mut used = 0;
4131+
// Expand tabs in source-column coordinates, independent of the gutter.
4132+
let mut column = 0usize;
4133+
let mut source = String::new();
4134+
for ch in line.content.chars() {
4135+
if ch == '\t' {
4136+
let spaces = 4 - column % 4;
4137+
source.push_str(&" ".repeat(spaces));
4138+
column += spaces;
4139+
} else {
4140+
source.push(ch);
4141+
column += unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
4142+
}
4143+
}
4144+
for span in markdown::highlight_line(&source, file_ext) {
4145+
let span = tint_span_with_diff_color(span, color);
4146+
let mut text = String::new();
4147+
for ch in span.content.chars() {
4148+
let cells = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
4149+
if used + cells > content_width {
4150+
if !text.is_empty() {
4151+
spans.push(Span::styled(std::mem::take(&mut text), span.style));
4152+
}
4153+
rows.push(Line::from(std::mem::take(&mut spans)).alignment(Alignment::Left));
4154+
spans.push(Span::styled(format!("│ {:>number_width$} ↪ ", ""), dim));
4155+
used = 0;
4156+
}
4157+
text.push(ch);
4158+
used += cells;
4159+
}
4160+
if !text.is_empty() {
4161+
spans.push(Span::styled(text, span.style));
4162+
}
4163+
}
4164+
rows.push(Line::from(spans).alignment(Alignment::Left));
4165+
rows
4166+
}
4167+
40954168
let path_width = width.saturating_sub(header.width().saturating_add(3));
40964169
if path_width > 0 {
40974170
header.spans.push(Span::styled(
@@ -4653,6 +4726,11 @@ pub(crate) fn render_tool_message(
46534726
});
46544727
const MAX_DIFF_LINES: usize = MAX_INLINE_DIFF_LINES;
46554728
let total_changes = change_lines.len();
4729+
let number_width = change_lines
4730+
.iter()
4731+
.map(|line| line.prefix.trim().trim_end_matches(['+', '-']).len())
4732+
.max()
4733+
.unwrap_or(0);
46564734
let additions = change_lines
46574735
.iter()
46584736
.filter(|line| line.kind == DiffLineKind::Add)
@@ -4678,6 +4756,15 @@ pub(crate) fn render_tool_message(
46784756
|line: &ParsedDiffLine| line.file_path.clone().or_else(|| file_path_for_ext.clone());
46794757
let first_file_path = display_lines.first().and_then(|line| diff_file_path(line));
46804758
lines.push(inline_diff_header(
4759+
if full_inline {
4760+
let file_ext = current_file_path
4761+
.as_deref()
4762+
.and_then(|p| std::path::Path::new(p).extension())
4763+
.and_then(|e| e.to_str());
4764+
lines.extend(expanded_diff_row(line, file_ext, number_width, row_width));
4765+
continue;
4766+
}
4767+
46814768
"╭─",
46824769
Some((additions, deletions)),
46834770
first_file_path.as_deref(),
@@ -4720,7 +4807,7 @@ pub(crate) fn render_tool_message(
47204807
let border_prefix = format!("{}│ ", pad_str);
47214808
let prefix_visual_width = unicode_width::UnicodeWidthStr::width(border_prefix.as_str())
47224809
+ unicode_width::UnicodeWidthStr::width(line.prefix.as_str());
4723-
let max_content_width = (width as usize).saturating_sub(prefix_visual_width + 1);
4810+
let max_content_width = row_width.saturating_sub(prefix_visual_width);
47244811

47254812
let mut spans: Vec<Span<'static>> = vec![
47264813
Span::styled(border_prefix, Style::default().fg(dim_color())),

‎crates/jcode-tui/src/tui/ui_messages/tests.rs‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
use super::*;
22

3+
#[test]
4+
fn expanded_diff_rows_preserve_source_and_align_continuations() {
5+
let source = " let result = compute(\"界界 e\u{301}\", argument);";
6+
let parsed = ParsedDiffLine {
7+
kind: DiffLineKind::Add,
8+
prefix: "9+ ".into(),
9+
content: source.into(),
10+
file_path: None,
11+
};
12+
let rows = expanded_diff_row(&parsed, Some("rs"), 3, 24);
13+
assert!(rows.len() > 1);
14+
assert!(extract_line_text(&rows[0]).starts_with("│ 9 + let"));
15+
let mut reconstructed = String::new();
16+
for (index, row) in rows.iter().enumerate() {
17+
assert!(row.width() <= 24, "{row:?}");
18+
assert_eq!(row.alignment, Some(Alignment::Left));
19+
if index > 0 {
20+
assert!(extract_line_text(row).starts_with("│ ↪ "));
21+
}
22+
for span in row.spans.iter().skip(if index == 0 { 2 } else { 1 }) {
23+
reconstructed.push_str(&span.content);
24+
}
25+
}
26+
assert_eq!(reconstructed, source);
27+
assert_eq!(rows[0].spans[1].style.fg, Some(diff_add_color()));
28+
}
29+
30+
#[test]
31+
fn expanded_diff_rows_fit_narrow_widths_and_expand_tabs() {
32+
let parsed = ParsedDiffLine {
33+
kind: DiffLineKind::Del,
34+
prefix: "123- ".into(),
35+
content: "\t界\tvalue".into(),
36+
file_path: None,
37+
};
38+
for width in 0..60 {
39+
let rows = expanded_diff_row(&parsed, None, 3, width);
40+
assert!(
41+
rows.iter().all(|row| row.width() <= width),
42+
"{width}: {rows:?}"
43+
);
44+
}
45+
let rows = expanded_diff_row(&parsed, None, 3, 60);
46+
assert_eq!(extract_line_text(&rows[0]), "│ 123 - 界 value");
47+
assert_eq!(rows[0].spans[1].style.fg, Some(diff_del_color()));
48+
let blank = ParsedDiffLine {
49+
content: String::new(),
50+
..parsed
51+
};
52+
assert_eq!(expanded_diff_row(&blank, None, 3, 60).len(), 1);
53+
}
54+
355
#[test]
456
fn inline_diff_header_compacts_directories_before_filename() {
557
let path = "/home/user/project/crates/very_long_directory/src/important_file.rs";
@@ -3750,7 +3802,7 @@ fn render_tool_message_full_inline_mode_shows_full_diff() {
37503802
assert!(!plain.contains("more changes"), "plain={plain}");
37513803
assert!(plain.contains("old line 4"), "plain={plain}");
37523804
assert!(
3753-
plain.contains("new line 4 suffix_4_abcdefghijklmnopqrstuvwxyz0123456789"),
3805+
source_text.contains("new line 4 suffix_4_abcdefghijklmnopqrstuvwxyz0123456789"),
37543806
"plain={plain}"
37553807
);
37563808
assert!(!plain.contains('…'), "plain={plain}");
@@ -3786,10 +3838,25 @@ fn render_tool_message_memory_recall_centered_mode_left_aligns_with_padding() {
37863838
)
37873839
.to_string(),
37883840
tool_calls: Vec::new(),
3841+
let source_text = lines
3842+
.iter()
3843+
.filter(|line| extract_line_text(line).starts_with("│ "))
3844+
.flat_map(|line| {
3845+
line.spans
3846+
.iter()
3847+
.skip(if line.spans[0].content.contains('↪') {
3848+
1
3849+
} else {
3850+
2
3851+
})
3852+
})
3853+
.map(|span| span.content.as_ref())
3854+
.collect::<String>();
37893855
duration_secs: None,
37903856
title: None,
37913857
tool_data: Some(crate::message::ToolCall {
37923858
id: "call_memory_recall_centered".to_string(),
3859+
assert!(lines.iter().all(|line| line.width() <= 39), "plain={plain}");
37933860
name: "memory".to_string(),
37943861
input: serde_json::json!({
37953862
"action": "recall",

0 commit comments

Comments
 (0)