Skip to content

Commit 0fd0082

Browse files
krishhggclaude
andcommitted
Merge #77's side-by-side number fix into the file tree branch
The new test counts rows from the one after the file's header, which sits a row lower here under the tree's row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 parents c5f908d + a1788a2 commit 0fd0082

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎src/tui/pull_requests/code.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,12 +1944,21 @@ fn body(
19441944
_ => "",
19451945
};
19461946
let mut wanted = Vec::new();
1947+
// The largest number before the change of those lines, which only the left side shows.
1948+
let mut old_last = 0;
19471949
for (stretch, showing) in stretches.iter().zip(&showings) {
19481950
let end = stretch.first + showing.head + showing.tail + rest(showing.rest);
1951+
let before = wanted.len();
19491952
wanted.extend(stretch.first..stretch.first + showing.head);
19501953
wanted.extend(end - showing.tail..end);
1954+
if let Some(&number) = wanted[before..].last() {
1955+
old_last = old_last.max(stretch.old + (number - stretch.first));
1956+
}
19511957
}
19521958
let mut found = lines_at(contents, &wanted).into_iter();
1959+
// Side by side, each side has its own numbers and marks, as the narrower left one has room
1960+
// for them.
1961+
let halves = split.then(|| Halves::of(width)).flatten();
19531962
let last = file
19541963
.hunks
19551964
.iter()
@@ -1958,12 +1967,10 @@ fn body(
19581967
old.max(hunk.new_start + hunk.new_count.saturating_sub(1))
19591968
})
19601969
.chain(wanted.last().copied())
1970+
.chain(halves.map(|_| old_last))
19611971
.max()
19621972
.unwrap_or_default();
19631973
let digits = last.max(1).ilog10() as usize + 1;
1964-
// Side by side, each side has its own numbers and marks, as the narrower left one has room
1965-
// for them.
1966-
let halves = split.then(|| Halves::of(width)).flatten();
19671974
let mut slides = Slides {
19681975
halves,
19691976
..Slides::default()

‎src/tui/pull_requests/code/tests.rs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3752,6 +3752,51 @@ fn unchanged_lines_side_by_side_show_their_number_on_each_side() {
37523752
assert_eq!(slid.widest, "13 l14".width() + BETWEEN.width() + 19);
37533753
}
37543754

3755+
#[test]
3756+
fn the_numbers_side_by_side_fit_an_unchanged_line_s_old_place() {
3757+
// Five lines removed at the top put each line after them five places further on the left,
3758+
// so the last of those shown is old line 10 beside new line 5.
3759+
let patch = "diff --git a/g.rs b/g.rs
3760+
--- a/g.rs
3761+
+++ b/g.rs
3762+
@@ -1,6 +1 @@
3763+
-o1
3764+
-o2
3765+
-o3
3766+
-o4
3767+
-o5
3768+
l1
3769+
";
3770+
let mut code = read(slice(patch, None), 0);
3771+
code.act(Control::File(0), &[]);
3772+
code.act(Control::Layout, &[]);
3773+
for place in 0..2 {
3774+
code.act(Control::Stretch(0, place), &[]);
3775+
}
3776+
assert!(code.ask_text(2).is_some());
3777+
assert!(code.take_text(2, text(numbered(5))));
3778+
for place in 0..2 {
3779+
code.act(Control::Stretch(0, place), &[]);
3780+
}
3781+
let removed = (1..=5).map(|old| format!("{old:>2} - {:<14}│", format!("o{old}")));
3782+
let kept = (1..=5).map(|new| {
3783+
let old = new + 5;
3784+
format!("{old:>2} {:<14}│ {new:>2} l{new}", format!("l{new}"))
3785+
});
3786+
assert_eq!(
3787+
rows(&code, 40)[8..],
3788+
removed.chain(kept).collect::<Vec<_>>()
3789+
);
3790+
// Stacked, an unchanged line shows only its number after the change.
3791+
code.act(Control::Layout, &[]);
3792+
let removed = (1..=5).map(|old| format!("{old} - o{old}"));
3793+
let kept = (1..=5).map(|new| format!("{new} l{new}"));
3794+
assert_eq!(
3795+
rows(&code, 40)[8..],
3796+
removed.chain(kept).collect::<Vec<_>>()
3797+
);
3798+
}
3799+
37553800
#[test]
37563801
fn only_a_file_added_or_deleted_keeps_one_column_side_by_side() {
37573802
let mut code = read(slice(PATCH, None), 0);

0 commit comments

Comments
 (0)