r61091@onn: sartak | 2008-05-26 21:32:50 -0400
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / LexEnv.pm
CommitLineData
314f2293 1package Devel::REPL::Plugin::CompletionDriver::LexEnv;
2use Devel::REPL::Plugin;
3use namespace::clean -except => [ 'meta' ];
4
7b380d09 5sub AFTER_PLUGIN {
6 my ($_REPL) = @_;
7
8 if (!$_REPL->can('lexical_environment')) {
9 warn "Devel::REPL::Plugin::CompletionDriver::LexEnv requires Devel::REPL::Plugin::LexEnv.";
10 }
11}
314f2293 12
13around complete => sub {
14 my $orig = shift;
15 my ($self, $text, $document) = @_;
16
8051a5e0 17 my $last = $self->last_ppi_element($document);
314f2293 18
19 return $orig->(@_)
20 unless $last->isa('PPI::Token::Symbol');
21
22 my $sigil = substr($last, 0, 1, '');
23 my $re = qr/^\Q$last/;
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 keys %{$self->lexical_environment->get_context('_')};
31};
32
331;
34
cfd1094b 35__END__
36
37=head1 NAME
38
39Devel::REPL::Plugin::CompletionDriver::LexEnv - Complete variable names in the REPL's lexical environment
40
41=cut
42