Commit | Line | Data |
1716b200 |
1 | use strict; |
2 | use warnings; |
23d2a2ac |
3 | package Devel::REPL::Plugin::CompletionDriver::Methods; |
6f4f9516 |
4 | |
54beb05d |
5 | our $VERSION = '1.003027'; |
6 | |
23d2a2ac |
7 | use Devel::REPL::Plugin; |
b1c83802 |
8 | use Devel::REPL::Plugin::Completion; # die early if cannot load |
aa8b7647 |
9 | use namespace::autoclean; |
23d2a2ac |
10 | |
3a400715 |
11 | sub BEFORE_PLUGIN { |
12 | my $self = shift; |
13 | for (qw/Completion FindVariable/) { |
14 | $self->load_plugin($_); |
15 | } |
16 | } |
d13b1087 |
17 | |
23d2a2ac |
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->(@_); |
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 | |
70 | 1; |
71 | |
cfd1094b |
72 | __END__ |
73 | |
74 | =head1 NAME |
75 | |
76 | Devel::REPL::Plugin::CompletionDriver::Methods - Complete class or object method names |
77 | |
30b459d4 |
78 | =head1 AUTHOR |
79 | |
80 | Shawn M Moore, C<< <sartak at gmail dot com> >> |
81 | |
cfd1094b |
82 | =cut |
23d2a2ac |
83 | |