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