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