blib/lib/Text/FindIndent.pm

filter: Statementstmt 81.0% 214 / 264 Branchbran 71.1% 121 / 170 Conditioncond 67.2% 41 / 61 MC/DCmcdc 48.7% 19 / 39 Subroutinesub 72.7% 16 / 22 Podpod 100% 2 / 2 totaltotal 74.0% 413 / 558 CC 110 SCAR 38.8
CC
110
Cov
76%
parse
84.1
to_vim_commands
51.9
_check_emacs_local_variables
14.0
CRAP 48.2
↑ Summary
1 package Text::FindIndent;
2 # -*- mode: Perl -*-
3 # Emacs mode is necessary for https://github.com/github/linguist/issues/2458
4  
5 2 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 $VERSION = '0.12';
11 }
12  
13 2 ▶use constant MAX_LINES => 500;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 21 ▶sub parse {
Subroutine parse: called CC=65 SCAR=44.3
Pod documented
16 21 my $class = shift;
17 21 my $text = shift;
18  
19 21 my %opts = @_;
20 21 ▶ my $textref = ref($text) ? $text : \$text; # accept references, too
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 0 21 21
21  
22 21 my $skip_pod = $opts{skip_pod};
23 21 ▶ my $first_level_indent_only = $opts{first_level_indent_only}?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'first_level_indent_only'} ? : 0 21 21
24  
25 21 my %modeline_settings;
26  
27 my %indentdiffs;
28 21 my $lines = 0;
29 21 my $prev_indent = undef;
30 21 my $skip = 0;
31 21 my $in_pod = 0;
32  
33 # Do we have emacs smart comments?
34 21 $class->_check_emacs_local_variables_at_file_end($textref, \%modeline_settings);
35 21 ▶ if (exists $modeline_settings{softtabstop} and exists $modeline_settings{usetabs}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $modeline_settings{'softtabstop'} and exists $modeline_settings{'usetabs'}) { } 0 21 21
elsif (exists $modeline_settings{'tabstop'} and $modeline_settings{'usetabs'}) { } 0 21 21
elsif (exists $modeline_settings{'tabstop'} and exists $modeline_settings{'usetabs'}) { } 1 40 41
Condition33%1/3
exists $modeline_settings{"softtabstop"} and exists $modeline_settings{"usetabs"}
!ll&&!rl&&r
2100
MC/DC0%0/2
exists $modeline_settings{"softtabstop"} and exists $modeline_settings{"usetabs"}
exists $modeline_settings{"softtabstop"} exists $modeline_settings{"usetabs"}
Truth table33%1/3
A and B
A= exists $modeline_settings{"softtabstop"}B= exists $modeline_settings{"usetabs"}
ABresult
0X0
100
111
36 $modeline_settings{mixedmode} = $modeline_settings{usetabs}
37 0 ▶ if not defined $modeline_settings{mixedmode};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $modeline_settings{"mixedmode"} 0 0 0
38 return(
39 ($modeline_settings{mixedmode} ? "m" : "s")
40 . $modeline_settings{softtabstop}
41 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$modeline_settings{'mixedmode'} ? : 0 0 0
42 }
43 ▶ elsif (exists $modeline_settings{tabstop} and $modeline_settings{usetabs}) {
Condition66%2/3
exists $modeline_settings{"tabstop"} and $modeline_settings{"usetabs"}
!ll&&!rl&&r
2010
MC/DC0%0/2
exists $modeline_settings{"tabstop"} and $modeline_settings{"usetabs"}
exists $modeline_settings{"tabstop"} $modeline_settings{"usetabs"}
Truth table66%2/3
A and B
A= exists $modeline_settings{"tabstop"}B= $modeline_settings{"usetabs"}
ABresult
0X0
100
111
44 0 ▶ return( ($modeline_settings{mixedmode} ? "m" : "t") . $modeline_settings{tabstop} );
Branch: 1 branch
BranchTrueFalseTotal
$modeline_settings{'mixedmode'} ? : 0 0 0
45 }
46 ▶ elsif (exists $modeline_settings{tabstop} and exists $modeline_settings{usetabs}) {
Condition66%2/3
exists $modeline_settings{"tabstop"} and exists $modeline_settings{"usetabs"}
!ll&&!rl&&r
2001
MC/DC50%1/2
exists $modeline_settings{"tabstop"} and exists $modeline_settings{"usetabs"}
exists $modeline_settings{"tabstop"} exists $modeline_settings{"usetabs"}
Truth table66%2/3
A and B
A= exists $modeline_settings{"tabstop"}B= exists $modeline_settings{"usetabs"}
ABresult
0X0
100
111
47 1 return( "s" . $modeline_settings{tabstop} );
48 }
49  
50 20 my $next_line_braces_pos_plus_1;
51 20 my $prev_indent_type = undef;
52 20 ▶ while ($$textref =~ /\G([ \t]*)([^\r\n]*)[\r\n]+/cgs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$textref =~ /\G([ \t]*)([^\r\n]*)[\r\n]+/cgs) 404 13 417
53 404 my $ws = $1;
54 404 my $rest = $2;
55 404 my $fullline = "$ws$rest";
56 404 $lines++;
57
58 # check emacs start line stuff with some slack (shebang)
59 404 my $changed_modelines;
60 404 ▶ if ($lines < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines < 3) 37 367 404
61 37 $changed_modelines = $class->_check_emacs_local_variables_first_line($fullline, \%modeline_settings);
62 }
63  
64 # Do we have emacs smart comments?
65 # ==> Done once at start
66 #$class->_check_emacs_local_variables($fullline, \%modeline_settings);
67  
68 # Do we have vim smart comments?
69 404 ▶ if ($class->_check_vim_modeline($fullline, \%modeline_settings) || $changed_modelines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_check_vim_modeline($fullline, \%modeline_settings) or $changed_modelines) 10 394 404
Condition100%3/3
$class->_check_vim_modeline($fullline, \%modeline_settings) or $changed_modelines
l!l&&r!l&&!r
64394
MC/DC100%2/2
$class->_check_vim_modeline($fullline, \%modeline_settings) or $changed_modelines
$class->_check_vim_modeline($fullline, \%modeline_settings) $changed_modelines
Truth table100%3/3
A or B
A= $class->_check_vim_modeline($fullline, \%modeline_settings)B= $changed_modelines
ABresult
1X1
011
000
70 10 ▶ if (exists $modeline_settings{softtabstop} and exists $modeline_settings{usetabs}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $modeline_settings{'softtabstop'} and exists $modeline_settings{'usetabs'}) { } 2 8 10
elsif (exists $modeline_settings{'tabstop'} and $modeline_settings{'usetabs'}) { } 2 6 8
elsif (exists $modeline_settings{'tabstop'} and exists $modeline_settings{'usetabs'}) { } 3 4 7
Condition100%3/3
exists $modeline_settings{"softtabstop"} and exists $modeline_settings{"usetabs"}
!ll&&!rl&&r
712
MC/DC100%2/2
exists $modeline_settings{"softtabstop"} and exists $modeline_settings{"usetabs"}
exists $modeline_settings{"softtabstop"} exists $modeline_settings{"usetabs"}
Truth table100%3/3
A and B
A= exists $modeline_settings{"softtabstop"}B= exists $modeline_settings{"usetabs"}
ABresult
0X0
100
111
71 $modeline_settings{mixedmode} = $modeline_settings{usetabs}
72 2 ▶ if not defined $modeline_settings{mixedmode};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $modeline_settings{"mixedmode"} 2 0 2
73 return(
74 ($modeline_settings{mixedmode} ? "m" : "s")
75 . $modeline_settings{softtabstop}
76 2 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$modeline_settings{'mixedmode'} ? : 1 1 2
77 }
78 ▶ elsif (exists $modeline_settings{tabstop} and $modeline_settings{usetabs}) {
Condition100%3/3
exists $modeline_settings{"tabstop"} and $modeline_settings{"usetabs"}
!ll&&!rl&&r
152
MC/DC100%2/2
exists $modeline_settings{"tabstop"} and $modeline_settings{"usetabs"}
exists $modeline_settings{"tabstop"} $modeline_settings{"usetabs"}
Truth table100%3/3
A and B
A= exists $modeline_settings{"tabstop"}B= $modeline_settings{"usetabs"}
ABresult
0X0
100
111
79 2 ▶ return( ($modeline_settings{mixedmode} ? "m" : "t") . $modeline_settings{tabstop} );
Branch: 1 branch
BranchTrueFalseTotal
$modeline_settings{'mixedmode'} ? : 1 1 2
80 }
81 ▶ elsif (exists $modeline_settings{tabstop} and exists $modeline_settings{usetabs}) {
Condition100%3/3
exists $modeline_settings{"tabstop"} and exists $modeline_settings{"usetabs"}
!ll&&!rl&&r
123
MC/DC100%2/2
exists $modeline_settings{"tabstop"} and exists $modeline_settings{"usetabs"}
exists $modeline_settings{"tabstop"} exists $modeline_settings{"usetabs"}
Truth table100%3/3
A and B
A= exists $modeline_settings{"tabstop"}B= exists $modeline_settings{"usetabs"}
ABresult
0X0
100
111
82 3 return( "s" . $modeline_settings{tabstop} );
83 }
84 }
85  
86 397 ▶ if ($lines > MAX_LINES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines > 500) 0 397 397
87 0 next;
88 }
89  
90 397 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 0 397 397
91 0 $skip--;
92 0 next;
93 }
94  
95 397 ▶ if ($skip_pod and $ws eq '' and substr($rest, 0, 1) eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_pod and $ws eq "" and substr($rest, 0, 1) eq "=") 4 393 397
Condition100%6/6
$skip_pod and $ws eq "" and substr($rest, 0, 1) eq "="
!ll&&!rl&&r
38494
$skip_pod and $ws eq ""
!ll&&!rl&&r
3463813
MC/DC100%3/3
$skip_pod and $ws eq "" and substr($rest, 0, 1) eq "="
$skip_pod $ws eq "" substr($rest, 0, 1) eq "="
Truth table100%4/4
A and B and C
A= $skip_podB= $ws eq ""C= substr($rest, 0, 1) eq "="
ABCresult
0XX0
10X0
1100
1111
96 4 ▶ if (not $in_pod and $rest =~ /^=(?:head\d|over|item|back|pod|begin|for|end)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_pod and $rest =~ /^=(?:head\d|over|item|back|pod|begin|for|end)/) { } 2 2 4
elsif ($in_pod and $rest =~ /^=cut/) { } 2 0 2
Condition66%2/3
not $in_pod and $rest =~ /^=(?:head\d|over|item|back|pod|begin|for|end)/
!ll&&!rl&&r
202
MC/DC50%1/2
not $in_pod and $rest =~ /^=(?:head\d|over|item|back|pod|begin|for|end)/
not $in_pod $rest =~ /^=(?:head\d|over|item|back|pod|begin|for|end)/
Truth table66%2/3
not(A) and B
A= not $in_podB= $rest =~ /^=(?:head\d|over|item|back|pod|begin|for|end)/
ABresult
0X0
100
111
97 2 $in_pod = 1;
98 }
99 ▶ elsif ($in_pod and $rest =~ /^=cut/) {
Condition33%1/3
$in_pod and $rest =~ /^=cut/
!ll&&!rl&&r
002
MC/DC0%0/2
$in_pod and $rest =~ /^=cut/
$in_pod $rest =~ /^=cut/
Truth table33%1/3
A and B
A= $in_podB= $rest =~ /^=cut/
ABresult
0X0
100
111
100 2 $in_pod = 0;
101 }
102  
103 }
104 397 ▶ next if $in_pod or $rest eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $in_pod or $rest eq "" 45 352 397
Condition100%3/3
$in_pod or $rest eq ""
l!l&&r!l&&!r
3213352
MC/DC100%2/2
$in_pod or $rest eq ""
$in_pod $rest eq ""
Truth table100%3/3
A or B
A= $in_podB= $rest eq ""
ABresult
1X1
011
000
105  
106 352 ▶ if ($ws eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ws eq "") 99 253 352
107 99 $prev_indent = $ws;
108 99 next;
109 }
110  
111 # skip next line if the last char is a backslash.
112 # Doesn't matter for Perl, but let's be generous!
113 253 ▶ $skip = 1 if $rest =~ /\\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\\$/ 0 253 253
114
115 # skip single-line comments
116 253 ▶ next if $rest =~ /^(?:#|\/\/|\/\*)/; # TODO: parse /* ... */!
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ m[^(?:#|//|/\*)] 0 253 253
117  
118 253 ▶ if ($next_line_braces_pos_plus_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line_braces_pos_plus_1) { } 10 243 253
119 10 ▶ if ($next_line_braces_pos_plus_1==_length_with_tabs_converted($ws)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line_braces_pos_plus_1 == _length_with_tabs_converted($ws)) 8 2 10
120 8 next;
121 }
122 2 $next_line_braces_pos_plus_1=0;
123 } else {
124 243 ▶ if ($rest =~ /=> \{$/) { #handle case where hash keys and values are indented by braces pos + 1
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /=> \{$/) 2 241 243
125 2 $next_line_braces_pos_plus_1=_length_with_tabs_converted($ws)+length($rest);
126 }
127 }
128  
129 245 ▶ if ($first_level_indent_only and $prev_indent ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_level_indent_only and $prev_indent ne "") 0 245 245
Condition33%1/3
$first_level_indent_only and $prev_indent ne ""
!ll&&!rl&&r
24500
MC/DC0%0/2
$first_level_indent_only and $prev_indent ne ""
$first_level_indent_only $prev_indent ne ""
Truth table33%1/3
A and B
A= $first_level_indent_onlyB= $prev_indent ne ""
ABresult
0X0
100
111
130 0 next;
131 }
132  
133 245 ▶ if ($prev_indent eq $ws) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_indent eq $ws) 70 175 245
134 70 ▶ if ($prev_indent_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_indent_type) 70 0 70
135 70 $indentdiffs{$prev_indent_type}+=0.01;
136 #coefficient is not based on data, so change if you think it should be different
137 }
138 70 next;
139 }
140  
141 # prefix-matching higher indentation level
142 175 ▶ if ($ws =~ /^\Q$prev_indent\E(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ws =~ /^\Q$prev_indent\E(.+)$/) 75 100 175
143 75 my $diff = $1;
144 75 my $indent_type=_analyse_indent_diff($diff);
145 75 $indentdiffs{$indent_type}++;
146 75 $prev_indent_type=$indent_type;
147 75 $prev_indent = $ws;
148 75 next;
149 }
150  
151 # prefix-matching lower indentation level
152 100 ▶ if ($prev_indent =~ /^\Q$ws\E(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_indent =~ /^\Q$ws\E(.+)$/) 42 58 100
153 42 my $diff = $1;
154 #_grok_indent_diff($diff, \%indentdiffs);
155 42 my $indent_type=_analyse_indent_diff($diff);
156 42 $indentdiffs{$indent_type}++;
157 42 $prev_indent_type=$indent_type;
158 42 $prev_indent = $ws;
159 42 next;
160 }
161  
162 # at this point, we're desperate!
163 58 my $prev_spaces = $prev_indent;
164 58 $prev_spaces =~ s/[ ]{0,7}\t/ /g;
165 58 my $spaces = $ws;
166 58 $spaces =~ s/[ ]{0,7}\t/ /g;
167 58 my $len_diff = length($spaces) - length($prev_spaces);
168 58 ▶ if ($len_diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_diff) 58 0 58
169 58 $len_diff = abs($len_diff);
170 58 $indentdiffs{"m$len_diff"}++;
171 }
172 58 $prev_indent = $ws;
173
174 } # end while lines
175  
176 # nothing found
177 13 ▶ return 'u' if not keys %indentdiffs;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %indentdiffs 1 12 13
178  
179 12 my $max = 0;
180 12 my $maxkey = undef;
181 12 ▶ while (my ($key, $value) = each %indentdiffs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %indentdiffs) 27 12 39
182 27 ▶ $maxkey = $key, $max = $value if $value > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $value > $max 18 9 27
183 }
184  
185 12 ▶ if ($maxkey =~ /^s(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxkey =~ /^s(\d+)$/) 8 4 12
186 8 my $mixedkey = "m" . $1;
187 8 my $mixed = $indentdiffs{$mixedkey};
188 8 ▶ if (defined($mixed) and $mixed >= $max * 0.2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mixed and $mixed >= $max * "0.2") 3 5 8
Condition66%2/3
defined $mixed and $mixed >= $max * "0.2"
!ll&&!rl&&r
503
MC/DC50%1/2
defined $mixed and $mixed >= $max * "0.2"
defined $mixed $mixed >= $max * "0.2"
Truth table66%2/3
A and B
A= defined $mixedB= $mixed >= $max * "0.2"
ABresult
0X0
100
111
189 3 $maxkey = $mixedkey;
190 }
191 }
192  
193 # fallback to emacs styles which are guesses only
194 12 foreach my $key (qw(softtabstop tabstop usetabs)) {
195 36 ▶ if (not exists $modeline_settings{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $modeline_settings{$key} and exists $modeline_settings{"style_$key"}) 6 30 36
Condition100%3/3
not exists $modeline_settings{$key} and exists $modeline_settings{"style_$key"}
!ll&&!rl&&r
3276
MC/DC100%2/2
not exists $modeline_settings{$key} and exists $modeline_settings{"style_$key"}
not exists $modeline_settings{$key} exists $modeline_settings{"style_$key"}
Truth table100%3/3
not(A) and B
A= not exists $modeline_settings{$key}B= exists $modeline_settings{"style_$key"}
ABresult
0X0
100
111
196 and exists $modeline_settings{"style_$key"}) {
197 6 $modeline_settings{$key} = $modeline_settings{"style_$key"};
198 }
199 }
200  
201 12 ▶ if (exists $modeline_settings{softtabstop}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $modeline_settings{'softtabstop'}) { } 3 9 12
elsif (exists $modeline_settings{'tabstop'}) { } 1 8 9
202 3 $maxkey =~ s/\d+/$modeline_settings{softtabstop}/;
203 }
204 elsif (exists $modeline_settings{tabstop}) {
205 1 $maxkey =~ s/\d+/$modeline_settings{tabstop}/;
206 }
207 12 ▶ if (exists $modeline_settings{usetabs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $modeline_settings{"usetabs"}) 2 10 12
208 2 ▶ if ($modeline_settings{usetabs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modeline_settings{'usetabs'}) { } 2 0 2
209 2 ▶ $maxkey =~ s/^(.)(\d+)$/$1 eq 'u' ? "t8" : ($2 == 8 ? "t8" : "m$2")/e;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq 'u' ? : 0 2 2
$2 == 8 ? : 1 1 2
210 }
211 else {
212 0 $maxkey =~ s/^./m/;
213 }
214 }
215  
216 # mixedmode explicitly asked for
217 12 ▶ if ($modeline_settings{mixedmode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modeline_settings{"mixedmode"}) 0 12 12
218 0 $maxkey =~ s/^./m/;
219 }
220  
221 12 return $maxkey;
222 }
223  
224 12 ▶sub _length_with_tabs_converted {
Subroutine _length_with_tabs_converted: called CC=3 SCAR=11.2
225 12 my $str=shift;
226 12 ▶ my $tablen=shift || 8;
Condition50%1/2
shift() || 8
l!l
012
MC/DC0%0/1
shift() || 8
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
227 12 $str =~ s/( +)$//;
228 12 ▶ my $trailing_spaces = $1||'';
Condition50%1/2
$1 || ''
l!l
120
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
229 12 $str =~ s/ +//g; # assume the spaces are all contained in tabs!
230 12 return length($str)*$tablen+length($trailing_spaces);
231 }
232  
233 0 ▶sub _grok_indent_diff {
Subroutine _grok_indent_diff: not called CC=3 SCAR=24.8
234 0 my $diff = shift;
235 0 my $indentdiffs = shift;
236  
237 0 ▶ if ($diff =~ /^ +$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff =~ /^ +$/) { } 0 0 0
elsif ($diff =~ /^\t+$/) { } 0 0 0
238 0 $indentdiffs->{"s" . length($diff)}++;
239 }
240 elsif ($diff =~ /^\t+$/) {
241 0 $indentdiffs->{"t8"}++; # we can't infer what a tab means. Or rather, we need smarter code to do it
242 }
243 else { # mixed!
244 0 $indentdiffs->{"m" . _length_with_tabs_converted($diff)}++;
245 }
246 }
247  
248 117 ▶sub _analyse_indent_diff {
Subroutine _analyse_indent_diff: called CC=3 SCAR=11.3
249 117 my $diff = shift;
250  
251 117 ▶ if ($diff =~ /^ +$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff =~ /^ +$/) { } 102 15 117
elsif ($diff =~ /^\t+$/) { } 15 0 15
252 102 return "s" . length($diff);
253 }
254 elsif ($diff =~ /^\t+$/) {
255 15 return "t8"; # we can't infer what a tab means. Or rather, we need smarter code to do it
256 }
257 else { # mixed!
258 0 return "m" . _length_with_tabs_converted($diff);
259 }
260 }
261  
262 {
263 # the vim modeline regexes
264 2 my $VimTag = qr/(?:ex|vim?(?:[<=>]\d+)?):/;
265 2 my $OptionArg = qr/[^\s\\]*(?:\\[\s\\][^\s\\]*)*/;
266 2 my $VimOption = qr/
267 \w+(?:=)?$OptionArg
268 /xo;
269  
270 2 my $VimModeLineStart = qr/(?:^|\s+)$VimTag/o;
271  
272 # while technically, we match against $VimModeLineStart before,
273 # IF there is a vim modeline, we don't need to optimize
274 2 my $VimModelineTypeOne = qr/
275 $VimModeLineStart
276 \s*
277 ($VimOption
278 (?:
279 (?:\s*:\s*|\s+)
280 $VimOption
281 )*
282 )
283 \s*$
284 /xo;
285
286 2 my $VimModelineTypeTwo = qr/
287 $VimModeLineStart
288 \s*
289 set?\s+
290 ($VimOption
291 (?:\s+$VimOption)*
292 )
293 \s*
294 :
295 /xo;
296  
297 404 ▶ sub _check_vim_modeline {
Subroutine _check_vim_modeline: called CC=11 SCAR=24.1
298 404 my $class = shift;
299 404 my $line = shift;
300 404 my $settings = shift;
301  
302 # Quoting the vim docs:
303 # There are two forms of modelines. The first form:
304 # [text]{white}{vi:|vim:|ex:}[white]{options}
305 #
306 #[text] any text or empty
307 #{white} at least one blank character (<Space> or <Tab>)
308 #{vi:|vim:|ex:} the string "vi:", "vim:" or "ex:"
309 #[white] optional white space
310 #{options} a list of option settings, separated with white space or ':',
311 # where each part between ':' is the argument for a ":set"
312 # command (can be empty)
313 #
314 #Example:
315 # vi:noai:sw=3 ts=6 ~
316 # The second form (this is compatible with some versions of Vi):
317 #
318 # [text]{white}{vi:|vim:|ex:}[white]se[t] {options}:[text]
319 #
320 #[text] any text or empty
321 #{white} at least one blank character (<Space> or <Tab>)
322 #{vi:|vim:|ex:} the string "vi:", "vim:" or "ex:"
323 #[white] optional white space
324 #se[t] the string "set " or "se " (note the space)
325 #{options} a list of options, separated with white space, which is the
326 # argument for a ":set" command
327 #: a colon
328 #[text] any text or empty
329 #
330 #Example:
331 # /* vim: set ai tw=75: */ ~
332 #
333
334 404 my @options;
335 404 ▶ return if $line !~ $VimModeLineStart;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$VimModeLineStart/ 398 6 404
336  
337 6 ▶ if ($line =~ $VimModelineTypeOne) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /$VimModelineTypeOne/) { } 2 4 6
elsif ($line =~ /$VimModelineTypeTwo/) { } 4 0 4
338 2 push @options, split /(?!<\\)[:\s]+/, $1;
339 }
340 elsif ($line =~ $VimModelineTypeTwo) {
341 4 push @options, split /(?!<\\)\s+/, $1;
342 }
343 else {
344 0 return;
345 }
346  
347 6 ▶ return if not @options;
Branch: 1 branch
BranchTrueFalseTotal
unless @options 0 6 6
348  
349 6 my $changed = 0;
350 6 foreach (@options) {
351 14 ▶ /s(?:ts|ofttabstop)=(\d+)/i and $settings->{softtabstop} = $1, $changed = 1, next;
Branch: 1 branch
BranchTrueFalseTotal
/s(?:ts|ofttabstop)=(\d+)/i and $settings->{"softtabstop"} = $1, $changed = 1, next 3 11 14
352 11 ▶ /t(?:s|abstop)=(\d+)/i and $settings->{tabstop} = $1, $changed = 1, next;
Branch: 1 branch
BranchTrueFalseTotal
/t(?:s|abstop)=(\d+)/i and $settings->{"tabstop"} = $1, $changed = 1, next 4 7 11
353 7 ▶ /((?:no)?)(?:expandtab|et)/i and $settings->{usetabs} = (defined $1 and $1 =~ /no/i ? 1 : 0), $changed = 1, next;
Branch: 2 branches
BranchTrueFalseTotal
/((?:no)?)(?:expandtab|et)/i and $settings->{"usetabs"} = defined $1 && ($1 =~ /no/i ? 1 : 0), $changed = 1, next 4 3 7
$1 =~ /no/i ? : 2 2 4
Condition66%2/3
defined $1 && ($1 =~ /no/i ? 1 : 0)
!ll&&!rl&&r
022
MC/DC50%1/2
defined $1 && ($1 =~ /no/i ? 1 : 0)
defined $1 ($1 =~ /no/i ? 1 : 0)
Truth table66%2/3
A && B
A= defined $1B= ($1 =~ /no/i ? 1 : 0)
ABresult
0X0
100
111
354 }
355 6 return $changed;
356 }
357 }
358  
359  
360  
361  
362 {
363 # lookup for emacs tab modes
364 2 my %tabmodelookup = (
365 1 ▶ t => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
366 1 $_[0]->{mixedmode} = 1;
367 1 $_[0]->{usetabs} = 1;
368 },
369 3 ▶ nil => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
370 3 delete $_[0]->{mixedmode};
371 3 $_[0]->{usetabs} = 0;
372 },
373 2 );
374  
375 # lookup for emacs styles
376 my %stylelookup = (
377 1 ▶ kr => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
378 1 $_[0]->{style_softtabstop} = 4;
379 1 $_[0]->{style_tabstop} = 8;
380 1 $_[0]->{style_usetabs} = 1;
381 },
382 1 ▶ linux => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
383 1 $_[0]->{style_softtabstop} = 8;
384 1 $_[0]->{style_tabstop} = 8;
385 1 $_[0]->{style_usetabs} = 1;
386 },
387 0 ▶ 'gnu' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
388 0 $_[0]->{style_softtabstop} = 2;
389 0 $_[0]->{style_tabstop} = 8;
390 0 $_[0]->{style_usetabs} = 1;
391 },
392 0 ▶ 'bsd' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
393 0 $_[0]->{style_softtabstop} = 4;
394 0 $_[0]->{style_tabstop} = 8;
395 0 $_[0]->{style_usetabs} = 1;
396 },
397 0 ▶ 'ellemtel' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
398 0 $_[0]->{style_softtabstop} = 3;
399 0 $_[0]->{style_tabstop} = 3;
400 0 $_[0]->{style_usetabs} = 0;
401 },
402 0 ▶ 'java' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
403 0 $_[0]->{style_softtabstop} = 4;
404 0 $_[0]->{style_tabstop} = 8;
405 },
406 2 );
407 2 $stylelookup{'k&r'} = $stylelookup{kr};
408 2 $stylelookup{'bsd'} = $stylelookup{kr};
409 2 $stylelookup{'whitesmith'} = $stylelookup{kr};
410 2 $stylelookup{'stroustrup'} = $stylelookup{kr};
411  
412 2 my $FirstLineVar = qr/[^\s:]+/;
413 2 my $FirstLineValue = qr/[^;]+/; # dumb
414 2 my $FirstLinePair = qr/\s*$FirstLineVar\s*:\s*$FirstLineValue;/o;
415 2 my $FirstLineRegexp = qr/-\*-\s*mode:\s*[^\s;]+;\s*($FirstLinePair+)\s*-\*-/o;
416
417
418 37 ▶ sub _check_emacs_local_variables_first_line {
Subroutine _check_emacs_local_variables_first_line: called CC=10 SCAR=23.4
419 37 my $class = shift;
420 37 my $line = shift;
421 37 my $settings = shift;
422  
423 # on first line (second if there is a shebang):
424 # -*- mode: $MODENAME; $VARNAME: $VALUE; ... -*-
425 # ($FOO is not a literal)
426 # Example with a Lisp comment:
427 # ;; -*- mode: Lisp; fill-column: 75; comment-column: 50; -*-
428  
429  
430 37 my $changed = 0;
431 37 ▶ if ($line =~ $FirstLineRegexp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$FirstLineRegexp/) 4 33 37
432 4 my @pairs = split /\s*;\s*/, $1;
433 4 foreach my $pair (@pairs) {
434 7 my ($key, $value) = split /\s*:\s*/, $pair, 2;
435 7 ▶ if ($key eq 'tab-width') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'tab-width') { } 1 6 7
elsif ($key eq 'indent-tabs-mode') { } 3 3 6
elsif ($key eq 'c-basic-offset') { } 2 1 3
elsif ($key eq 'style') { } 1 0 1
436 1 $settings->{tabstop} = $value;# FIXME: check var
437 1 $changed = 1;
438 }
439 elsif ($key eq 'indent-tabs-mode') {
440 3 ▶ $tabmodelookup{$value}->($settings), $changed = 1 if defined $tabmodelookup{$value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tabmodelookup{$value} 3 0 3
441 }
442 elsif ($key eq 'c-basic-offset') {
443 2 ▶ $settings->{tabstop} ||= $value; # tab-width takes precedence!?
Condition33%1/3
$settings->{"tabstop"} ||= $value
l!l&&r!l&&!r
020
MC/DC0%0/2
$settings->{"tabstop"} ||= $value
$settings->{"tabstop"} $value
Truth table33%1/3
A ||= B
A= $settings->{"tabstop"}B= $value
ABresult
1X1
011
000
444 2 $changed = 1;
445 }
446 elsif ($key eq 'style') { # this is quite questionable practice...
447 1 ▶ $stylelookup{$value}->($settings), $changed = 1 if defined $stylelookup{$value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $stylelookup{$value} 1 0 1
448 }
449 }
450 }
451  
452 # do this only as a LAST resort!
453 #$settings->{tabstop} = $settings->{style_tabstop} if not exists $settings->{tabstop};
454 #$settings->{softtabstop} = $settings->{style_softtabstop} if not exists $settings->{softtabstop};
455 #$settings->{usetabs} = $settings->{style_usetabs} if not exists $settings->{usetabs};
456  
457 37 return $changed;
458 }
459  
460 513 ▶ sub _check_emacs_local_variables {
Subroutine _check_emacs_local_variables: called CC=13 SCAR=26.4
461 513 my $class = shift;
462 513 my $line = shift;
463 513 my $settings = shift;
464  
465 # A local variables list goes near the end of the file, in the last page.[...]
466 # The local variables list starts with a line containing the string `Local Variables:',
467 # and ends with a line containing the string `End:'. In between come the variable names
468 # and values, one set per line, as `variable: value'. The values are not evaluated;
469 # they are used literally. If a file has both a local variables list and a `-*-'
470 # line, Emacs processes everything in the `-*-' line first, and everything in the
471 # local variables list afterward.
472 #
473 # Here is an example of a local variables list:
474 #
475 # ;; Local Variables: **
476 # ;; mode:lisp **
477 # ;; comment-column:0 **
478 # ;; comment-start: ";; " **
479 # ;; comment-end:"**" **
480 # ;; End: **
481 #
482 # Each line starts with the prefix `;; ' and each line ends with the suffix ` **'.
483 # Emacs recognizes these as the prefix and suffix based on the first line of the
484 # list, by finding them surrounding the magic string `Local Variables:'; then it
485 # automatically discards them from the other lines of the list.
486 #
487 # The usual reason for using a prefix and/or suffix is to embed the local variables
488 # list in a comment, so it won't confuse other programs that the file is intended as
489 # input for. The example above is for a language where comment lines start with `;; '
490 # and end with `**'; the local values for comment-start and comment-end customize the
491 # rest of Emacs for this unusual syntax. Don't use a prefix (or a suffix) if you don't need one.
492 #
493 #
494 # Can it be any more annoying to parse? --Steffen
495  
496 513 ▶ if ($settings->{in_local_variables_section}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($settings->{'in_local_variables_section'}) { } 5 508 513
elsif ($line =~ /^\s*(\S*)\s*Local Variables:\s*(\S*)\s*$/) { } 2 506 508
497 5 my $prefix = $settings->{local_variable_prefix};
498 5 ▶ $prefix = '' if not defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 5 5
499 5 $prefix = quotemeta($prefix);
500 5 my $suffix = $settings->{local_variable_suffix};
501 5 ▶ $suffix = '' if not defined $suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suffix 0 5 5
502 5 $suffix = quotemeta($suffix);
503  
504 5 ▶ if ($line =~ /^\s*$prefix\s*([^\s:]+):\s*(.+)$suffix\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$prefix\s*([^\s:]+):\s*(.+)$suffix\s*$/) { } 3 2 5
505 3 my $key = $1;
506 3 my $value = $2;
507 3 $value =~ s/\s+$//;
508 3 ▶ if ($key eq 'tab-width') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'tab-width') { } 0 3 3
elsif ($key eq 'indent-tabs-mode') { } 1 2 3
elsif ($key eq 'c-basic-offset') { } 1 1 2
elsif ($key eq 'style') { } 1 0 1
509 0 $settings->{tabstop} = $value;
510 }
511 elsif ($key eq 'indent-tabs-mode') {
512 1 ▶ $tabmodelookup{$value}->($settings) if defined $tabmodelookup{$value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tabmodelookup{$value} 1 0 1
513 }
514 elsif ($key eq 'c-basic-offset') {
515 1 ▶ $settings->{tabstop} ||= $value; # tab-width takes precedence!?
Condition33%1/3
$settings->{"tabstop"} ||= $value
l!l&&r!l&&!r
010
MC/DC0%0/2
$settings->{"tabstop"} ||= $value
$settings->{"tabstop"} $value
Truth table33%1/3
A ||= B
A= $settings->{"tabstop"}B= $value
ABresult
1X1
011
000
516 }
517 elsif ($key eq 'style') { # this is quite questionable practice...
518 1 ▶ $stylelookup{$value}->($settings) if defined $stylelookup{$value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $stylelookup{$value} 1 0 1
519 }
520 } # end if variable line
521 else {
522 2 delete $settings->{in_local_variables_section};
523 2 delete $settings->{local_variable_prefix};
524 2 delete $settings->{local_variable_suffix};
525 }
526 }
527 elsif ($line =~ /^\s*(\S*)\s*Local Variables:\s*(\S*)\s*$/) {
528 2 $settings->{local_variable_prefix} = $1;
529 2 $settings->{local_variable_suffix} = $2;
530 2 $settings->{in_local_variables_section} = 1;
531 }
532 }
533  
534 21 ▶ sub _check_emacs_local_variables_at_file_end {
Subroutine _check_emacs_local_variables_at_file_end: called CC=3 SCAR=11.0
535 21 my $class = shift;
536 21 my $textref = shift;
537 21 my $settings = shift;
538 21 my $len = length($$textref);
539 21 my $start = $len-3000;
540 21 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 21 0 21
541 21 my $text = substr($$textref, $start);
542  
543 21 ▶ while ($text =~ /\G[ \t]*([^\r\n]*)[\r\n]+/cgs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G[ \t]*([^\r\n]*)[\r\n]+/cgs) 513 21 534
544 513 $class->_check_emacs_local_variables($1, $settings);
545 }
546 21 return;
547 }
548 } # end lexical block for emacs lookups
549  
550  
551 2 ▶sub to_vim_commands {
Subroutine to_vim_commands: not called CC=7 SCAR=39.5
Pod documented
552 0 my $indent = shift;
553 0 ▶ $indent = shift if $indent eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $indent eq __PACKAGE__ 0 0 0
554 0 ▶ $indent = __PACKAGE__->parse($indent) if ref($indent) or length($indent) > 5;
Branch: 1 branch
BranchTrueFalseTotal
if ref $indent or length $indent > 5 0 0 0
Condition0%0/3
ref $indent or length $indent > 5
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $indent or length $indent > 5
ref $indent length $indent > 5
Truth table0%0/3
A or B
A= ref $indentB= length $indent > 5
ABresult
1X1
011
000
555  
556 0 my @cmd;
557 0 ▶ if ( $indent =~ /^t(\d+)/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($indent =~ /^t(\d+)/) { } 0 0 0
elsif ($indent =~ /^s(\d+)/) { } 0 0 0
elsif ($indent =~ /^m(\d+)/) { } 0 0 0
558 0 my $chars = $1;
559 0 push @cmd, ":set shiftwidth=$chars";
560 0 push @cmd, ":set tabstop=$chars";
561 0 push @cmd, ":set softtabstop=0";
562 0 push @cmd, ":set noexpandtab";
563 } elsif ( $indent =~ /^s(\d+)/ ) {
564 0 my $spaces = $1;
565 0 push @cmd, ":set shiftwidth=$spaces";
566 0 push @cmd, ":set tabstop=8";
567 0 push @cmd, ":set softtabstop=$spaces";
568 0 push @cmd, ':set expandtab';
569 } elsif ( $indent =~ /^m(\d+)/ ) {
570 0 my $spaces = $1;
571 0 push @cmd, ":set shiftwidth=$spaces";
572 0 push @cmd, ":set tabstop=8";
573 0 push @cmd, ":set softtabstop=$spaces";
574 0 push @cmd, ':set noexpandtab';
575 }
576 0 return @cmd;
577 }
578  
579 2 1;
580  
581 __END__
↑ Summary