r61423@onn: sartak | 2008-06-02 16:00:33 -0400
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / Globals.pm
1 package Devel::REPL::Plugin::CompletionDriver::Globals;
2 use Devel::REPL::Plugin;
3 use namespace::clean -except => [ 'meta' ];
4
5 around complete => sub {
6   my $orig = shift;
7   my ($self, $text, $document) = @_;
8
9   my $last = $self->last_ppi_element($document);
10
11   return $orig->(@_)
12     unless $last->isa('PPI::Token::Symbol')
13         || $last->isa('PPI::Token::Word');
14
15   my $sigil = $last =~ s/^[\$\@\%\&\*]// ? $1 : undef;
16   my $re = qr/^\Q$last/;
17
18   my @package_fragments = split qr/::|'/, $last;
19
20   # split drops the last fragment if it's empty
21   push @package_fragments, '' if $last =~ /(?:'|::)$/;
22
23   # the beginning of the variable, or an incomplete package name
24   my $incomplete = pop @package_fragments;
25
26   # recurse for the complete package fragments
27   my $stash = \%::;
28   for (@package_fragments) {
29     $stash = $stash->{"$_\::"};
30   }
31
32   # collect any variables from this stash
33   my @found = grep { /$re/ }
34               map  { join '::', @package_fragments, $_ }
35               keys %$stash;
36
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
42       push @found,
43         map { join '::', @package_fragments, $_ }
44         map { "$key$_" } # $key already has trailing ::
45         keys %{ $stash->{$key} };
46   }
47
48   return $orig->(@_), @found;
49 };
50
51 1;
52
53 __END__
54
55 =head1 NAME
56
57 Devel::REPL::Plugin::CompletionDriver::Globals - Complete global variables, packages, namespaced functions
58
59 =head1 AUTHOR
60
61 Shawn M Moore, C<< <sartak at gmail dot com> >>
62
63 =cut
64