diff: Don't render hunk header
0b239bd6debae85ad45378454ea7b44bd9acc981
1 parent
919ad68e
crates/diff/src/renderer.rs
+14 -40
| 495 | 495 | return RenderRow { |
|
| 496 | 496 | line: border.divider(inner_cols, DIVIDER), |
|
| 497 | 497 | diff: None, |
|
| 498 | 498 | }; |
|
| 499 | 499 | } |
|
| 500 | - | let mut header_rows = file.hunk_header_rows(collapsed).peekable(); |
|
| 501 | - | while let Some(&(hunk, header_row)) = header_rows.peek() { |
|
| 502 | - | let body_end = header_row + 1 + hunk.lines.len(); |
|
| 503 | - | if local < header_row { |
|
| 500 | + | let mut body_rows = file.hunk_body_rows(collapsed).peekable(); |
|
| 501 | + | while let Some(&(hunk, body_start)) = body_rows.peek() { |
|
| 502 | + | let body_end = body_start + hunk.lines.len(); |
|
| 503 | + | if local < body_start { |
|
| 504 | 504 | return RenderRow { |
|
| 505 | 505 | line: wrap(Self::hunk_gap_line()), |
|
| 506 | 506 | diff: None, |
|
| 507 | 507 | }; |
|
| 508 | 508 | } |
|
| 509 | - | if local == header_row { |
|
| 510 | - | return RenderRow { |
|
| 511 | - | line: wrap(hunk.header_line()), |
|
| 512 | - | diff: None, |
|
| 513 | - | }; |
|
| 514 | - | } |
|
| 515 | 509 | if local < body_end { |
|
| 516 | - | let row = self.hunk_row_at(file, hunk, local - header_row - 1, child_cols); |
|
| 510 | + | let row = self.hunk_row_at(file, hunk, local - body_start, child_cols); |
|
| 517 | 511 | return RenderRow { |
|
| 518 | 512 | line: wrap(row.line), |
|
| 519 | 513 | diff: row.diff, |
|
| 520 | 514 | }; |
|
| 521 | 515 | } |
|
| 522 | - | header_rows.next(); |
|
| 516 | + | body_rows.next(); |
|
| 523 | 517 | } |
|
| 524 | 518 | RenderRow { |
|
| 525 | 519 | line: border.bottom(inner_cols), |
|
| 526 | 520 | diff: None, |
|
| 527 | 521 | } |
| 529 | 523 | ||
| 530 | 524 | /// Returns source metadata for one rendered row without building UI lines. |
|
| 531 | 525 | fn diff_meta_at(&self, row: usize) -> Option<DiffLineMeta> { |
|
| 532 | 526 | let location = self.file_row_location(row)?; |
|
| 533 | 527 | let collapsed = self.state.collapsed_files.contains(&location.file.id); |
|
| 534 | - | for (hunk, header_row) in location.file.hunk_header_rows(collapsed) { |
|
| 535 | - | let body_start = header_row + 1; |
|
| 528 | + | for (hunk, body_start) in location.file.hunk_body_rows(collapsed) { |
|
| 536 | 529 | let body_end = body_start + hunk.lines.len(); |
|
| 537 | 530 | if location.local < body_start { |
|
| 538 | 531 | return None; |
|
| 539 | 532 | } |
|
| 540 | 533 | if location.local < body_end { |
| 761 | 754 | focus: DiffFocus::File(file.id), |
|
| 762 | 755 | row, |
|
| 763 | 756 | }); |
|
| 764 | 757 | } |
|
| 765 | 758 | if filter == FocusFilter::Hunk { |
|
| 766 | - | for (hunk, local) in file.hunk_header_rows(collapsed) { |
|
| 759 | + | for (hunk, local) in file.hunk_body_rows(collapsed) { |
|
| 767 | 760 | targets.push(FocusTarget { |
|
| 768 | 761 | focus: DiffFocus::Hunk(hunk.id), |
|
| 769 | 762 | row: row + local, |
|
| 770 | 763 | }); |
|
| 771 | 764 | } |
| 1028 | 1021 | File, |
|
| 1029 | 1022 | /// Hunk targets only. |
|
| 1030 | 1023 | Hunk, |
|
| 1031 | 1024 | } |
|
| 1032 | 1025 | ||
| 1033 | - | /// Local row of the first hunk header (top border + file header + divider). |
|
| 1026 | + | /// Local row of the first hunk body line (top border + file header + divider). |
|
| 1034 | 1027 | const HUNK_BODY_START: usize = 3; |
|
| 1035 | 1028 | /// Total card height when the file has no visible body (just borders + header). |
|
| 1036 | 1029 | const EMPTY_CARD_ROWS: usize = 3; |
|
| 1037 | 1030 | ||
| 1038 | 1031 | impl FileDiff { |
| 1046 | 1039 | if !self.has_body(collapsed) { |
|
| 1047 | 1040 | return EMPTY_CARD_ROWS; |
|
| 1048 | 1041 | } |
|
| 1049 | 1042 | let hunks = self.hunks.len(); |
|
| 1050 | 1043 | let body: usize = self.hunks.iter().map(|hunk| hunk.lines.len()).sum(); |
|
| 1051 | - | // top + header + divider + (hunk header + body) per hunk + gaps + bottom. |
|
| 1052 | - | HUNK_BODY_START + hunks + body + hunks.saturating_sub(1) + 1 |
|
| 1044 | + | // top + header + divider + body lines + gaps + bottom. |
|
| 1045 | + | HUNK_BODY_START + body + hunks.saturating_sub(1) + 1 |
|
| 1053 | 1046 | } |
|
| 1054 | 1047 | ||
| 1055 | - | /// Returns each hunk together with the local row of its header. |
|
| 1056 | - | fn hunk_header_rows(&self, collapsed: bool) -> impl Iterator<Item = (&Hunk, usize)> + '_ { |
|
| 1048 | + | /// Returns each hunk together with the local row of its first body line. |
|
| 1049 | + | fn hunk_body_rows(&self, collapsed: bool) -> impl Iterator<Item = (&Hunk, usize)> + '_ { |
|
| 1057 | 1050 | let has_body = self.has_body(collapsed); |
|
| 1058 | 1051 | self.hunks |
|
| 1059 | 1052 | .iter() |
|
| 1060 | 1053 | .enumerate() |
|
| 1061 | 1054 | .scan(HUNK_BODY_START, move |local, (index, hunk)| { |
| 1064 | 1057 | } |
|
| 1065 | 1058 | if index > 0 { |
|
| 1066 | 1059 | *local += 1; |
|
| 1067 | 1060 | } |
|
| 1068 | 1061 | let row = *local; |
|
| 1069 | - | *local += 1 + hunk.lines.len(); |
|
| 1062 | + | *local += hunk.lines.len(); |
|
| 1070 | 1063 | Some((hunk, row)) |
|
| 1071 | 1064 | }) |
|
| 1072 | 1065 | } |
|
| 1073 | 1066 | ||
| 1074 | 1067 | /// Returns the source path for a file side. |
| 1212 | 1205 | match side { |
|
| 1213 | 1206 | DiffSide::Old => self.header.old_line_no, |
|
| 1214 | 1207 | DiffSide::New => self.header.new_line_no, |
|
| 1215 | 1208 | } |
|
| 1216 | 1209 | } |
|
| 1217 | - | ||
| 1218 | - | /// Returns a hunk header line. |
|
| 1219 | - | fn header_line(&self) -> Line { |
|
| 1220 | - | Line::new(Label::new(&self.header_text()).fg(colors::fixed::FAINT)) |
|
| 1221 | - | } |
|
| 1222 | - | ||
| 1223 | - | /// Returns the stable hunk header prefix and trailing text. |
|
| 1224 | - | fn header_text(&self) -> String { |
|
| 1225 | - | let header = &self.header; |
|
| 1226 | - | let prefix = format!( |
|
| 1227 | - | "@@ -{},{} +{},{} @@", |
|
| 1228 | - | header.old_line_no, header.old_size, header.new_line_no, header.new_size |
|
| 1229 | - | ); |
|
| 1230 | - | if header.text.is_empty() { |
|
| 1231 | - | prefix |
|
| 1232 | - | } else { |
|
| 1233 | - | format!("{prefix} {}", header.text) |
|
| 1234 | - | } |
|
| 1235 | - | } |
|
| 1236 | 1210 | } |
|
| 1237 | 1211 | ||
| 1238 | 1212 | impl DiffLineKind { |
|
| 1239 | 1213 | /// Returns whether this line exists on a source side. |
|
| 1240 | 1214 | fn appears_on(self, side: DiffSide) -> bool { |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
crates/diff/tests/golden/expanded_file_card_64x9.txt
+8 -8
| 1 | - | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 2 | - | │ ○ a.rs +2 -1 │▓ |
|
| 3 | - | ├─────────────────────────────────────────────────────────────┤▓ |
|
| 4 | - | │ @@ -1,3 +1,4 @@ fn main │▓ |
|
| 5 | - | │ 1 1 context │▓ |
|
| 6 | - | │ 2 - old │▓ |
|
| 7 | - | │ 2 + new │▓ |
|
| 8 | - | │ 3 + extra │▓ |
|
| 1 | + | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | + | │ ○ a.rs +2 -1 │🮘 |
|
| 3 | + | ├─────────────────────────────────────────────────────────────┤🮘 |
|
| 4 | + | │ 1 1 context │🮘 |
|
| 5 | + | │ 2 - old │🮘 |
|
| 6 | + | │ 2 + new │🮘 |
|
| 7 | + | │ 3 + extra │🮘 |
|
| 9 | 8 | │ 3 4 tail │🮘 |
|
| 9 | + | ╰─────────────────────────────────────────────────────────────╯🮘 |
crates/diff/tests/golden/file_kinds_80x24.txt
+2 -2
| 1 | 1 | ╭─────────────────────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | 2 | │ ○ new.rs created +1 │🮘 |
|
| 3 | 3 | ├─────────────────────────────────────────────────────────────────────────────┤🮘 |
|
| 4 | - | │ @@ -0,0 +1,1 @@ │🮘 |
|
| 5 | 4 | │ 1 + new │🮘 |
|
| 6 | 5 | ╰─────────────────────────────────────────────────────────────────────────────╯🮘 |
|
| 7 | 6 | ╭─────────────────────────────────────────────────────────────────────────────╮🮘 |
|
| 8 | 7 | │ ○ old.rs deleted -1 │🮘 |
|
| 9 | 8 | ├─────────────────────────────────────────────────────────────────────────────┤🮘 |
|
| 10 | - | │ @@ -1,1 +0,0 @@ │🮘 |
|
| 11 | 9 | │ 1 - old │🮘 |
|
| 12 | 10 | ╰─────────────────────────────────────────────────────────────────────────────╯🮘 |
|
| 13 | 11 | ╭─────────────────────────────────────────────────────────────────────────────╮🮘 |
|
| 14 | 12 | │ ○ from.rs -> to.rs moved │🮘 |
|
| 15 | 13 | ╰─────────────────────────────────────────────────────────────────────────────╯🮘 |
| 20 | 18 | │ ○ mode.sh mode changed │🮘 |
|
| 21 | 19 | ╰─────────────────────────────────────────────────────────────────────────────╯🮘 |
|
| 22 | 20 | ╭─────────────────────────────────────────────────────────────────────────────╮🮘 |
|
| 23 | 21 | │ ○ image.png binary │🮘 |
|
| 24 | 22 | ╰─────────────────────────────────────────────────────────────────────────────╯🮘 |
|
| 23 | + | 🮘 |
|
| 24 | + | 🮘 |
crates/diff/tests/golden/focused_second_file_64x6.txt
+2 -2
| 1 | 1 | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | 2 | │ ○ b.rs +1 -1 │🮘 |
|
| 3 | 3 | ├─────────────────────────────────────────────────────────────┤🮘 |
|
| 4 | - | │ @@ -1,2 +1,2 @@ fn b │▓ |
|
| 5 | 4 | │ 1 1 keep │▓ |
|
| 6 | - | │ 2 - before │🮘 |
|
| 5 | + | │ 2 - before │▓ |
|
| 6 | + | │ 2 + after │🮘 |
crates/diff/tests/golden/highlighted_source_64x5.txt
+1 -1
| 1 | 1 | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 2 | 2 | │ ○ a.rs +2 -1 │▓ |
|
| 3 | 3 | ├─────────────────────────────────────────────────────────────┤🮘 |
|
| 4 | - | │ @@ -1,3 +1,4 @@ fn main │🮘 |
|
| 5 | 4 | │ 1 1 highlighted context │🮘 |
|
| 5 | + | │ 2 - old │🮘 |
crates/diff/tests/golden/multi_hunk_gap_64x12.txt
+11 -11
| 1 | - | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 2 | - | │ ○ a.rs +2 -2 │▓ |
|
| 3 | - | ├─────────────────────────────────────────────────────────────┤▓ |
|
| 4 | - | │ @@ -1,2 +1,2 @@ fn first │▓ |
|
| 5 | - | │ 1 1 keep │▓ |
|
| 6 | - | │ 2 - old │▓ |
|
| 7 | - | │ 2 + new │▓ |
|
| 8 | - | │ ⋮ │▓ |
|
| 9 | - | │ @@ -10,2 +10,2 @@ fn second │▓ |
|
| 10 | - | │ 10 10 keep │▓ |
|
| 11 | - | │ 11 - before │▓ |
|
| 1 | + | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | + | │ ○ a.rs +2 -2 │🮘 |
|
| 3 | + | ├─────────────────────────────────────────────────────────────┤🮘 |
|
| 4 | + | │ 1 1 keep │🮘 |
|
| 5 | + | │ 2 - old │🮘 |
|
| 6 | + | │ 2 + new │🮘 |
|
| 7 | + | │ ⋮ │🮘 |
|
| 8 | + | │ 10 10 keep │🮘 |
|
| 9 | + | │ 11 - before │🮘 |
|
| 12 | 10 | │ 11 + after │🮘 |
|
| 11 | + | ╰─────────────────────────────────────────────────────────────╯🮘 |
|
| 12 | + | 🮘 |
crates/diff/tests/golden/narrow_view_14x8.txt
+3 -3
| 1 | 1 | ╭───────────╮▓ |
|
| 2 | 2 | │ ○ a.rs +2 │▓ |
|
| 3 | 3 | ├───────────┤▓ |
|
| 4 | - | │ @@ -1,3 … │▓ |
|
| 5 | 4 | │ 1 1 │▓ |
|
| 6 | 5 | │ 2 │▓ |
|
| 7 | - | │ 2 │🮘 |
|
| 8 | - | │ 3 │🮘 |
|
| 6 | + | │ 2 │▓ |
|
| 7 | + | │ 3 │▓ |
|
| 8 | + | │ 3 4 │🮘 |
crates/diff/tests/golden/preamble_and_no_newline_64x17.txt
+16 -16
| 1 | - | ╭──────────────────────────────────────────────────╮ ▓ |
|
| 2 | - | │ commit 1234567890abcdef1234567890abcdef12345678 │ ▓ |
|
| 3 | - | │ author A <a@example.com> │ ▓ |
|
| 4 | - | │ date Tue May 12 10:20:30 2026 +0200 │ ▓ |
|
| 5 | - | ├──────────────────────────────────────────────────┤ ▓ |
|
| 6 | - | │ Commit summary │ ▓ |
|
| 7 | - | │ │ ▓ |
|
| 8 | - | │ Message body text 1. │ ▓ |
|
| 9 | - | │ Message body text 2. │ ▓ |
|
| 10 | - | ╰──────────────────────────────────────────────────╯ ▓ |
|
| 11 | - | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 12 | - | │ ○ a.rs +1 -1 │▓ |
|
| 13 | - | ├─────────────────────────────────────────────────────────────┤▓ |
|
| 14 | - | │ @@ -1,1 +1,1 @@ │▓ |
|
| 15 | - | │ 1 - old │▓ |
|
| 16 | - | │ 1 + new │▓ |
|
| 1 | + | ╭──────────────────────────────────────────────────╮ 🮘 |
|
| 2 | + | │ commit 1234567890abcdef1234567890abcdef12345678 │ 🮘 |
|
| 3 | + | │ author A <a@example.com> │ 🮘 |
|
| 4 | + | │ date Tue May 12 10:20:30 2026 +0200 │ 🮘 |
|
| 5 | + | ├──────────────────────────────────────────────────┤ 🮘 |
|
| 6 | + | │ Commit summary │ 🮘 |
|
| 7 | + | │ │ 🮘 |
|
| 8 | + | │ Message body text 1. │ 🮘 |
|
| 9 | + | │ Message body text 2. │ 🮘 |
|
| 10 | + | ╰──────────────────────────────────────────────────╯ 🮘 |
|
| 11 | + | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 12 | + | │ ○ a.rs +1 -1 │🮘 |
|
| 13 | + | ├─────────────────────────────────────────────────────────────┤🮘 |
|
| 14 | + | │ 1 - old │🮘 |
|
| 15 | + | │ 1 + new │🮘 |
|
| 17 | 16 | │ \ No newline at end of file │🮘 |
|
| 17 | + | ╰─────────────────────────────────────────────────────────────╯🮘 |
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+3 -3
| 1 | - | │ 3 4 tail │🮘 |
|
| 2 | 1 | ╰─────────────────────────────────────────────────────────────╯🮘 |
|
| 3 | - | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 4 | - | │ ○ b.rs +1 -1 │🮘 |
|
| 2 | + | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 3 | + | │ ○ b.rs +1 -1 │▓ |
|
| 5 | 4 | ├─────────────────────────────────────────────────────────────┤🮘 |
|
| 5 | + | │ 1 1 keep │🮘 |
crates/diff/tests/golden/sticky_header_inside_file_64x5.txt
+3 -3
| 1 | 1 | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | 2 | │ ○ a.rs +2 -1 │🮘 |
|
| 3 | - | ├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤▓ |
|
| 4 | - | │ 3 + extra │▓ |
|
| 5 | - | │ 3 4 tail │🮘 |
|
| 3 | + | ├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘 |
|
| 4 | + | │ 3 4 tail │▓ |
|
| 5 | + | ╰─────────────────────────────────────────────────────────────╯▓ |
crates/diff/tests/golden/sticky_header_replaces_chrome_64x5.txt
+3 -3
| 1 | 1 | ╭─────────────────────────────────────────────────────────────╮🮘 |
|
| 2 | - | │ ○ a.rs +2 -1 │▓ |
|
| 2 | + | │ ○ a.rs +2 -1 │🮘 |
|
| 3 | 3 | ├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤▓ |
|
| 4 | - | │ 2 + new │🮘 |
|
| 5 | - | │ 3 + extra │🮘 |
|
| 4 | + | │ 3 + extra │▓ |
|
| 5 | + | │ 3 4 tail │🮘 |
crates/diff/tests/golden/sticky_header_starting_64x5.txt
+1 -1
| 1 | 1 | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 2 | 2 | │ ○ a.rs +2 -1 │▓ |
|
| 3 | 3 | ├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘 |
|
| 4 | - | │ 1 1 context │🮘 |
|
| 5 | 4 | │ 2 - old │🮘 |
|
| 5 | + | │ 2 + new │🮘 |