Add a bit more description of Turtle plugin.
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Completion.pm
CommitLineData
e4ac8502 1package Devel::REPL::Plugin::Completion;
1989c3d2 2use Devel::REPL::Plugin;
3use Scalar::Util 'weaken';
4use PPI;
e4ac8502 5use namespace::clean -except => [ 'meta' ];
6
1989c3d2 7has current_matches => (
314f2293 8 is => 'rw',
9 isa => 'ArrayRef',
10 lazy => 1,
11 default => sub { [] },
1989c3d2 12);
ac71b56c 13
1989c3d2 14has match_index => (
314f2293 15 is => 'rw',
16 isa => 'Int',
17 lazy => 1,
18 default => sub { 0 },
1989c3d2 19);
e4ac8502 20
97d28d6b 21has no_term_class_warning => (
22 isa => "Bool",
23 is => "rw",
24 default => 0,
25);
26
839614c7 27before 'read' => sub {
1989c3d2 28 my ($self) = @_;
e4ac8502 29
f2833460 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);
839614c7 34 }
35
1989c3d2 36 my $weakself = $self;
37 weaken($weakself);
ac71b56c 38
1989c3d2 39 $self->term->Attribs->{attempted_completion_function} = sub {
40 $weakself->_completion(@_);
41 };
f2833460 42
43 $self->term->Attribs->{completion_function} = sub {
44 $weakself->_completion(@_);
45 };
839614c7 46};
97d28d6b 47
1989c3d2 48sub _completion {
f2833460 49 my $is_trp = scalar(@_) == 4 ? 1 : 0;
1989c3d2 50 my ($self, $text, $line, $start, $end) = @_;
f2833460 51 $end = $start+length($text) if $is_trp;
1989c3d2 52
53 # we're discarding everything after the cursor for completion purposes
314f2293 54 # we can't just use $text because we want all the code before the cursor to
55 # matter, not just the current word
1989c3d2 56 substr($line, $end) = '';
57
58 my $document = PPI::Document->new(\$line);
59 return unless defined($document);
60
314f2293 61 $document->prune('PPI::Token::Whitespace');
62
1989c3d2 63 my @matches = $self->complete($text, $document);
ac71b56c 64
1989c3d2 65 # iterate through the completions
f2833460 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 }
1989c3d2 83}
84
85sub complete {
86 return ();
87}
e4ac8502 88
8051a5e0 89# recursively find the last element
90sub 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
e4ac8502 1001;
1989c3d2 101
cfd1094b 102__END__
103
104=head1 NAME
105
106Devel::REPL::Plugin::Completion - Extensible tab completion
107
30b459d4 108=head1 AUTHOR
109
110Shawn M Moore, C<< <sartak at gmail dot com> >>
111
cfd1094b 112=cut
113