diff-highlight: refactor to prepare for multi-line hunks

The current code structure assumes that we will only look at
a pair of lines at any given time, and that the end result
should always be to output that pair. However, we want to
eventually handle multi-line hunks, which will involve
collating pairs of removed/added lines. Let's refactor the
code to return highlighted pairs instead of printing them.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2012-02-13 17:33:10 -05:00 committed by Junio C Hamano
parent 097128d1bc
commit 6463fd7ed1

View file

@ -23,7 +23,7 @@ while (<>) {
$window[2] =~ /^$COLOR*\+/ && $window[2] =~ /^$COLOR*\+/ &&
$window[3] !~ /^$COLOR*\+/) { $window[3] !~ /^$COLOR*\+/) {
print shift @window; print shift @window;
show_pair(shift @window, shift @window); show_hunk(shift @window, shift @window);
} }
else { else {
print shift @window; print shift @window;
@ -48,7 +48,7 @@ if (@window == 3 &&
$window[1] =~ /^$COLOR*-/ && $window[1] =~ /^$COLOR*-/ &&
$window[2] =~ /^$COLOR*\+/) { $window[2] =~ /^$COLOR*\+/) {
print shift @window; print shift @window;
show_pair(shift @window, shift @window); show_hunk(shift @window, shift @window);
} }
# And then flush any remaining lines. # And then flush any remaining lines.
@ -58,7 +58,13 @@ while (@window) {
exit 0; exit 0;
sub show_pair { sub show_hunk {
my ($a, $b) = @_;
print highlight_pair($a, $b);
}
sub highlight_pair {
my @a = split_line(shift); my @a = split_line(shift);
my @b = split_line(shift); my @b = split_line(shift);
@ -106,12 +112,12 @@ sub show_pair {
} }
if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) { if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
print highlight(\@a, $pa, $sa); return highlight_line(\@a, $pa, $sa),
print highlight(\@b, $pb, $sb); highlight_line(\@b, $pb, $sb);
} }
else { else {
print join('', @a); return join('', @a),
print join('', @b); join('', @b);
} }
} }
@ -121,7 +127,7 @@ sub split_line {
split /($COLOR*)/; split /($COLOR*)/;
} }
sub highlight { sub highlight_line {
my ($line, $prefix, $suffix) = @_; my ($line, $prefix, $suffix) = @_;
return join('', return join('',