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')
13 || $last->isa('PPI::Token::Word');
15 my $sigil = $last =~ s/^[\$\@\%\&\*]// ? $1 : undef;
16 my $re = qr/^\Q$last/;
18 my @package_fragments = split qr/::|'/, $last;
20 # split drops the last fragment if it's empty
21 push @package_fragments, '' if $last =~ /(?:'|::)$/;
23 # the beginning of the variable, or an incomplete package name
24 my $incomplete = pop @package_fragments;
26 # recurse for the complete package fragments
28 for (@package_fragments) {
29 $stash = $stash->{"$_\::"};
32 # collect any variables from this stash
33 my @found = grep { /$re/ }
34 map { join '::', @package_fragments, $_ }
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
43 map { join '::', @package_fragments, $_ }
44 map { "$key$_" } # $key already has trailing ::
45 keys %{ $stash->{$key} };
48 return $orig->(@_), @found;
57 Devel::REPL::Plugin::CompletionDriver::Globals - Complete global variables, packages, namespaced functions
61 Shawn M Moore, C<< <sartak at gmail dot com> >>