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