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->no_term_class_warning) {
31 warn "Term::ReadLine::Gnu is required for the Completion plugin to work";
32 $self->no_term_class_warning(1);
38 $self->term->Attribs->{attempted_completion_function} = sub {
39 $weakself->_completion(@_);
44 my ($self, $text, $line, $start, $end) = @_;
46 # we're discarding everything after the cursor for completion purposes
47 # we can't just use $text because we want all the code before the cursor to
48 # matter, not just the current word
49 substr($line, $end) = '';
51 my $document = PPI::Document->new(\$line);
52 return unless defined($document);
54 $document->prune('PPI::Token::Whitespace');
56 my @matches = $self->complete($text, $document);
58 # iterate through the completions
59 return $self->term->completion_matches($text, sub {
60 my ($text, $state) = @_;
63 $self->current_matches(\@matches);
64 $self->match_index(0);
67 $self->match_index($self->match_index + 1);
70 return $self->current_matches->[$self->match_index];
78 # recursively find the last element
79 sub last_ppi_element {
80 my ($self, $document, $type) = @_;
82 while ($last->can('last_element') && defined($last->last_element)) {
83 $last = $last->last_element;
84 return $last if $type && $last->isa($type);
95 Devel::REPL::Plugin::Completion - Extensible tab completion
99 Shawn M Moore, C<< <sartak at gmail dot com> >>