3 package Devel::REPL::Plugin::Completion;
5 our $VERSION = '1.003027';
7 use Devel::REPL::Plugin;
8 use Scalar::Util 'weaken';
10 use namespace::autoclean;
12 has current_matches => (
16 default => sub { [] },
26 has no_term_class_warning => (
32 has do_readline_filename_completion => ( # so default is no if Completion loaded
39 before 'read' => sub {
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);
51 if ($self->term->isa("Term::ReadLine::Gnu")) {
52 $self->term->Attribs->{attempted_completion_function} = sub {
53 $weakself->_completion(@_);
57 if ($self->term->isa("Term::ReadLine::Perl")) {
58 $self->term->Attribs->{completion_function} = sub {
59 $weakself->_completion(@_);
66 my $is_trp = scalar(@_) == 4 ? 1 : 0;
67 my ($self, $text, $line, $start, $end) = @_;
68 $end = $start+length($text) if $is_trp;
70 # we're discarding everything after the cursor for completion purposes
71 # we can't just use $text because we want all the code before the cursor to
72 # matter, not just the current word
73 substr($line, $end) = '';
75 my $document = PPI::Document->new(\$line);
76 return unless defined($document);
78 $document->prune('PPI::Token::Whitespace');
80 my @matches = $self->complete($text, $document);
82 # iterate through the completions
84 if (scalar(@matches)) {
87 return ($self->do_readline_filename_completion) ? readline::rl_filename_list($text) : () ;
90 $self->term->Attribs->{attempted_completion_over} = 1 unless $self->do_readline_filename_completion;
91 if (scalar(@matches)) {
92 return $self->term->completion_matches($text, sub {
93 my ($text, $state) = @_;
96 $self->current_matches(\@matches);
97 $self->match_index(0);
100 $self->match_index($self->match_index + 1);
103 return $self->current_matches->[$self->match_index];
115 # recursively find the last element
116 sub last_ppi_element {
117 my ($self, $document, $type) = @_;
118 my $last = $document;
119 while ($last->can('last_element') && defined($last->last_element)) {
120 $last = $last->last_element;
121 return $last if $type && $last->isa($type);
132 Devel::REPL::Plugin::Completion - Extensible tab completion
136 By default, the Completion plugin explicitly does I<not> use the Gnu readline
137 or Term::ReadLine::Perl fallback filename completion.
139 Set the attribute C<do_readline_filename_completion> to 1 to enable this feature.
143 Shawn M Moore, C<< <sartak at gmail dot com> >>