3 package Devel::REPL::Plugin::CompletionDriver::Globals;
4 use Devel::REPL::Plugin;
5 use namespace::autoclean;
9 $self->load_plugin('Completion');
12 around complete => sub {
14 my ($self, $text, $document) = @_;
16 my $last = $self->last_ppi_element($document);
19 unless $last->isa('PPI::Token::Symbol')
20 || $last->isa('PPI::Token::Word');
22 my $sigil = $last =~ s/^[\$\@\%\&\*]// ? $1 : undef;
23 my $re = qr/^\Q$last/;
25 my @package_fragments = split qr/::|'/, $last;
27 # split drops the last fragment if it's empty
28 push @package_fragments, '' if $last =~ /(?:'|::)$/;
30 # the beginning of the variable, or an incomplete package name
31 my $incomplete = pop @package_fragments;
33 # recurse for the complete package fragments
35 for (@package_fragments) {
36 $stash = $stash->{"$_\::"};
39 # collect any variables from this stash
40 my @found = grep { /$re/ }
41 map { join '::', @package_fragments, $_ }
44 # check to see if it's an incomplete package name, and add its variables
45 # so Devel<TAB> is completed correctly
46 for my $key (keys %$stash) {
47 next unless $key =~ /::$/; # only look at deeper packages
48 next unless $key =~ /^\Q$incomplete/; # only look at matching packages
50 map { join '::', @package_fragments, $_ }
51 map { "$key$_" } # $key already has trailing ::
52 keys %{ $stash->{$key} };
55 return $orig->(@_), @found;
64 Devel::REPL::Plugin::CompletionDriver::Globals - Complete global variables, packages, namespaced functions
68 Shawn M Moore, C<< <sartak at gmail dot com> >>