1 package Devel::REPL::Plugin::Completion;
2 use Devel::REPL::Plugin;
3 use Scalar::Util 'weaken';
5 use namespace::clean -except => [ 'meta' ];
7 has current_matches => (
11 default => sub { [] },
21 has no_term_class_warning => (
27 before 'read' => sub {
30 if ((!$self->term->isa("Term::ReadLine::Gnu") and !$self->term->isa("Term::ReadLine::Perl"))
31 and !$self->no_term_class_warning) {
32 warn "Term::ReadLine::Gnu or Term::ReadLine::Perl is required for the Completion plugin to work";
33 $self->no_term_class_warning(1);
39 $self->term->Attribs->{attempted_completion_function} = sub {
40 $weakself->_completion(@_);
43 $self->term->Attribs->{completion_function} = sub {
44 $weakself->_completion(@_);
49 my $is_trp = scalar(@_) == 4 ? 1 : 0;
50 my ($self, $text, $line, $start, $end) = @_;
51 $end = $start+length($text) if $is_trp;
53 # we're discarding everything after the cursor for completion purposes
54 # we can't just use $text because we want all the code before the cursor to
55 # matter, not just the current word
56 substr($line, $end) = '';
58 my $document = PPI::Document->new(\$line);
59 return unless defined($document);
61 $document->prune('PPI::Token::Whitespace');
63 my @matches = $self->complete($text, $document);
65 # iterate through the completions
69 return $self->term->completion_matches($text, sub {
70 my ($text, $state) = @_;
73 $self->current_matches(\@matches);
74 $self->match_index(0);
77 $self->match_index($self->match_index + 1);
80 return $self->current_matches->[$self->match_index];
89 # recursively find the last element
90 sub last_ppi_element {
91 my ($self, $document, $type) = @_;
93 while ($last->can('last_element') && defined($last->last_element)) {
94 $last = $last->last_element;
95 return $last if $type && $last->isa($type);
106 Devel::REPL::Plugin::Completion - Extensible tab completion
110 Shawn M Moore, C<< <sartak at gmail dot com> >>