lib/Devel/Trepan/Complete.pm

filter: Statementstmt 42.1% 113 / 268 Branchbran 25.5% 25 / 98 Conditioncond 20.8% 5 / 24 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 63.3% 19 / 30 Podpod 50.0% 10 / 20 totaltotal 38.3% 175 / 456 CC 67 SCAR 32.5
CC
67
Cov
37%
filename_list
96.0
filename_list
96.0
complete_token_filtered
20.0
CRAP 25.8
« lib/Devel/Trepan/DB/Eval.pm ↑ Summary blib/lib/Devel/Trepan/CmdProcessor/Command/Subcmd/SubsubMgr.pm »
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2012, 2014 Rocky Bernstein <rocky@cpan.org>
3 13 ▶use warnings; use strict; use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Devel::Trepan::Complete;
7 =head1 SUMMARY
8  
9 Completion routines for L<Devel::Trepan>
10  
11 =cut
12 13 ▶use vars qw(@ISA @EXPORT); @ISA = qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 @EXPORT = qw(
14 complete_builtins complete_subs complete_packages
15 complete_token complete_token_with_next
16 next_token signal_complete
17 complete_token_filtered_with_next);
18  
19 13 ▶use constant BUILTIN_CONST => qw(__FILE__ __LINE__ __PACKAGE__);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use constant BUILTIN_FNS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 abs accept alarm
22 and atan2 bind binmode bless caller
23 chdir chmod chown chr chroot close
24 closedir cmp connect continue cos crypt
25 dbmclose dbmopen die dump endgrent
26 endhostent endnetent endprotoent endpwent
27 endservent eof eq exec exit exp fcntl
28 fileno flock fork formline ge getc
29 getgrent getgrgid getgrnam gethostbyaddr
30 gethostbyname gethostent getlogin
31 getnetbyaddr getnetbyname getnetent
32 getpeername getpgrp getppid getpriority
33 getprotobyname getprotobynumber getprotoent
34 getpwent getpwnam getpwuid getservbyname
35 getservbyport getservent getsockname
36 getsockopt glob gmtime gt hex index int
37 ioctl join kill lc lcfirst le length
38 link listen localtime lock log lstat lt
39 mkdir msgctl msgget msgrcv msgsnd ne
40 not oct open opendir or ord pack pipe
41 quotemeta rand read readdir readline
42 readlink readpipe recv ref rename require
43 reset reverse rewinddir rindex rmdir seek
44 seekdir select semctl semget semop send
45 setgrent sethostent setnetent setpgrp
46 setpriority setprotoent setpwent setservent
47 setsockopt shmctl shmget shmread shmwrite
48 shutdown sin sleep socket socketpair
49 sprintf sqrt srand stat substr symlink
50 syscall sysopen sysread system syswrite tell
51 telldir time times truncate uc ucfirst
52 umask unlink unpack utime values vec
53 wait waitpid wantarray warn write x xor
54 13 );
55  
56 13 ▶use constant BUILTIN_CORE_FNS => map { 'CORE::' . $_ } BUILTIN_FNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 =head2 Subroutines
59  
60 =head3 complete_token
61  
62 Return an list of string, I<$complete_ary>, which start out with
63 String I<$prefix>.
64  
65 =cut
66 sub complete_token($$)
67 41 ▶{
Subroutine complete_token: called CC=3 SCAR=14.2
Pod documented
68 0 ▶ my ($complete_ary, $prefix) = @_;
Subroutine complete_token: not called CC=3 SCAR=14.2
Pod documented
69 0 my @result = ();
70 0 for my $cmd (@$complete_ary) {
71 0 ▶ if (0 == index($cmd, $prefix)) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == index($cmd, $prefix)) 1116 6570 7686
if (0 == index($cmd, $prefix)) 0 0 0
72 0 push @result, $cmd ;
73 }
74 }
75 0 sort @result;
76 }
77  
78 sub complete_token_with_next($$;$)
79 32 ▶{
Subroutine complete_token_with_next: called CC=4 SCAR=18.6
Pod undocumented
80 0 ▶ my ($complete_hash, $prefix, $cmd_prefix) = @_;
Subroutine complete_token_with_next: not called CC=4 SCAR=18.6
Pod undocumented
81 0 ▶ $cmd_prefix ='' if scalar(@_) < 3;
Branch: 2 branches
BranchTrueFalseTotal
if scalar @_ < 3 32 0 32
if scalar @_ < 3 0 0 0
82 0 my $cmd_prefix_len = length($cmd_prefix);
83 0 my @result = ();
84 0 ▶ while (my ($cmd_name, $cmd_obj) = each %{$complete_hash}) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$complete_hash;}) 838 32 870
if (() = each %{$complete_hash;}) 0 0 0
85 0 ▶ if (0 == index($cmd_name, $cmd_prefix . $prefix)) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == index($cmd_name, $cmd_prefix . $prefix)) 67 771 838
if (0 == index($cmd_name, $cmd_prefix . $prefix)) 0 0 0
86 0 push @result, [substr($cmd_name, $cmd_prefix_len), $cmd_obj]
87 }
88 }
89 0 sort {$a->[0] cmp $b->[0]} @result;
90 }
91  
92 =head3 complete_token_filtered
93  
94 Find all starting matches in Hash I<$aliases+>that start with
95 I<$prefix>, but filter out any matches already in I<$expanded>.
96  
97 =cut
98 sub complete_token_filtered($$$)
99 0 ▶{
Subroutine complete_token_filtered: not called CC=4 SCAR=30.0
Pod documented
100 0 ▶ my ($aliases, $prefix, $expanded) = @_;
Subroutine complete_token_filtered: not called CC=4 SCAR=30.0
Pod documented
101 0 my @complete_ary = keys %{$aliases};
102 0 my @result = ();
103 0 for my $cmd (@complete_ary) {
104 push @result, $cmd if
105 0 ▶ 0 == index($cmd, $prefix) && !exists $expanded->{$aliases->{$cmd}};
Branch: 2 branches
BranchTrueFalseTotal
if 0 == index($cmd, $prefix) and not exists $$expanded{$aliases->{$cmd}} 0 0 0
if 0 == index($cmd, $prefix) and not exists $$expanded{$aliases->{$cmd}} 0 0 0
Condition0%0/6
0 == index($cmd, $prefix) and not exists $$expanded{$aliases->{$cmd}}
!ll&&!rl&&r
000
0 == index($cmd, $prefix) and not exists $$expanded{$aliases->{$cmd}}
!ll&&!rl&&r
000
MC/DC0%0/2
0 == index($cmd, $prefix) and not exists $$expanded{$aliases->{$cmd}}
0 == index($cmd, $prefix) not exists $$expanded{$aliases->{$cmd}}
MC/DC0%0/2
0 == index($cmd, $prefix) and not exists $$expanded{$aliases->{$cmd}}
0 == index($cmd, $prefix) not exists $$expanded{$aliases->{$cmd}}
Truth table0%0/3
A and not(B)
A= 0 == index($cmd, $prefix)B= not exists $$expanded{$aliases->{$cmd}}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= 0 == index($cmd, $prefix)B= not exists $$expanded{$aliases->{$cmd}}
ABresult
0X0
100
111
106 }
107 0 sort @result;
108 }
109  
110 =head3 complete_token_filtered_with_next
111  
112 Find all starting matches in hash I<$aliases> that start with I<$prefix>,
113 but filter out any matches already in I<$expanded>.
114  
115 =cut
116 sub complete_token_filtered_with_next($$$$)
117 18 ▶{
Subroutine complete_token_filtered_with_next: called CC=4 SCAR=17.9
Pod documented
118 0 ▶ my ($aliases, $prefix, $expanded, $commands) = @_;
Subroutine complete_token_filtered_with_next: not called CC=4 SCAR=17.9
Pod documented
119 # require Enbugger; Enbugger->stop;
120 0 my @complete_ary = keys %{$aliases};
121 0 my %expanded = %{$expanded};
122 0 my @result = ();
123 0 for my $cmd (@complete_ary) {
124 0 ▶ if (0 == index($cmd, $prefix) && !exists $expanded{$aliases->{$cmd}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == index($cmd, $prefix) and not exists $expanded{$aliases->{$cmd}}) 1 755 756
if (0 == index($cmd, $prefix) and not exists $expanded{$aliases->{$cmd}}) 0 0 0
Condition50%3/6
0 == index($cmd, $prefix) and not exists $expanded{$aliases->{$cmd}}
!ll&&!rl&&r
74871
0 == index($cmd, $prefix) and not exists $expanded{$aliases->{$cmd}}
!ll&&!rl&&r
000
MC/DC100%2/2
0 == index($cmd, $prefix) and not exists $expanded{$aliases->{$cmd}}
0 == index($cmd, $prefix) not exists $expanded{$aliases->{$cmd}}
MC/DC0%0/2
0 == index($cmd, $prefix) and not exists $expanded{$aliases->{$cmd}}
0 == index($cmd, $prefix) not exists $expanded{$aliases->{$cmd}}
Truth table100%3/3
A and not(B)
A= 0 == index($cmd, $prefix)B= not exists $expanded{$aliases->{$cmd}}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= 0 == index($cmd, $prefix)B= not exists $expanded{$aliases->{$cmd}}
ABresult
0X0
100
111
125 0 push @result, [$cmd, $commands->{$aliases->{$cmd}}];
126 }
127 }
128 0 @result;
129 }
130  
131 =head3 next_token
132  
133 Find the next token in str string from start_pos. We return
134 the token and the next blank position after the token or
135 length($str) if this is the last token. Tokens are delimited by
136 white space.
137  
138 =cut
139 sub next_token($$)
140 47 ▶{
Subroutine next_token: called CC=5 SCAR=23.8
Pod documented
141 0 ▶ my ($str, $start_pos) = @_;
Subroutine next_token: not called CC=5 SCAR=23.8
Pod documented
142 0 my $look_at = substr($str, $start_pos);
143 0 my $strlen = length($look_at);
144 0 ▶ return (1, '') if 0 == $strlen;
Branch: 2 branches
BranchTrueFalseTotal
if 0 == $strlen 1 46 47
if 0 == $strlen 0 0 0
145 0 my $next_nonblank_pos = $start_pos;
146 0 my $next_blank_pos;
147 0 ▶ if ($look_at =~ /^(\s*)(\S+)\s*/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($look_at =~ /^(\s*)(\S+)\s*/) { } 43 3 46
elsif ($look_at =~ /^(\s+)$/) { } 3 0 3
elsif ($look_at =~ /^(\S+)\s*/) { } 0 0 0
if ($look_at =~ /^(\s*)(\S+)\s*/) { } 0 0 0
elsif ($look_at =~ /^(\s+)$/) { } 0 0 0
elsif ($look_at =~ /^(\S+)\s*/) { } 0 0 0
148 0 $next_nonblank_pos += length($1);
149 0 $next_blank_pos = $next_nonblank_pos+length($2);
150 } elsif ($look_at =~ /^(\s+)$/) {
151 0 return ($start_pos + length($1), '');
152 } elsif ($look_at =~/^(\S+)\s*/) {
153 0 $next_blank_pos = $next_nonblank_pos + length($1);
154 } else {
155 0 die "Something is wrong in next_token";
156 }
157 0 my $token_size = $next_blank_pos - $next_nonblank_pos;
158 0 return ($next_blank_pos, substr($str, $next_nonblank_pos, $token_size));
159 }
160  
161 =head3 filename_list
162  
163 I<filename_list> is from I<Term::ReadLine::readline.pm>:
164  
165 For use in passing to completion_matches(), returns a list of
166 filenames that begin with the given pattern. The user of this
167 package can set I<$rl_completion_function> to 'rl_filename_list' to
168 restore the default of filename matching if they'd changed it
169 earlier, either directly or via I<&rl_basic_commands>.
170  
171 =cut
172 sub filename_list(;$$)
173 0 ▶{
Subroutine filename_list: not called CC=10 SCAR=45.6
Pod documented
174 0 ▶ my ($pattern, $add_suffix) = @_;
Subroutine filename_list: not called CC=10 SCAR=45.6
Pod documented
175 0 ▶ $pattern = '' unless defined $pattern;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $pattern 0 0 0
unless defined $pattern 0 0 0
176 0 ▶ $add_suffix = 0 unless defined $add_suffix;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $add_suffix 0 0 0
unless defined $add_suffix 0 0 0
177 # $pattern = glob($pattern) if substr($pattern, 0, 1) = '~';
178 0 ▶ my @files = (<$pattern*>);
Subroutine BEGIN: called CC=1 SCAR=0.6
179 0 ▶ if ($add_suffix) {
Branch: 2 branches
BranchTrueFalseTotal
if ($add_suffix) 0 0 0
if ($add_suffix) 0 0 0
180 0 foreach (@files) {
181 0 ▶ if (-l $_) {
Branch: 8 branches
BranchTrueFalseTotal
if (-l $_) { } 0 0 0
elsif (-d _) { } 0 0 0
elsif (-x _) { } 0 0 0
elsif (-S _ or -p _) { } 0 0 0
if (-l $_) { } 0 0 0
elsif (-d _) { } 0 0 0
elsif (-x _) { } 0 0 0
elsif (-S _ or -p _) { } 0 0 0
182 0 $_ .= '@';
183 } elsif (-d _) {
184 0 $_ .= '/';
185 } elsif (-x _) {
186 0 $_ .= '*';
187 ▶ } elsif (-S _ || -p _) {
Condition0%0/6
-S _ or -p _
l!l&&r!l&&!r
000
-S _ or -p _
l!l&&r!l&&!r
000
MC/DC0%0/2
-S _ or -p _
-S _ -p _
MC/DC0%0/2
-S _ or -p _
-S _ -p _
Truth table0%0/3
A or B
A= -S _B= -p _
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= -S _B= -p _
ABresult
1X1
011
000
188 0 $_ .= '=';
189 }
190 }
191 }
192 0 return @files;
193 }
194  
195 # Custom completion routines
196 13 my @signal_complete_completions=();
197 6 ▶sub signal_complete($) {
Subroutine signal_complete: called CC=2 SCAR=9.2
Pod undocumented
198 0 ▶ my ($prefix) = @_;
Subroutine signal_complete: not called CC=2 SCAR=9.2
Pod undocumented
199 0 ▶ unless(@signal_complete_completions) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@signal_complete_completions) 2 4 6
unless (@signal_complete_completions) 0 0 0
200 0 @signal_complete_completions = keys %SIG;
201 0 my $last_sig = scalar @signal_complete_completions;
202 push(@signal_complete_completions,
203 0 map({lc $_} @signal_complete_completions));
204 0 my @nums = (-$last_sig .. $last_sig);
205 0 push @signal_complete_completions, @nums;
206 }
207 0 complete_token(\@signal_complete_completions, $prefix);
208 }
209  
210 sub complete_builtins($)
211 5 ▶{
Subroutine complete_builtins: called CC=2 SCAR=9.2
Pod undocumented
212 0 ▶ my ($prefix) = @_;
Subroutine complete_builtins: not called CC=2 SCAR=9.2
Pod undocumented
213 0 my @builtin_fns = BUILTIN_FNS;
214 0 ▶ if (0 == index($prefix ,'CORE::')) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == index($prefix, 'CORE::')) { } 2 3 5
if (0 == index($prefix, 'CORE::')) { } 0 0 0
215 0 map { 'CORE::' . $_ }
216 complete_token(\@builtin_fns, substr($prefix, length('CORE::')));
217 } else {
218 0 complete_token(\@builtin_fns, $prefix);
219 }
220 }
221  
222 sub complete_subs($)
223 10 ▶{
Subroutine complete_subs: called CC=5 SCAR=24.2
Pod undocumented
224 0 ▶ my ($prefix) = @_;
Subroutine complete_subs: not called CC=5 SCAR=24.2
Pod undocumented
225 13 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
226 0 my @all_fns = sort((keys(%DB::sub),
227 BUILTIN_FNS, BUILTIN_CORE_FNS, BUILTIN_CONST));
228 0 my $have_fn_sigl = 0;
229 0 ▶ if (substr($prefix, 0, 1) eq '&') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($prefix, 0, 1) eq "&") 0 10 10
if (substr($prefix, 0, 1) eq "&") 0 0 0
230 0 @all_fns = map { '&' . $_ } @all_fns;
231 0 $have_fn_sigl = 1;
232 }
233 0 my @functions = complete_token(\@all_fns, $prefix);
234 0 ▶ if (scalar @functions == 0 && !($prefix =~ /::/)) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @functions == 0 and not $prefix =~ /::/) 4 6 10
if (scalar @functions == 0 and not $prefix =~ /::/) 0 0 0
Condition33%2/6
scalar @functions == 0 and not $prefix =~ /::/
!ll&&!rl&&r
604
scalar @functions == 0 and not $prefix =~ /::/
!ll&&!rl&&r
000
MC/DC50%1/2
scalar @functions == 0 and not $prefix =~ /::/
scalar @functions == 0 not $prefix =~ /::/
MC/DC0%0/2
scalar @functions == 0 and not $prefix =~ /::/
scalar @functions == 0 not $prefix =~ /::/
Truth table66%2/3
A and not(B)
A= scalar @functions == 0B= not $prefix =~ /::/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= scalar @functions == 0B= not $prefix =~ /::/
ABresult
0X0
100
111
235 0 my $pkg_prefix = $DB::package . '::';
236 0 ▶ if ($have_fn_sigl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($have_fn_sigl) { } 0 4 4
if ($have_fn_sigl) { } 0 0 0
237 0 my $new_prefix = '&' . $pkg_prefix . substr($prefix, 1);
238 0 @functions = map { substr($_, length($pkg_prefix)+1) }
239 complete_token(\@all_fns, $new_prefix);
240 } else {
241 0 my $new_prefix = $pkg_prefix . $prefix;
242 0 @functions = map { substr($_, length($pkg_prefix)) }
243 complete_token(\@all_fns, $new_prefix);
244 }
245 }
246 0 return sort @functions;
247 }
248  
249 sub complete_packages($)
250 2 ▶{
Subroutine complete_packages: called CC=4 SCAR=17.2
Pod undocumented
251 0 ▶ my ($prefix) = @_;
Pod undocumented
252 0 my %pkgs;
253 13 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
254 0 foreach my $function (keys %DB::sub) {
255 0 my @parts = split('::', $function);
256 0 ▶ if (scalar @parts > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @parts > 1) 2 0 2
if (scalar @parts > 1) 0 0 0
257 0 pop(@parts);
258 2 my $pkg = join('::', @parts);
259 2 ▶ $pkgs{$pkg} = 1 if $pkg =~ /^$prefix/;
Branch: 2 branches
BranchTrueFalseTotal
if $pkg =~ /^$prefix/ 2 0 2
if $pkg =~ /^$prefix/ 0 0 0
260 }
261 }
262 2 return sort keys %pkgs;
263 }
264  
265 13 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 13 13
266 0 my $hash_ref = {'ab' => 1, 'aac' => 2, 'aa' => 3, 'b' => 4};
267 0 my @cmds = keys %{$hash_ref};
268 0 printf("complete_token(@cmds, '') => %s\n",
269 join(', ', complete_token(\@cmds, '')));
270 0 printf("complete_token(@cmds, 'a') => %s\n",
271 join(', ', complete_token(\@cmds, 'a')));
272 0 printf("complete_token(@cmds, 'b') => %s\n",
273 join(', ', complete_token(\@cmds, 'b')));
274 0 printf("complete_token(@cmds, 'c') => %s\n",
275 join(', ', complete_token(\@cmds, 'c')));
276 0 my @ary = complete_token_with_next($hash_ref, 'a');
277 0 my @ary_str = map "($_->[0], $_->[1])", @ary;
278 0 printf("complete_token_with_next(\$hash_ref, 'a') => %s\n",
279 join(', ', @ary_str));
280 0 print "0 1 \n";
281 0 print "0123456789012345678\n";
282 0 my $x = ' now is the time';
283 0 print "$x\n";
284 0 for my $pos (0, 2, 5, 6, 8, 9, 13, 18, 19) {
285 0 my @ary = next_token($x, $pos);
286 0 printf "next_token($pos) = %d, '%s'\n", $ary[0], $ary[1];
287 }
288 0 print "List of filenames:\n";
289 0 print join(', ', filename_list), "\n";
290 0 print "List of filenames beginning with C:\n";
291 0 print join(', ', filename_list('C')), "\n";
292  
293 0 print join(', ', signal_complete('C')), "\n";
294  
295 0 foreach my $prefix (qw(CORE::len len db foo CORE::foo)) {
296 0 printf("complete_subs($prefix) => %s\n",
297 join(', ', complete_subs($prefix)));
298 }
299  
300 0 $DB::package = 'main';
301 0 %DB::sub = qw(main::gcd 1);
302 0 foreach my $prefix (qw(end CORE::end gcd main::gcd foo CO __FI)) {
303 0 printf("complete_subs($prefix) => %s\n",
304 join(', ', complete_subs($prefix)));
305 }
306 0 my $prefix = 'mai';
307 0 printf("complete_packages($prefix) => %s\n",
308 join(', ', complete_packages($prefix)));
309  
310 # FIXME: We don't handle ~ expansion right now.
311 # print "List of filenames expanded from ~\n";
312 }
313  
314 13 1;
« lib/Devel/Trepan/DB/Eval.pm ↑ Summary blib/lib/Devel/Trepan/CmdProcessor/Command/Subcmd/SubsubMgr.pm »