r61087@onn: sartak | 2008-05-26 21:31:37 -0400
[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->(@_)
310ddf9f 12 unless $last->isa('PPI::Token::Symbol')
13 || $last->isa('PPI::Token::Word');
908733a9 14
310ddf9f 15 my $sigil = $last =~ s/^[\$\@\%\&\*]// ? $1 : undef;
908733a9 16 my $re = qr/^\Q$last/;
17
18 my @package_fragments = split qr/::|'/, $last;
19
20 # split drops the last fragment if it's empty
21 push @package_fragments, '' if $last =~ /(?:'|::)$/;
22
23 # the beginning of the variable, or an incomplete package name
24 my $incomplete = pop @package_fragments;
25
26 # recurse for the complete package fragments
27 my $stash = \%::;
28 for (@package_fragments) {
29 $stash = $stash->{"$_\::"};
30 }
31
32 # collect any variables from this stash
33 my @found = grep { /$re/ }
34 map { join '::', @package_fragments, $_ }
35 keys %$stash;
36
37 # check to see if it's an incomplete package name, and add its variables
38 # so Devel<TAB> is completed correctly
39 for my $key (keys %$stash) {
40 next unless $key =~ /::$/; # only look at deeper packages
41 next unless $key =~ /^\Q$incomplete/; # only look at matching packages
42 push @found,
43 map { join '::', @package_fragments, $_ }
44 map { "$key$_" } # $key already has trailing ::
45 keys %{ $stash->{$key} };
46 }
47
48 return $orig->(@_), @found;
49};
50
511;
52