blib/lib/Complete/Module.pm

filter: Statementstmt 89.8% 88 / 98 Branchbran 82.5% 33 / 40 Conditioncond 86.8% 33 / 38 MC/DCmcdc 81.8% 18 / 22 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 87.5% 183 / 209 CC 42 SCAR 29.5
CC
42
Cov
88%
complete_module
24.5
__ANON__
20.0
BEGIN
1.0
CRAP 19.0
↑ Summary
1 package Complete::Module;
2  
3 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Complete::Common qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13 3 our $DATE = '2023-03-19'; # DATE
14 3 our $DIST = 'Complete-Module'; # DIST
15 3 our $VERSION = '0.263'; # VERSION
16  
17 3 our %SPEC;
18  
19 3 our @EXPORT_OK = qw(complete_module);
20  
21 3 our $OPT_SHORTCUT_PREFIXES;
22 3 ▶if ($ENV{COMPLETE_MODULE_OPT_SHORTCUT_PREFIXES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'COMPLETE_MODULE_OPT_SHORTCUT_PREFIXES'}) { } 0 3 3
23 $OPT_SHORTCUT_PREFIXES =
24 0 { split /=|;/, $ENV{COMPLETE_MODULE_OPT_SHORTCUT_PREFIXES} };
25 } else {
26 3 $OPT_SHORTCUT_PREFIXES = {
27 #cp => 'Catalyst/Plugin/' # candidate
28 df => 'DateTime/Format/',
29 #dp => 'Dancer/Plugin/', # candidate
30 #d2p => 'Dancer2/Plugin/', # candidate
31 dz => 'Dist/Zilla/',
32 dzb => 'Dist/Zilla/PluginBundle/',
33 dzp => 'Dist/Zilla/Plugin/',
34 dzr => 'Dist/Zilla/Role/',
35 #pa => 'Plack/App/', # candidate
36 #pc => 'POE/Component/', # candidate
37 #pc => 'Perl/Critic/', # candidate?
38 #pcp => 'Perl/Critic/Policy/', # candidate?
39 #pd => 'Padre/Document/', # candidate
40 #pm => 'Plack/Middleware/', # candidate
41 #pp => 'Padre/Plugin/', # candidate
42 pw => 'Pod/Weaver/',
43 pwb => 'Pod/Weaver/PluginBundle/',
44 pwp => 'Pod/Weaver/Plugin/',
45 pwr => 'Pod/Weaver/Role/',
46 pws => 'Pod/Weaver/Section/',
47 #rtx => 'RT/Extension/', # candidate
48 #se => 'Search/Elasticsearch/', # candidate
49 #sec => 'Search/Elasticsearch/Client/', # candidate
50 #ser => 'Search/Elasticsearch/Role/', # candidate
51 #tp => 'Template/Plugin/', # candidate
52 #tw => 'Tickit/Widget/', # candidate
53  
54 # MooseX, MooX
55 # Moose::Exception
56 # Finance::Bank
57 # Mojo::*, MojoX::*, Mojolicious::*
58 };
59 }
60  
61 $SPEC{complete_module} = {
62 3 v => 1.1,
63 summary => 'Complete with installed Perl module names',
64 description => <<'_',
65  
66 For each directory in `@INC` (coderefs are ignored), find Perl modules and
67 module prefixes which have `word` as prefix. So for example, given `Te` as
68 `word`, will return e.g. `[Template, Template::, Term::, Test, Test::, Text::]`.
69 Given `Text::` will return `[Text::ASCIITable, Text::Abbrev, ...]` and so on.
70  
71 This function has a bit of overlapping functionality with <pm:Module::List>, but
72 this function is geared towards shell tab completion. Compared to Module::List,
73 here are some differences: 1) list modules where prefix is incomplete; 2)
74 interface slightly different; 3) (currently) doesn't do recursing; 4) contains
75 conveniences for completion, e.g. map casing, expand intermediate paths (see
76 `Complete` for more details on those features), autoselection of path separator
77 character, some shortcuts, and so on.
78  
79 _
80 args => {
81 %arg_word,
82 path_sep => {
83 summary => 'Path separator',
84 schema => 'str*',
85 description => <<'_',
86  
87 For convenience in shell (bash) completion, instead of defaulting to `::` all
88 the time, will look at `word`. If word does not contain any `::` then will
89 default to `/`. This is because `::` (contains colon) is rather problematic as
90 it is by default a word-break character in bash and the word needs to be quoted
91 to avoid word-breaking by bash.
92  
93 _
94 },
95 find_pm => {
96 summary => 'Whether to find .pm files',
97 schema => 'bool*',
98 default => 1,
99 },
100 find_pod => {
101 summary => 'Whether to find .pod files',
102 schema => 'bool*',
103 default => 1,
104 },
105 find_pmc => {
106 summary => 'Whether to find .pmc files',
107 schema => 'bool*',
108 default => 1,
109 },
110 find_prefix => {
111 summary => 'Whether to find module prefixes',
112 schema => 'bool*',
113 default => 1,
114 },
115 ns_prefix => {
116 summary => 'Namespace prefix',
117 schema => 'perl::modname*',
118 description => <<'_',
119  
120 This is useful if you want to complete module under a specific namespace
121 (instead of the root). For example, if you set `ns_prefix` to
122 `Dist::Zilla::Plugin` (or `Dist::Zilla::Plugin::`) and word is `F`, you can get
123 `['FakeRelease', 'FileFinder::', 'FinderCode']` (those are modules under the
124 `Dist::Zilla::Plugin::` namespace).
125  
126 _
127 },
128 ns_prefixes => {
129 summary => 'Namespace prefixes',
130 schema => ['array*', of=>'perl::modname*'],
131 description => <<'_',
132  
133 If you specify this instead of `ns_prefix`, then the routine will search from
134 all the prefixes instead of just one.
135  
136 _
137 },
138 recurse => {
139 schema => 'bool*',
140 cmdline_aliases => {r=>{}},
141 },
142 recurse_matching => {
143 schema => ['str*', in=>['level-by-level', 'all-at-once']],
144 default => 'level-by-level',
145 },
146 exclude_leaf => {
147 schema => 'bool*',
148 },
149 exclude_dir => {
150 schema => 'bool*',
151 },
152 },
153 args_rels => {
154 choose_one => [qw/ns_prefix ns_prefixes/],
155 },
156 result_naked => 1,
157 };
158 22 ▶sub complete_module {
Subroutine complete_module: called CC=23 SCAR=32.0
Pod documented
159 22 require Complete::Path;
160  
161 22 my %args = @_;
162  
163 22 ▶ my $word = $args{word} // '';
Condition50%1/2
$args{"word"} // ""
l!l
220
MC/DC0%0/1
$args{"word"} // ""
$args{"word"}
Truth table50%1/2
A
A= $args{"word"}
Aresult
11
00
164 #log_trace('[compmod] Entering complete_module(), word=<%s>', $word);
165 #log_trace('[compmod] args=%s', \%args);
166  
167 # convenience: allow Foo/Bar.{pm,pod,pmc}
168 22 $word =~ s/\.(pm|pmc|pod)\z//;
169  
170 # convenience (and compromise): if word doesn't contain :: we use the
171 # "safer" separator /, but if already contains '::' we use '::'. (Can also
172 # use '.' if user uses that.) Using "::" in bash means user needs to use
173 # quote (' or ") to make completion behave as expected since : is by default
174 # a word break character in bash/readline.
175 22 my $sep = $args{path_sep};
176 22 ▶ unless (defined $sep) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sep) 22 0 22
177 22 ▶ $sep = $word =~ /::/ ? '::' :
Branch: 2 branches
BranchTrueFalseTotal
$word =~ /::/ ? : 10 12 22
$word =~ /\./ ? : 0 12 12
178 $word =~ /\./ ? '.' : '/';
179 }
180  
181 # find shortcut prefixes
182 {
183 22 my $tmp = lc $word;
184 22 for (keys %$OPT_SHORTCUT_PREFIXES) {
185 211 ▶ if ($tmp =~ /\A\Q$_\E(?:(\Q$sep\E).*|\z)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ /\A\Q$_\E(?:(\Q$sep\E).*|\z)/) 1 210 211
186 substr($word, 0, length($_) + length($1 // '')) =
187 1 ▶ $OPT_SHORTCUT_PREFIXES->{$_};
Condition50%1/2
$1 // ""
l!l
01
MC/DC0%0/1
$1 // ""
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
188 1 last;
189 }
190 }
191 }
192  
193 22 $word =~ s!(::|/|\.)!::!g;
194  
195 22 ▶ my $find_pm = $args{find_pm} // 1;
Condition100%2/2
$args{"find_pm"} // 1
l!l
121
MC/DC100%1/1
$args{"find_pm"} // 1
$args{"find_pm"}
Truth table100%2/2
A
A= $args{"find_pm"}
Aresult
11
00
196 22 ▶ my $find_pmc = $args{find_pmc} // 1;
Condition100%2/2
$args{"find_pmc"} // 1
l!l
121
MC/DC100%1/1
$args{"find_pmc"} // 1
$args{"find_pmc"}
Truth table100%2/2
A
A= $args{"find_pmc"}
Aresult
11
00
197 22 ▶ my $find_pod = $args{find_pod} // 1;
Condition100%2/2
$args{"find_pod"} // 1
l!l
121
MC/DC100%1/1
$args{"find_pod"} // 1
$args{"find_pod"}
Truth table100%2/2
A
A= $args{"find_pod"}
Aresult
11
00
198 22 ▶ my $find_prefix = $args{find_prefix} // 1;
Condition100%2/2
$args{"find_prefix"} // 1
l!l
121
MC/DC100%1/1
$args{"find_prefix"} // 1
$args{"find_prefix"}
Truth table100%2/2
A
A= $args{"find_prefix"}
Aresult
11
00
199  
200 22 ▶ my @ns_prefixes = $args{ns_prefixes} ? @{$args{ns_prefixes}} : ($args{ns_prefix});
Branch: 1 branch
BranchTrueFalseTotal
$args{'ns_prefixes'} ? : 2 20 22
201 22 my $res = [];
202 22 for my $ns_prefix (@ns_prefixes) {
203 26 ▶ $ns_prefix //= '';
Condition100%2/2
$ns_prefix //= ""
l!l
1016
MC/DC100%1/1
$ns_prefix //= ""
$ns_prefix
Truth table100%2/2
A
A= $ns_prefix
Aresult
11
00
204 26 $ns_prefix =~ s/(::)+\z//;
205  
206 #log_trace('[compmod] invoking complete_path, word=<%s>', $word);
207 my $cp_res = Complete::Path::complete_path(
208 word => $word,
209 starting_path => $ns_prefix,
210 68 ▶ list_func => sub {
Subroutine __ANON__: called CC=20 SCAR=30.0
211 68 my ($path, $intdir, $isint) = @_;
212 68 (my $fspath = $path) =~ s!::!/!g;
213 68 my @res;
214 68 for my $inc (@INC) {
215 612 ▶ next if ref($inc);
Branch: 1 branch
BranchTrueFalseTotal
if ref $inc 0 612 612
216 612 ▶ my $dir = $inc . (length($fspath) ? "/$fspath" : "");
Branch: 1 branch
BranchTrueFalseTotal
length $fspath ? : 468 144 612
217 612 ▶ opendir my($dh), $dir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or next 196 416 612
218 196 for (readdir $dh) {
219 5876 ▶ next if $_ eq '.' || $_ eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "." or $_ eq ".." 392 5484 5876
Condition100%3/3
$_ eq "." or $_ eq ".."
l!l&&r!l&&!r
1961965484
MC/DC100%2/2
$_ eq "." or $_ eq ".."
$_ eq "." $_ eq ".."
Truth table100%3/3
A or B
A= $_ eq "."B= $_ eq ".."
ABresult
1X1
011
000
220 5484 ▶ next unless /\A\w+(\.\w+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\A\w+(\.\w+)?\z/ 176 5308 5484
221 5308 my $is_dir = (-d "$dir/$_");
222 5308 ▶ next if $isint && !$is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $isint and not $is_dir 2108 3200 5308
Condition100%3/3
$isint and not $is_dir
!ll&&!rl&&r
42027802108
MC/DC100%2/2
$isint and not $is_dir
$isint not $is_dir
Truth table100%3/3
A and not(B)
A= $isintB= not $is_dir
ABresult
0X0
100
111
223 3200 ▶ push @res, "$_\::" if $is_dir && ($isint || $find_prefix);
Branch: 1 branch
BranchTrueFalseTotal
if $is_dir and $isint || $find_prefix 3003 197 3200
Condition100%6/6
$is_dir and $isint || $find_prefix
!ll&&!rl&&r
19613003
$isint or $find_prefix
l!l&&r!l&&!r
27802231
MC/DC100%3/3
$is_dir and $isint || $find_prefix
$is_dir $isint $find_prefix
Truth table100%4/4
A and B or C
A= $is_dirB= $isintC= $find_prefix
ABCresult
0XX0
1000
11X1
1011
224 3200 ▶ push @res, $1 if /(.+)\.pm\z/ && $find_pm;
Branch: 1 branch
BranchTrueFalseTotal
if /(.+)\.pm\z/ and $find_pm 142 3058 3200
Condition100%3/3
/(.+)\.pm\z/ and $find_pm
!ll&&!rl&&r
30562142
MC/DC100%2/2
/(.+)\.pm\z/ and $find_pm
/(.+)\.pm\z/ $find_pm
Truth table100%3/3
A and B
A= /(.+)\.pm\z/B= $find_pm
ABresult
0X0
100
111
225 3200 ▶ push @res, $1 if /(.+)\.pmc\z/ && $find_pmc;
Branch: 1 branch
BranchTrueFalseTotal
if /(.+)\.pmc\z/ and $find_pmc 10 3190 3200
Condition100%3/3
/(.+)\.pmc\z/ and $find_pmc
!ll&&!rl&&r
3188210
MC/DC100%2/2
/(.+)\.pmc\z/ and $find_pmc
/(.+)\.pmc\z/ $find_pmc
Truth table100%3/3
A and B
A= /(.+)\.pmc\z/B= $find_pmc
ABresult
0X0
100
111
226 3200 ▶ push @res, $1 if /(.+)\.pod\z/ && $find_pod;
Branch: 1 branch
BranchTrueFalseTotal
if /(.+)\.pod\z/ and $find_pod 20 3180 3200
Condition100%3/3
/(.+)\.pod\z/ and $find_pod
!ll&&!rl&&r
3178220
MC/DC100%2/2
/(.+)\.pod\z/ and $find_pod
/(.+)\.pod\z/ $find_pod
Truth table100%3/3
A and B
A= /(.+)\.pod\z/B= $find_pod
ABresult
0X0
100
111
227 }
228 }
229 68 [sort(uniq(@res))];
230 },
231 path_sep => '::',
232 28 ▶ is_dir_func => sub { }, # not needed, we already suffix "dirs" with ::
Subroutine __ANON__: called CC=1 SCAR=0.0
233 recurse => $args{recurse},
234 recurse_matching => $args{recurse_matching},
235 exclude_leaf => $args{exclude_leaf},
236 exclude_dir => $args{exclude_dir},
237 26 );
238 26 push @$res, @$cp_res;
239 } # for $ns_prefix
240  
241 # dedup
242 {
243 22 ▶ last unless @ns_prefixes > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @ns_prefixes > 1 20 2 22
244 2 my $res_dedup = [];
245 2 my %seen;
246 2 ▶ for (@$res) { push @$res_dedup, $_ unless $seen{$_}++ }
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$_}++ 4 2 6
247 2 $res = $res_dedup;
248 }
249  
250 FILTER_WITH_SCHEMA: {
251 22 my $sch = $args{_schema};
252 22 ▶ last unless $sch;
Branch: 1 branch
BranchTrueFalseTotal
unless $sch 22 0 22
253  
254 0 my $fres = [];
255 0 for my $word (@$res) {
256 0 log_trace("[compmod] Validating word %s with validator ...", $word);
257 0 ▶ if ($sch->[1]{in} && !(grep { index($_, $word)==0 } @{ $sch->[1]{in} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sch->[1]{"in"} and not grep({index($_, $word) == 0;} @{$$sch[1]{"in"};})) 0 0 0
Condition0%0/3
$sch->[1]{"in"} and not grep({index($_, $word) == 0;} @{$$sch[1]{"in"};})
!ll&&!rl&&r
000
MC/DC0%0/2
$sch->[1]{"in"} and not grep({index($_, $word) == 0;} @{$$sch[1]{"in"};})
$sch->[1]{"in"} not grep({index($_, $word) == 0;} @{$$sch[1]{"in"};})
Truth table0%0/3
A and not(B)
A= $sch->[1]{"in"}B= not grep({index($_, $word) == 0;} @{$$sch[1]{"in"};})
ABresult
0X0
100
111
258 0 next;
259 }
260 0 push @$fres, $word;
261 }
262 0 $res = $fres;
263 }
264  
265 22 for (@$res) { s/::/$sep/g }
266  
267 22 $res = { words=>$res, path_sep=>$sep };
268 #log_trace('[compmod] Leaving complete_module(), result=<%s>', $res);
269 22 $res;
270 }
271  
272 3 1;
273 # ABSTRACT: Complete with installed Perl module names
274  
275 __END__
↑ Summary