Return a reference to the correct lexical variable (not the copy)
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / Globals.pm
CommitLineData
908733a9 1package Devel::REPL::Plugin::CompletionDriver::Globals;
2use Devel::REPL::Plugin;
3use namespace::clean -except => [ 'meta' ];
4
5around complete => sub {
6 my $orig = shift;
7 my ($self, $text, $document) = @_;
8
9 my $last = $self->last_ppi_element($document);
10
11 return $orig->(@_)
12 unless $last->isa('PPI::Token::Symbol');
13
14 my $sigil = substr($last, 0, 1, '');
15 my $re = qr/^\Q$last/;
16
17 my @package_fragments = split qr/::|'/, $last;
18
19 # split drops the last fragment if it's empty
20 push @package_fragments, '' if $last =~ /(?:'|::)$/;
21
22 # the beginning of the variable, or an incomplete package name
23 my $incomplete = pop @package_fragments;
24
25 # recurse for the complete package fragments
26 my $stash = \%::;
27 for (@package_fragments) {
28 $stash = $stash->{"$_\::"};
29 }
30
31 # collect any variables from this stash
32 my @found = grep { /$re/ }
33 map { join '::', @package_fragments, $_ }
34 keys %$stash;
35
36 # check to see if it's an incomplete package name, and add its variables
37 # so Devel<TAB> is completed correctly
38 for my $key (keys %$stash) {
39 next unless $key =~ /::$/; # only look at deeper packages
40 next unless $key =~ /^\Q$incomplete/; # only look at matching packages
41 push @found,
42 map { join '::', @package_fragments, $_ }
43 map { "$key$_" } # $key already has trailing ::
44 keys %{ $stash->{$key} };
45 }
46
47 return $orig->(@_), @found;
48};
49
501;
51