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