whitespace fixes
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / CompletionDriver / Methods.pm
CommitLineData
1716b200 1use strict;
2use warnings;
23d2a2ac 3package Devel::REPL::Plugin::CompletionDriver::Methods;
4use Devel::REPL::Plugin;
aa8b7647 5use namespace::autoclean;
23d2a2ac 6
3a400715 7sub BEFORE_PLUGIN {
8 my $self = shift;
9 for (qw/Completion FindVariable/) {
10 $self->load_plugin($_);
11 }
12}
d13b1087 13
23d2a2ac 14around 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->(@_);
d13b1087 39 $previous->isa('PPI::Token::Word') || $previous->isa('PPI::Token::Symbol')
23d2a2ac 40 or return $orig->(@_);
d13b1087 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 }
23d2a2ac 53
54 # now we have $class->$incomplete
55
56 my $metaclass = Class::MOP::Class->initialize($class);
57
58 my $re = qr/^\Q$incomplete/;
59
60 return $orig->(@_),
61 grep { $_ =~ $re }
32d646c7 62 map { $_->name }
63 $metaclass->get_all_methods;
23d2a2ac 64};
65
661;
67
cfd1094b 68__END__
69
70=head1 NAME
71
72Devel::REPL::Plugin::CompletionDriver::Methods - Complete class or object method names
73
30b459d4 74=head1 AUTHOR
75
76Shawn M Moore, C<< <sartak at gmail dot com> >>
77
cfd1094b 78=cut
23d2a2ac 79