3 package Devel::REPL::Plugin::CompletionDriver::Globals;
4 use Devel::REPL::Plugin;
5 use Devel::REPL::Plugin::Completion; # die early if cannot load
6 use namespace::autoclean;
10 $self->load_plugin('Completion');
13 around complete => sub {
15 my ($self, $text, $document) = @_;
17 my $last = $self->last_ppi_element($document);
20 unless $last->isa('PPI::Token::Symbol')
21 || $last->isa('PPI::Token::Word');
23 my $sigil = $last =~ s/^[\$\@\%\&\*]// ? $1 : undef;
24 my $re = qr/^\Q$last/;
26 my @package_fragments = split qr/::|'/, $last;
28 # split drops the last fragment if it's empty
29 push @package_fragments, '' if $last =~ /(?:'|::)$/;
31 # the beginning of the variable, or an incomplete package name
32 my $incomplete = pop @package_fragments;
34 # recurse for the complete package fragments
36 for (@package_fragments) {
37 $stash = $stash->{"$_\::"};
40 # collect any variables from this stash
41 my @found = grep { /$re/ }
42 map { join '::', @package_fragments, $_ }
45 # check to see if it's an incomplete package name, and add its variables
46 # so Devel<TAB> is completed correctly
47 for my $key (keys %$stash) {
48 next unless $key =~ /::$/; # only look at deeper packages
49 next unless $key =~ /^\Q$incomplete/; # only look at matching packages
51 map { join '::', @package_fragments, $_ }
52 map { "$key$_" } # $key already has trailing ::
53 keys %{ $stash->{$key} };
56 return $orig->(@_), @found;
65 Devel::REPL::Plugin::CompletionDriver::Globals - Complete global variables, packages, namespaced functions
69 Shawn M Moore, C<< <sartak at gmail dot com> >>