1 package Devel::REPL::Plugin::CompletionDriver::Globals;
2 use Devel::REPL::Plugin;
3 use namespace::clean -except => [ 'meta' ];
7 $self->load_plugin('Completion');
10 around complete => sub {
12 my ($self, $text, $document) = @_;
14 my $last = $self->last_ppi_element($document);
17 unless $last->isa('PPI::Token::Symbol')
18 || $last->isa('PPI::Token::Word');
20 my $sigil = $last =~ s/^[\$\@\%\&\*]// ? $1 : undef;
21 my $re = qr/^\Q$last/;
23 my @package_fragments = split qr/::|'/, $last;
25 # split drops the last fragment if it's empty
26 push @package_fragments, '' if $last =~ /(?:'|::)$/;
28 # the beginning of the variable, or an incomplete package name
29 my $incomplete = pop @package_fragments;
31 # recurse for the complete package fragments
33 for (@package_fragments) {
34 $stash = $stash->{"$_\::"};
37 # collect any variables from this stash
38 my @found = grep { /$re/ }
39 map { join '::', @package_fragments, $_ }
42 # check to see if it's an incomplete package name, and add its variables
43 # so Devel<TAB> is completed correctly
44 for my $key (keys %$stash) {
45 next unless $key =~ /::$/; # only look at deeper packages
46 next unless $key =~ /^\Q$incomplete/; # only look at matching packages
48 map { join '::', @package_fragments, $_ }
49 map { "$key$_" } # $key already has trailing ::
50 keys %{ $stash->{$key} };
53 return $orig->(@_), @found;
62 Devel::REPL::Plugin::CompletionDriver::Globals - Complete global variables, packages, namespaced functions
66 Shawn M Moore, C<< <sartak at gmail dot com> >>