dd833bcb309bc1336c983069727bd5534851dd10
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / LexEnv.pm
1 use strict;
2 use warnings;
3 package Devel::REPL::Plugin::CompletionDriver::LexEnv;
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 namespace::autoclean;
10
11 sub BEFORE_PLUGIN {
12     my $self = shift;
13     $self->load_plugin('Completion');
14 }
15
16 around complete => sub {
17   my $orig = shift;
18   my ($self, $text, $document) = @_;
19
20   my $last = $self->last_ppi_element($document);
21
22   return $orig->(@_)
23     unless $last->isa('PPI::Token::Symbol');
24
25   my ($sigil, $name) = split(//, $last, 2);
26   my $re = qr/^\Q$name/;
27
28   return $orig->(@_),
29          # ReadLine is weirdly inconsistent
30          map  { $sigil eq '%' ? '%' . $_ : $_ }
31          grep { /$re/ }
32          map  { substr($_, 1) } # drop lexical's sigil
33          '$_REPL', keys %{$self->lexical_environment->get_context('_')};
34 };
35
36 1;
37
38 __END__
39
40 =head1 NAME
41
42 Devel::REPL::Plugin::CompletionDriver::LexEnv - Complete variable names in the REPL's lexical environment
43
44 =head1 AUTHOR
45
46 Shawn M Moore, C<< <sartak at gmail dot com> >>
47
48 =cut
49