@@ -26,6 +26,7 @@ my $use_unicode_dash_for_ruler = git_config_boolean("diff-so-fancy.useUnicodeRul
26
26
my $ruler_width = git_config(" diff-so-fancy.rulerWidth" , undef );
27
27
my $git_strip_prefix = git_config_boolean(" diff.noprefix" ," false" );
28
28
my $has_stdin = has_stdin();
29
+ my $CONTEXT_LINES = undef ; # Number of lines of context diff used
29
30
30
31
my $ansi_regex = qr /\e\[ ([0-9]{1,3}(;[0-9]{1,3}){0,10})[mK]/ ;
31
32
my $ansi_color_regex = qr / (${ansi_regex} )?/ ;
@@ -139,6 +140,11 @@ do_dsf_stuff(\@lines);
139
140
sub do_dsf_stuff {
140
141
my $input = shift ();
141
142
143
+ # Calculate the context lines the first time
144
+ if (!defined $CONTEXT_LINES ) {
145
+ $CONTEXT_LINES = calculate_context_lines(@lines );
146
+ }
147
+
142
148
# print STDERR "START -------------------------------------------------\n";
143
149
# print STDERR join("",@$input);
144
150
# print STDERR "END ---------------------------------------------------\n";
@@ -534,16 +540,14 @@ sub start_line_calc {
534
540
return 1;
535
541
}
536
542
537
- # Git defaults to three lines of context
538
- my $default_context_lines = 3;
539
543
# Three lines on either side, and the line itself = 7
540
- my $expected_context = ($default_context_lines * 2 + 1);
544
+ my $expected_context = ($CONTEXT_LINES * 2 + 1);
541
545
542
546
# The first three lines
543
547
if ($line_num == 1 && $diff_context < $expected_context ) {
544
- $ret = $diff_context - $default_context_lines ;
548
+ $ret = $diff_context - $CONTEXT_LINES ;
545
549
} else {
546
- $ret = $line_num + $default_context_lines ;
550
+ $ret = $line_num + $CONTEXT_LINES ;
547
551
}
548
552
549
553
if ($ret < 1) {
@@ -1081,6 +1085,27 @@ sub debug_log {
1081
1085
return 1;
1082
1086
}
1083
1087
1088
+ sub calculate_context_lines {
1089
+ my @lines = @_ ;
1090
+ my $count = 0;
1091
+ my $hunk_line = 0;
1092
+
1093
+ foreach my $line (@lines ) {
1094
+ if ($line =~ / ^${ansi_color_regex} (@@@* .+? @@@*)(.*)/ ) {
1095
+ $hunk_line = $count ;
1096
+ } elsif ($hunk_line && $line =~ / ^${ansi_color_regex} [+-]/ ) {
1097
+ my $diff = $count - $hunk_line - 1;
1098
+ # print "Hunk: $hunk_line, ChangedLine: $count ($diff context)\n";
1099
+ return $diff ;
1100
+ }
1101
+
1102
+ $count ++;
1103
+ };
1104
+
1105
+ # If for some reason we can't figure it out, assume 3
1106
+ return 3;
1107
+ }
1108
+
1084
1109
# Borrowed from: https://www.perturb.org/display/1097_Perl_detect_if_a_module_is_installed_before_using_it.html
1085
1110
sub AUTOLOAD {
1086
1111
our $AUTOLOAD ; # keep 'use strict' happy
0 commit comments