| 1 | package Bash::Completion::Plugins::Perldoc; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 1 | $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 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use parent 'Bash::Completion::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | use Bash::Completion::Utils | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | qw( command_in_path match_perl_modules prefix_match ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use File::Spec::Functions qw/ catfile rel2abs catdir splitpath no_upwards /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use List::MoreUtils qw/ apply uniq /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | sub should_activate { | ||||||||||||||||||||||||||
|
Subroutine
should_activate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 1 | my @commands = ('perldoc'); | |||||||||||||||||||||||||||
| 46 | 1 | return [grep { command_in_path($_) } @commands]; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub generate_bash_setup { return [qw( nospace default )] } | ||||||||||||||||||||||||||
|
Subroutine
generate_bash_setup: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub complete { | ||||||||||||||||||||||||||
|
Subroutine
complete: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ($class, $req) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my @args = $req->args; | |||||||||||||||||||||||||||
| 59 | 0 | pop @args; # last is the word | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $function = @args && $args[-1] eq '-f' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@args and $args[-1] eq "-f"
MC/DC0%0/2
@args and $args[-1] eq "-f"
@args
$args[-1] eq "-f"
Truth table0%0/3
A and B
A= @argsB= $args[-1] eq "-f"
| |||||||||||||||||||||||||||||
| 62 | ? \&get_function_suggestions | ||||||||||||||||||||||||||||
| 63 | : \&get_package_suggestions | ||||||||||||||||||||||||||||
| 64 | ; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | $req->candidates( $function->( $req->word ) ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub slurp_dir { | ||||||||||||||||||||||||||
|
Subroutine
slurp_dir: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | opendir my $dir, shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | no_upwards readdir $dir; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub suggestion_from_name { | ||||||||||||||||||||||||||
|
Subroutine
suggestion_from_name: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 0 | my ( $file_rx, $path, $name ) = @_; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return if not $name =~ /$file_rx/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return $name.'::', $name.':: ' if -d catdir $path, $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | return $1; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub suggestions_from_path { | ||||||||||||||||||||||||||
|
Subroutine
suggestions_from_path: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 0 | my ( $file_rx, $path ) = @_; | |||||||||||||||||||||||||||
| 83 | 0 | map { suggestion_from_name( $file_rx, $path, $_ ) } slurp_dir( $path ); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub get_package_suggestions { | ||||||||||||||||||||||||||
|
Subroutine
get_package_suggestions: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 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/; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my $home = rel2abs $ENV{'HOME'}; | |||||||||||||||||||||||||||
| 93 | 0 | my $cwd = rel2abs do { require Cwd; Cwd::cwd() }; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | my @suggestion = | ||||||||||||||||||||||||||||
| 96 | uniq | ||||||||||||||||||||||||||||
| 97 | 0 | map { ( my $x = $_ ) =~ s/::\s$/::/; $x } | |||||||||||||||||||||||||||
| 98 | 0 | map { suggestions_from_path $file_rx, $_ } | |||||||||||||||||||||||||||
| 99 | uniq | ||||||||||||||||||||||||||||
| 100 | 0 | map { catdir $_, @segment } | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | grep { $home ne $_ and $cwd ne $_ } | ||||||||||||||||||||||||||
|
Condition0%0/3
$home ne $_ && $cwd ne $_
MC/DC0%0/2
$home ne $_ && $cwd ne $_
$home ne $_
$cwd ne $_
Truth table0%0/3
A && B
A= $home ne $_B= $cwd ne $_
| |||||||||||||||||||||||||||||
| 102 | 0 | map { $_, ( catdir $_, 'pod' ) } | |||||||||||||||||||||||||||
| 103 | 0 | map { rel2abs $_ } | |||||||||||||||||||||||||||
| 104 | @INC; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # fixups | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( $pkg eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 0 | my $total = @suggestion; | |||||||||||||||||||||||||||
| 109 | 0 | @suggestion = grep { not /^perl/ } @suggestion; | |||||||||||||||||||||||||||
| 110 | 0 | my $num_hidden = $total - @suggestion; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | push @suggestion, "perl* ($num_hidden hidden)" if $num_hidden; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | elsif ( $pkg =~ /(?<!:):\z/ ) { | ||||||||||||||||||||||||||||
| 114 | 0 | @suggestion = map { ":$_" } @suggestion; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | return @suggestion; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub get_function_suggestions { | ||||||||||||||||||||||||||
|
Subroutine
get_function_suggestions: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 0 | my ( $func ) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my $perlfunc; | |||||||||||||||||||||||||||
| 124 | 0 | for ( @INC, undef ) { | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | return if not defined; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | $perlfunc = catfile( $_, qw( pod perlfunc.pod ) ); | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | last if -r $perlfunc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | open my $fh, '<', $perlfunc or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my @suggestion; | |||||||||||||||||||||||||||
| 133 | 0 | my $nest_level = -1; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | while ( <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | next if 1 .. /^=head2 Alphabetical Listing of Perl Functions$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | ++$nest_level if /^=over/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | --$nest_level if /^=back/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | next if $nest_level; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | push @suggestion, /^=item (-?\w+)/; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | my $func_rx = qr/\A${\quotemeta $func}/; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | return grep { /$func_rx/ } @suggestion; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | 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<perldoc> 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<Bash::Completion::Plugins::Perldoc> plugin if we can | ||||||||||||||||||||||||||||
| 177 | find the C<perldoc> command. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 generate_bash_setup | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Make sure we use bash C<complete> options C<nospace> and C<default>. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 complete | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Completion logic for C<perldoc>. 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 <melo@cpan.org> | ||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||