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