make room for $VERSION after package declaration (newer [PkgVersion] requires it)
[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 use Devel::REPL::Plugin;
6 use Devel::REPL::Plugin::Completion;    # die early if cannot load
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::Symbol');
22
23   my ($sigil, $name) = split(//, $last, 2);
24   my $re = qr/^\Q$name/;
25
26   return $orig->(@_),
27          # ReadLine is weirdly inconsistent
28          map  { $sigil eq '%' ? '%' . $_ : $_ }
29          grep { /$re/ }
30          map  { substr($_, 1) } # drop lexical's sigil
31          '$_REPL', keys %{$self->lexical_environment->get_context('_')};
32 };
33
34 1;
35
36 __END__
37
38 =head1 NAME
39
40 Devel::REPL::Plugin::CompletionDriver::LexEnv - Complete variable names in the REPL's lexical environment
41
42 =head1 AUTHOR
43
44 Shawn M Moore, C<< <sartak at gmail dot com> >>
45
46 =cut
47