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 => (
33 $self->term->Attribs->{attempted_completion_function} = sub {
34 $weakself->_completion(@_);
41 warn "Term::ReadLine::Gnu is required for the Completion plugin to work"
42 unless $self->term->isa("Term::ReadLine::Gnu") and !$self->no_term_class_warning;
46 my ($self, $text, $line, $start, $end) = @_;
48 # we're discarding everything after the cursor for completion purposes
49 # we can't just use $text because we want all the code before the cursor to
50 # matter, not just the current word
51 substr($line, $end) = '';
53 my $document = PPI::Document->new(\$line);
54 return unless defined($document);
56 $document->prune('PPI::Token::Whitespace');
58 my @matches = $self->complete($text, $document);
60 # iterate through the completions
61 return $self->term->completion_matches($text, sub {
62 my ($text, $state) = @_;
65 $self->current_matches(\@matches);
66 $self->match_index(0);
69 $self->match_index($self->match_index + 1);
72 return $self->current_matches->[$self->match_index];
80 # recursively find the last element
81 sub last_ppi_element {
82 my ($self, $document, $type) = @_;
84 while ($last->can('last_element') && defined($last->last_element)) {
85 $last = $last->last_element;
86 return $last if $type && $last->isa($type);