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