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