keep $VERSION right in the repo
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / Methods.pm
CommitLineData
1716b200 1use strict;
2use warnings;
23d2a2ac 3package Devel::REPL::Plugin::CompletionDriver::Methods;
6f4f9516 4
54beb05d 5our $VERSION = '1.003027';
6
23d2a2ac 7use Devel::REPL::Plugin;
b1c83802 8use Devel::REPL::Plugin::Completion; # die early if cannot load
aa8b7647 9use namespace::autoclean;
23d2a2ac 10
3a400715 11sub BEFORE_PLUGIN {
12 my $self = shift;
13 for (qw/Completion FindVariable/) {
14 $self->load_plugin($_);
15 }
16}
d13b1087 17
23d2a2ac 18around 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->(@_);
d13b1087 43 $previous->isa('PPI::Token::Word') || $previous->isa('PPI::Token::Symbol')
23d2a2ac 44 or return $orig->(@_);
d13b1087 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 }
23d2a2ac 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 }
32d646c7 66 map { $_->name }
67 $metaclass->get_all_methods;
23d2a2ac 68};
69
701;
71
cfd1094b 72__END__
73
74=head1 NAME
75
76Devel::REPL::Plugin::CompletionDriver::Methods - Complete class or object method names
77
30b459d4 78=head1 AUTHOR
79
80Shawn M Moore, C<< <sartak at gmail dot com> >>
81
cfd1094b 82=cut
23d2a2ac 83