Skip to content

Commit 3ae83d5

Browse files
committed
print final unified context
1 parent c87446b commit 3ae83d5

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

text/diff_util/file_diff.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl<'a> FileDiff<'a> {
9393
&mut lcs_indices,
9494
);
9595

96+
println!("lcs_indices: {:?}", lcs_indices);
9697
diff.hunks
9798
.create_hunks_from_lcs(&lcs_indices, num_lines1, num_lines2);
9899

@@ -412,29 +413,40 @@ impl<'a> FileDiff<'a> {
412413
// do we have enough context between hunks?
413414
if (diff_disp.curr_pos1 != 0) && (hunk.ln1_start() - diff_disp.curr_pos1 > unified * 2)
414415
{
415-
// print the context after the previous hunk
416+
// add context after the previous hunk
416417
diff_disp.write_line(
417418
self.file1,
418419
diff_disp.curr_pos1,
419420
diff_disp.curr_pos1 + unified,
420421
" ",
421422
)?;
422-
// print a new section start
423+
// update current position and print the whole section
423424
diff_disp.update_curr_pos(hunk.ln1_start() - unified, hunk.ln2_start() - unified);
424425
diff_disp.print_section();
425426
}
426427

427-
// print context before current hunk
428+
// add context before current hunk
428429
diff_disp.write_line(self.file1, diff_disp.curr_pos1, hunk.ln1_start(), " ")?;
429-
// print delete hunk
430+
// add delete hunk
430431
diff_disp.write_line(self.file1, hunk.ln1_start(), hunk.ln1_end(), "-")?;
431-
// print insert hunk
432+
// add insert hunk
432433
diff_disp.write_line(self.file2, hunk.ln2_start(), hunk.ln2_end(), "+")?;
433434
}
434435

435436
// print final hunk
436437
if !diff_disp.hunk_lines.is_empty() {
437438
diff_disp.print_section();
439+
// display the remaining context if possible
440+
if diff_disp.curr_pos1 < self.file1.lines().len() {
441+
let end = self.file1.lines().len().min(diff_disp.curr_pos1 + unified);
442+
diff_disp.write_line(
443+
self.file1,
444+
diff_disp.curr_pos1,
445+
end,
446+
" ",
447+
)?;
448+
diff_disp.print_hunk();
449+
}
438450
}
439451

440452
if !self.file1.ends_with_newline() {
@@ -524,14 +536,19 @@ impl DiffDisplay {
524536
"@@ -{},{} +{},{} @@",
525537
self.context_start1, self.hunk1_len, self.context_start2, self.hunk2_len
526538
);
539+
self.print_hunk();
540+
self.context_start1 = self.curr_pos1 + 1;
541+
self.context_start2 = self.curr_pos2 + 1;
542+
self.hunk1_len = 0;
543+
self.hunk2_len = 0;
544+
}
545+
546+
pub fn print_hunk(&mut self) {
527547
if self.hunk_lines.ends_with('\n') {
528548
self.hunk_lines.pop();
529549
}
530550
println!("{}", self.hunk_lines);
531551
self.hunk_lines.clear();
532-
self.context_start1 = self.curr_pos1 + 1;
533-
self.context_start2 = self.curr_pos2 + 1;
534-
self.hunk1_len = 0;
535-
self.hunk2_len = 0;
536552
}
553+
537554
}

0 commit comments

Comments
 (0)