Add LexEnv completion plugin. I still owe mst a refactor of the completion subsystem :)
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Completion.pm
1 package Devel::REPL::Plugin::Completion;
2 use Devel::REPL::Plugin;
3 use Scalar::Util 'weaken';
4 use PPI;
5 use namespace::clean -except => [ 'meta' ];
6
7 has current_matches => (
8   is => 'rw',
9   isa => 'ArrayRef',
10   lazy => 1,
11   default => sub { [] },
12 );
13
14 has match_index => (
15   is => 'rw',
16   isa => 'Int',
17   lazy => 1,
18   default => sub { 0 },
19 );
20
21 sub BEFORE_PLUGIN {
22   my ($self) = @_;
23
24   my $weakself = $self;
25   weaken($weakself);
26
27   $self->term->Attribs->{attempted_completion_function} = sub {
28     $weakself->_completion(@_);
29   };
30 }
31
32 sub _completion {
33   my ($self, $text, $line, $start, $end) = @_;
34
35   # we're discarding everything after the cursor for completion purposes
36   # we can't just use $text because we want all the code before the cursor to
37   # matter, not just the current word
38   substr($line, $end) = '';
39
40   my $document = PPI::Document->new(\$line);
41   return unless defined($document);
42
43   $document->prune('PPI::Token::Whitespace');
44
45   my @matches = $self->complete($text, $document);
46
47   # iterate through the completions
48   return $self->term->completion_matches($text, sub {
49     my ($text, $state) = @_;
50
51     if (!$state) {
52       $self->current_matches(\@matches);
53       $self->match_index(0);
54     }
55     else {
56       $self->match_index($self->match_index + 1);
57     }
58
59     return $self->current_matches->[$self->match_index];
60   });
61 }
62
63 sub complete {
64   return ();
65 }
66
67 1;
68