Add a bit more description of Turtle plugin.
[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 has no_term_class_warning => (
22   isa => "Bool",
23   is  => "rw",
24   default => 0,
25 );
26
27 before 'read' => sub {
28   my ($self) = @_;
29
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);
34   }
35
36   my $weakself = $self;
37   weaken($weakself);
38
39   $self->term->Attribs->{attempted_completion_function} = sub {
40     $weakself->_completion(@_);
41   };
42
43   $self->term->Attribs->{completion_function} = sub {
44     $weakself->_completion(@_);
45   };
46 };
47
48 sub _completion {
49   my $is_trp = scalar(@_) == 4 ? 1 : 0;
50   my ($self, $text, $line, $start, $end) = @_;
51   $end = $start+length($text) if $is_trp;
52
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) = '';
57
58   my $document = PPI::Document->new(\$line);
59   return unless defined($document);
60
61   $document->prune('PPI::Token::Whitespace');
62
63   my @matches = $self->complete($text, $document);
64
65   # iterate through the completions
66   if ($is_trp) {
67      return @matches;
68   } else {
69      return $self->term->completion_matches($text, sub {
70            my ($text, $state) = @_;
71
72            if (!$state) {
73               $self->current_matches(\@matches);
74               $self->match_index(0);
75            }
76            else {
77               $self->match_index($self->match_index + 1);
78            }
79
80            return $self->current_matches->[$self->match_index];
81         });
82   }
83 }
84
85 sub complete {
86   return ();
87 }
88
89 # recursively find the last element
90 sub last_ppi_element {
91   my ($self, $document, $type) = @_;
92   my $last = $document;
93   while ($last->can('last_element') && defined($last->last_element)) {
94     $last = $last->last_element;
95     return $last if $type && $last->isa($type);
96   }
97   return $last;
98 }
99
100 1;
101
102 __END__
103
104 =head1 NAME
105
106 Devel::REPL::Plugin::Completion - Extensible tab completion
107
108 =head1 AUTHOR
109
110 Shawn M Moore, C<< <sartak at gmail dot com> >>
111
112 =cut
113