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