1fc094a89f6187a48d36c3c9414162a3b98074d0
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / Keywords.pm
1 use strict;
2 use warnings;
3 package Devel::REPL::Plugin::CompletionDriver::Keywords;
4 use Devel::REPL::Plugin;
5 use Devel::REPL::Plugin::Completion;    # die early if cannot load
6 use B::Keywords qw/@Functions @Barewords/;
7 use namespace::autoclean;
8
9 sub BEFORE_PLUGIN {
10     my $self = shift;
11     $self->load_plugin('Completion');
12 }
13
14 around complete => sub {
15   my $orig = shift;
16   my ($self, $text, $document) = @_;
17
18   my $last = $self->last_ppi_element($document);
19
20   return $orig->(@_)
21     unless $last->isa('PPI::Token::Word');
22
23   # don't complete keywords on foo->method
24   return $orig->(@_)
25     if $last->sprevious_sibling
26     && $last->sprevious_sibling->isa('PPI::Token::Operator')
27     && $last->sprevious_sibling->content eq '->';
28
29   my $re = qr/^\Q$last/;
30
31   return $orig->(@_),
32          grep { $_ =~ $re } @Functions, @Barewords;
33 };
34
35 1;
36
37 __END__
38
39 =head1 NAME
40
41 Devel::REPL::Plugin::CompletionDriver::Keywords - Complete Perl keywords and operators
42
43 =head1 AUTHOR
44
45 Shawn M Moore, C<< <sartak at gmail dot com> >>
46
47 =cut
48