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