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