File Coverage

blib/lib/Devel/REPL/Plugin/Completion.pm
Criterion Covered Total %
statement 18 47 38.3
branch 0 20 0.0
condition 0 6 0.0
subroutine 6 10 60.0
pod 0 2 0.0
total 24 85 28.2


line stmt bran cond sub pod time code
1 2     2   93997 use strict;
  2         4  
  2         61  
2 2     2   10 use warnings;
  2         5  
  2         119  
3             # ABSTRACT: Extensible tab completion
4              
5             our $VERSION = '1.003029';
6              
7             use Devel::REPL::Plugin;
8 2     2   11 use Scalar::Util 'weaken';
  2         5  
  2         17  
9 2     2   9225 use PPI;
  2         6  
  2         161  
10 2     2   697 use namespace::autoclean;
  2         106890  
  2         65  
11 2     2   15  
  2         5  
  2         19  
12             has current_matches => (
13             is => 'rw',
14             isa => 'ArrayRef',
15             lazy => 1,
16             default => sub { [] },
17             );
18              
19             has match_index => (
20             is => 'rw',
21             isa => 'Int',
22             lazy => 1,
23             default => sub { 0 },
24             );
25              
26             has no_term_class_warning => (
27             isa => "Bool",
28             is => "rw",
29             default => 0,
30             );
31              
32             has do_readline_filename_completion => ( # so default is no if Completion loaded
33             isa => "Bool",
34             is => "rw",
35             lazy => 1,
36             default => sub { 0 },
37             );
38              
39             before 'read' => sub {
40             my ($self) = @_;
41              
42             if ((!$self->term->isa("Term::ReadLine::Gnu") and !$self->term->isa("Term::ReadLine::Perl"))
43             and !$self->no_term_class_warning) {
44             warn "Term::ReadLine::Gnu or Term::ReadLine::Perl is required for the Completion plugin to work";
45             $self->no_term_class_warning(1);
46             }
47              
48             my $weakself = $self;
49             weaken($weakself);
50              
51             if ($self->term->isa("Term::ReadLine::Gnu")) {
52             $self->term->Attribs->{attempted_completion_function} = sub {
53             $weakself->_completion(@_);
54             };
55             }
56              
57             if ($self->term->isa("Term::ReadLine::Perl")) {
58             $self->term->Attribs->{completion_function} = sub {
59             $weakself->_completion(@_);
60             };
61             }
62              
63             };
64              
65             my $is_trp = scalar(@_) == 4 ? 1 : 0;
66             my ($self, $text, $line, $start, $end) = @_;
67 0 0   0     $end = $start+length($text) if $is_trp;
68 0            
69 0 0         # we're discarding everything after the cursor for completion purposes
70             # we can't just use $text because we want all the code before the cursor to
71             # matter, not just the current word
72             substr($line, $end) = '';
73              
74 0           my $document = PPI::Document->new(\$line);
75             return unless defined($document);
76 0            
77 0 0         $document->prune('PPI::Token::Whitespace');
78              
79 0           my @matches = $self->complete($text, $document);
80              
81 0           # iterate through the completions
82             if ($is_trp) {
83             if (scalar(@matches)) {
84 0 0         return @matches;
85 0 0         } else {
86 0           return ($self->do_readline_filename_completion) ? readline::rl_filename_list($text) : () ;
87             }
88 0 0         } else {
89             $self->term->Attribs->{attempted_completion_over} = 1 unless $self->do_readline_filename_completion;
90             if (scalar(@matches)) {
91 0 0         return $self->term->completion_matches($text, sub {
92 0 0         my ($text, $state) = @_;
93              
94 0     0     if (!$state) {
95             $self->current_matches(\@matches);
96 0 0         $self->match_index(0);
97 0           }
98 0           else {
99             $self->match_index($self->match_index + 1);
100             }
101 0            
102             return $self->current_matches->[$self->match_index];
103             });
104 0           } else {
105 0           return;
106             }
107 0           }
108             }
109              
110             return ();
111             }
112              
113 0     0 0   # recursively find the last element
114             my ($self, $document, $type) = @_;
115             my $last = $document;
116             while ($last->can('last_element') && defined($last->last_element)) {
117             $last = $last->last_element;
118 0     0 0   return $last if $type && $last->isa($type);
119 0           }
120 0   0       return $last;
121 0           }
122 0 0 0        
123             1;
124 0            
125              
126             =pod
127              
128             =encoding UTF-8
129              
130             =head1 NAME
131              
132             Devel::REPL::Plugin::Completion - Extensible tab completion
133              
134             =head1 VERSION
135              
136             version 1.003029
137              
138             =head1 NOTE
139              
140             By default, the Completion plugin explicitly does I<not> use the Gnu readline
141             or Term::ReadLine::Perl fallback filename completion.
142              
143             Set the attribute C<do_readline_filename_completion> to 1 to enable this feature.
144              
145             =head1 SUPPORT
146              
147             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
148             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
149              
150             There is also an irc channel available for users of this distribution, at
151             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
152              
153             =head1 AUTHOR
154              
155             Shawn M Moore, C<< <sartak at gmail dot com> >>
156              
157             =head1 COPYRIGHT AND LICENCE
158              
159             This software is copyright (c) 2007 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
160              
161             This is free software; you can redistribute it and/or modify it under
162             the same terms as the Perl 5 programming language system itself.
163              
164             =cut