Move to Moo for fast bootstrapping.
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / Methods.pm
1 package Devel::REPL::Plugin::CompletionDriver::Methods;
2 use Devel::REPL::Plugin;
3 use namespace::sweep;
4 use Package::Stash;
5 use Scalar::Util qw(blessed);
6
7 sub BEFORE_PLUGIN {
8     my $self = shift;
9     for (qw/Completion FindVariable/) {
10         $self->load_plugin($_);
11     }
12 }
13
14 around complete => sub {
15   my $orig = shift;
16   my ($self, $text, $document) = @_;
17
18   my $last = $self->last_ppi_element($document);
19   my $incomplete = '';
20
21   # handle an incomplete method name, and back up to the ->
22   if ($last->isa('PPI::Token::Word')) {
23       my $previous = $last->sprevious_sibling
24         or return $orig->(@_);
25       $previous->isa('PPI::Token::Operator') && $previous->content eq '->'
26         or return $orig->(@_);
27       $incomplete = $last->content;
28       $last = $previous;
29   }
30
31   # require a -> here
32   return $orig->(@_)
33     unless $last->isa('PPI::Token::Operator')
34         && $last->content eq '->';
35
36   # ..which is preceded by a word (class name)
37   my $previous = $last->sprevious_sibling
38     or return $orig->(@_);
39   $previous->isa('PPI::Token::Word') || $previous->isa('PPI::Token::Symbol')
40     or return $orig->(@_);
41   my $class;
42
43   # we have a variable, need to look up its class
44   if ($previous->isa('PPI::Token::Symbol')) {
45     my $object_ref = $self->find_variable($previous->content)
46       or return $orig->(@_);
47     $class = blessed($$object_ref)
48       or return $orig->(@_);
49   }
50   else  {
51     $class = $previous->content;
52   }
53
54   # now we have $class->$incomplete
55
56   my $metaclass = Package::Stash->new($class);
57
58   my $re = qr/^\Q$incomplete/;
59
60   return $orig->(@_),
61          grep { $_ =~ $re }
62          $metaclass->list_all_symbols('CODE');
63 };
64
65 1;
66
67 __END__
68
69 =head1 NAME
70
71 Devel::REPL::Plugin::CompletionDriver::Methods - Complete class or object method names
72
73 =head1 AUTHOR
74
75 Shawn M Moore, C<< <sartak at gmail dot com> >>
76
77 =cut
78