File Coverage

blib/lib/Bash/Completion/Plugins/Perldoc.pm
Criterion Covered Total %
statement 22 80 27.5
branch 0 30 0.0
condition 0 3 0.0
subroutine 8 14 57.1
pod 3 8 37.5
total 33 135 24.4


line stmt bran cond sub pod time code
1             package Bash::Completion::Plugins::Perldoc;
2             {
3             $Bash::Completion::Plugins::Perldoc::VERSION = '0.008';
4             }
5              
6             # ABSTRACT: complete perldoc command
7              
8             # for the part of the code that is heavily
9             # inspired by Aristotle's code:
10             #
11             # Copyright (c) 2010 Aristotle Pagaltzis {{{
12             #
13             # Permission is hereby granted, free of charge, to any person obtaining
14             # a copy of this software and associated documentation files (the
15             # "Software"), to deal in the Software without restriction, including
16             # without limitation the rights to use, copy, modify, merge, publish,
17             # distribute, sublicense, and/or sell copies of the Software, and to
18             # permit persons to whom the Software is furnished to do so, subject to
19             # the following conditions:
20             #
21             # The above copyright notice and this permission notice shall be
22             # included in all copies or substantial portions of the Software.
23             #
24             # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25             # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26             # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27             # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28             # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29             # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30             # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
31             # }}}
32              
33 1     1   766 use strict;
  1         3  
  1         33  
34 1     1   5 use warnings;
  1         2  
  1         34  
35              
36 1     1   6 use parent 'Bash::Completion::Plugin';
  1         2  
  1         8  
37              
38             use Bash::Completion::Utils
39 1     1   58 qw( command_in_path match_perl_modules prefix_match );
  1         2  
  1         65  
40 1     1   6 use File::Spec::Functions qw/ catfile rel2abs catdir splitpath no_upwards /;
  1         1  
  1         73  
41 1     1   790 use List::MoreUtils qw/ apply uniq /;
  1         16256  
  1         1491  
42              
43              
44             sub should_activate {
45 1     1 1 3 my @commands = ('perldoc');
46 1         1 return [grep { command_in_path($_) } @commands];
  1         5  
47             }
48              
49              
50              
51 1     1 1 4 sub generate_bash_setup { return [qw( nospace default )] }
52              
53              
54              
55             sub complete {
56 0     0 1   my ($class, $req) = @_;
57              
58 0           my @args = $req->args;
59 0           pop @args; # last is the word
60              
61 0 0 0       my $function = @args && $args[-1] eq '-f'
62             ? \&get_function_suggestions
63             : \&get_package_suggestions
64             ;
65              
66 0           $req->candidates( $function->( $req->word ) );
67             }
68              
69             sub slurp_dir {
70 0 0   0 0   opendir my $dir, shift or return;
71 0           no_upwards readdir $dir;
72             }
73              
74             sub suggestion_from_name {
75 0     0 0   my ( $file_rx, $path, $name ) = @_;
76 0 0         return if not $name =~ /$file_rx/;
77 0 0         return $name.'::', $name.':: ' if -d catdir $path, $name;
78 0           return $1;
79             }
80              
81             sub suggestions_from_path {
82 0     0 0   my ( $file_rx, $path ) = @_;
83 0           map { suggestion_from_name( $file_rx, $path, $_ ) } slurp_dir( $path );
  0            
84             }
85              
86             sub get_package_suggestions {
87 0     0 0   my ( $pkg ) = @_;
88              
89 0           my @segment = split /::|:\z/, $pkg, -1;
90 0           my $file_rx = qr/\A(${\quotemeta pop @segment}\w*)(?:\.pm|\.pod)?\z/;
  0            
91              
92 0           my $home = rel2abs $ENV{'HOME'};
93 0           my $cwd = rel2abs do { require Cwd; Cwd::cwd() };
  0            
  0            
94              
95 0           my @suggestion =
96             uniq
97 0           map { ( my $x = $_ ) =~ s/::\s$/::/; $x }
  0            
98 0           map { suggestions_from_path $file_rx, $_ }
99             uniq
100 0 0         map { catdir $_, @segment }
101 0           grep { $home ne $_ and $cwd ne $_ }
102 0           map { $_, ( catdir $_, 'pod' ) }
103 0           map { rel2abs $_ }
104             @INC;
105              
106             # fixups
107 0 0         if ( $pkg eq '' ) {
    0          
108 0           my $total = @suggestion;
109 0           @suggestion = grep { not /^perl/ } @suggestion;
  0            
110 0           my $num_hidden = $total - @suggestion;
111 0 0         push @suggestion, "perl* ($num_hidden hidden)" if $num_hidden;
112             }
113             elsif ( $pkg =~ /(?
114 0           @suggestion = map { ":$_" } @suggestion;
  0            
115             }
116              
117 0           return @suggestion;
118             }
119              
120             sub get_function_suggestions {
121 0     0 0   my ( $func ) = @_;
122              
123 0           my $perlfunc;
124 0           for ( @INC, undef ) {
125 0 0         return if not defined;
126 0           $perlfunc = catfile( $_, qw( pod perlfunc.pod ) );
127 0 0         last if -r $perlfunc;
128             }
129              
130 0 0         open my $fh, '<', $perlfunc or return;
131              
132 0           my @suggestion;
133 0           my $nest_level = -1;
134 0           while ( <$fh> ) {
135 0 0         next if 1 .. /^=head2 Alphabetical Listing of Perl Functions$/;
136 0 0         ++$nest_level if /^=over/;
137 0 0         --$nest_level if /^=back/;
138 0 0         next if $nest_level;
139 0           push @suggestion, /^=item (-?\w+)/;
140             }
141              
142 0           my $func_rx = qr/\A${\quotemeta $func}/;
  0            
143              
144 0           return grep { /$func_rx/ } @suggestion;
  0            
145             }
146              
147             1;
148              
149              
150              
151             =pod
152              
153             =head1 NAME
154              
155             Bash::Completion::Plugins::Perldoc - complete perldoc command
156              
157             =head1 VERSION
158              
159             version 0.008
160              
161             =head1 SYNOPSIS
162              
163             ## not to be used directly
164              
165             =head1 DESCRIPTION
166              
167             A plugin for the C command. Completes module names, and
168             function names if the V<-f> parameter is used.
169              
170             Heavily based on Aristotle's perldoc-complete
171              
172             =head1 METHODS
173              
174             =head2 should_activate
175              
176             Activate this C plugin if we can
177             find the C command.
178              
179             =head2 generate_bash_setup
180              
181             Make sure we use bash C options C and C.
182              
183             =head2 complete
184              
185             Completion logic for C. Completes Perl modules only for now.
186              
187             =head1 SEE ALSO
188              
189             =over
190              
191             =item Aristotle's perldoc-complete - https://github.com/ap/perldoc-complete
192              
193             =back
194              
195             =head1 AUTHOR
196              
197             Pedro Melo
198              
199             =head1 COPYRIGHT AND LICENSE
200              
201             This software is Copyright (c) 2011 by Pedro Melo.
202              
203             This is free software, licensed under:
204              
205             The Artistic License 2.0 (GPL Compatible)
206              
207             =cut
208              
209              
210             __END__