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
56/// A diff hunk resolved to rows in the buffer.56/// A diff hunk resolved to rows in the buffer.
57#[derive(Debug, Clone, PartialEq, Eq)]57#[derive(Debug, Clone, PartialEq, Eq)]
58pub struct DiffHunk {58pub struct DiffHunk {
59 /// The buffer range, expressed in terms of rows.59 /// The buffer range as points.
60 pub row_range: Range<u32>,60 pub range: Range<Point>,
61 /// The range in the buffer to which this hunk corresponds.61 /// The range in the buffer to which this hunk corresponds.
62 pub buffer_range: Range<Anchor>,62 pub buffer_range: Range<Anchor>,
63 /// The range in the buffer's diff base text to which this hunk corresponds.63 /// The range in the buffer's diff base text to which this hunk corresponds.
@@ -362,6 +362,7 @@ impl BufferDiffInner { · 298 unmodified lines
362 pending_hunks = secondary.pending_hunks.clone();362 pending_hunks = secondary.pending_hunks.clone();
363 }363 }
364 364
365 let max_point = buffer.max_point();
365 let mut summaries = buffer.summaries_for_anchors_with_payload::<Point, _, _>(anchor_iter);366 let mut summaries = buffer.summaries_for_anchors_with_payload::<Point, _, _>(anchor_iter);
366 iter::from_fn(move || loop {367 iter::from_fn(move || loop {
367 let (start_point, (start_anchor, start_base)) = summaries.next()?;368 let (start_point, (start_anchor, start_base)) = summaries.next()?;
@@ -371,7 +372,7 @@ impl BufferDiffInner { · 3 unmodified lines
371 continue;372 continue;
372 }373 }
373 374
374 if end_point.column > 0 {375 if end_point.column > 0 && end_point < max_point {
375 end_point.row += 1;376 end_point.row += 1;
376 end_point.column = 0;377 end_point.column = 0;
377 end_anchor = buffer.anchor_before(end_point);378 end_anchor = buffer.anchor_before(end_point);
@@ -416,7 +417,7 @@ impl BufferDiffInner { · 38 unmodified lines
416 }417 }
417 418
418 return Some(DiffHunk {419 return Some(DiffHunk {
419 row_range: start_point.row..end_point.row,420 range: start_point..end_point,
420 diff_base_byte_range: start_base..end_base,421 diff_base_byte_range: start_base..end_base,
421 buffer_range: start_anchor..end_anchor,422 buffer_range: start_anchor..end_anchor,
422 secondary_status,423 secondary_status,
@@ -442,14 +443,9 @@ impl BufferDiffInner { · 19 unmodified lines
442 443
443 let hunk = cursor.item()?;444 let hunk = cursor.item()?;
444 let range = hunk.buffer_range.to_point(buffer);445 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 };
450 446
451 Some(DiffHunk {447 Some(DiffHunk {
452 row_range: range.start.row..end_row,448 range,
453 diff_base_byte_range: hunk.diff_base_byte_range.clone(),449 diff_base_byte_range: hunk.diff_base_byte_range.clone(),
454 buffer_range: hunk.buffer_range.clone(),450 buffer_range: hunk.buffer_range.clone(),
455 // The secondary status is not used by callers of this method.451 // The secondary status is not used by callers of this method.
@@ -1136,12 +1132,10 @@ pub fn assert_hunks<Iter>( · 680 unmodified lines
1136 let actual_hunks = diff_hunks1132 let actual_hunks = diff_hunks
1137 .map(|hunk| {1133 .map(|hunk| {
1138 (1134 (
1139 hunk.row_range.clone(),1135 hunk.range.clone(),
1140 &diff_base[hunk.diff_base_byte_range.clone()],1136 &diff_base[hunk.diff_base_byte_range.clone()],
1141 buffer1137 buffer
1142 .text_for_range(1138 .text_for_range(hunk.range.clone())
1143 Point::new(hunk.row_range.start, 0)..Point::new(hunk.row_range.end, 0),
1144 )
1145 .collect::<String>(),1139 .collect::<String>(),
1146 hunk.status(),1140 hunk.status(),
1147 )1141 )
@@ -1150,7 +1144,14 @@ pub fn assert_hunks<Iter>( · 2 unmodified lines
1150 1144
1151 let expected_hunks: Vec<_> = expected_hunks1145 let expected_hunks: Vec<_> = expected_hunks
1152 .iter()1146 .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 })
1154 .collect();1155 .collect();
1155 1156
1156 assert_eq!(actual_hunks, expected_hunks);1157 assert_eq!(actual_hunks, expected_hunks);