History

Disable diff hunks for untracked files, even w/ no newline at eof (#25980)

This fixes an issue where diff hunks were shown for untracked files, but only if the files did not end with a newline. Release Notes: - N/A

Max Brunsfeld committed 1 year ago

Commit 563baf61 parent 11b79d04 files · +34 −41Browse files

Showing the changes to crates/buffer_diff/src/buffer_diff.rs (1 of 4 files). Show all

Unified · Split

crates/buffer_diff/src/buffer_diff.rs+16−15
@@ -56,8 +56,8 @@ pub enum DiffHunkSecondaryStatus { · 55 unmodified lines
5656 /// A diff hunk resolved to rows in the buffer.
5757 #[derive(Debug, Clone, PartialEq, Eq)]
5858 pub struct DiffHunk {
59− /// The buffer range, expressed in terms of rows.
60− pub row_range: Range<u32>,
59+ /// The buffer range as points.
60+ pub range: Range<Point>,
6161 /// The range in the buffer to which this hunk corresponds.
6262 pub buffer_range: Range<Anchor>,
6363 /// The range in the buffer's diff base text to which this hunk corresponds.
@@ -362,6 +362,7 @@ impl BufferDiffInner { · 298 unmodified lines
362362 pending_hunks = secondary.pending_hunks.clone();
363363 }
364364
365+ let max_point = buffer.max_point();
365366 let mut summaries = buffer.summaries_for_anchors_with_payload::<Point, _, _>(anchor_iter);
366367 iter::from_fn(move || loop {
367368 let (start_point, (start_anchor, start_base)) = summaries.next()?;
@@ -371,7 +372,7 @@ impl BufferDiffInner { · 3 unmodified lines
371372 continue;
372373 }
373374
374− if end_point.column > 0 {
375+ if end_point.column > 0 && end_point < max_point {
375376 end_point.row += 1;
376377 end_point.column = 0;
377378 end_anchor = buffer.anchor_before(end_point);
@@ -416,7 +417,7 @@ impl BufferDiffInner { · 38 unmodified lines
416417 }
417418
418419 return Some(DiffHunk {
419− row_range: start_point.row..end_point.row,
420+ range: start_point..end_point,
420421 diff_base_byte_range: start_base..end_base,
421422 buffer_range: start_anchor..end_anchor,
422423 secondary_status,
@@ -442,14 +443,9 @@ impl BufferDiffInner { · 19 unmodified lines
442443
443444 let hunk = cursor.item()?;
444445 let range = hunk.buffer_range.to_point(buffer);
445− let end_row = if range.end.column > 0 {
446− range.end.row + 1
447− } else {
448− range.end.row
449− };
450446
451447 Some(DiffHunk {
452− row_range: range.start.row..end_row,
448+ range,
453449 diff_base_byte_range: hunk.diff_base_byte_range.clone(),
454450 buffer_range: hunk.buffer_range.clone(),
455451 // The secondary status is not used by callers of this method.
@@ -1136,12 +1132,10 @@ pub fn assert_hunks<Iter>( · 680 unmodified lines
11361132 let actual_hunks = diff_hunks
11371133 .map(|hunk| {
11381134 (
1139− hunk.row_range.clone(),
1135+ hunk.range.clone(),
11401136 &diff_base[hunk.diff_base_byte_range.clone()],
11411137 buffer
1142− .text_for_range(
1143− Point::new(hunk.row_range.start, 0)..Point::new(hunk.row_range.end, 0),
1144− )
1138+ .text_for_range(hunk.range.clone())
11451139 .collect::<String>(),
11461140 hunk.status(),
11471141 )
@@ -1150,7 +1144,14 @@ pub fn assert_hunks<Iter>( · 2 unmodified lines
11501144
11511145 let expected_hunks: Vec<_> = expected_hunks
11521146 .iter()
1153− .map(|(r, s, h, status)| (r.clone(), *s, h.to_string(), *status))
1147+ .map(|(r, old_text, new_text, status)| {
1148+ (
1149+ Point::new(r.start, 0)..Point::new(r.end, 0),
1150+ *old_text,
1151+ new_text.to_string(),
1152+ *status,
1153+ )
1154+ })
11541155 .collect();
11551156
11561157 assert_eq!(actual_hunks, expected_hunks);