1 package Devel::REPL::Plugin::CompletionDriver::Globals;
2 use Devel::REPL::Plugin;
3 use namespace::clean -except => [ 'meta' ];
5 around complete => sub {
7 my ($self, $text, $document) = @_;
9 my $last = $self->last_ppi_element($document);
12 unless $last->isa('PPI::Token::Symbol');
14 my $sigil = substr($last, 0, 1, '');
15 my $re = qr/^\Q$last/;
17 my @package_fragments = split qr/::|'/, $last;
19 # split drops the last fragment if it's empty
20 push @package_fragments, '' if $last =~ /(?:'|::)$/;
22 # the beginning of the variable, or an incomplete package name
23 my $incomplete = pop @package_fragments;
25 # recurse for the complete package fragments
27 for (@package_fragments) {
28 $stash = $stash->{"$_\::"};
31 # collect any variables from this stash
32 my @found = grep { /$re/ }
33 map { join '::', @package_fragments, $_ }
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
42 map { join '::', @package_fragments, $_ }
43 map { "$key$_" } # $key already has trailing ::
44 keys %{ $stash->{$key} };
47 return $orig->(@_), @found;