Devel/REPL/Plugin/CompletionDriver/SOOT.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 183 CC 15 SCAR 31.7
CC
15
Cov
0%
_match_method_call
42.0
_try_complete_global
42.0
_show_matching_methods
12.0
CRAP 23.9
1 package Devel::REPL::Plugin::CompletionDriver::SOOT;
2 use Devel::REPL::Plugin;
3 use Scalar::Util qw(blessed);
4 use namespace::clean -except => [ 'meta' ];
5 use Term::ANSIColor qw(:constants :pushpop);
6  
7 sub BEFORE_PLUGIN {
8 my $self = shift;
9 $self->load_plugin('Completion');
10 }
11  
12 # returns array ref: [$ok, $show_prototype, $methname, $invocant]
13 sub _match_method_call {
14 my ($self, $last) = @_;
15 my $show_prototype = 0;
16 # If we're after a paren (and the rest below matches, too),
17 # then we want to print method signatures.
18 if ($last->isa('PPI::Token::Structure') and $last->content eq '(') {
19 $show_prototype = 1;
20 $last = $last->parent->sprevious_sibling;
21 last if not $last;
22 }
23 # match method name
24 return [0, $show_prototype]
25 if not $last or not $last->isa('PPI::Token::Word');
26
27 # match arrow operator
28 my $prev = $last->sprevious_sibling;
29 return [0, $show_prototype]
30 if not $prev or not $prev->isa('PPI::Token::Operator') or $prev->content ne '->';
31  
32 # match invocant
33 my $invocant = $prev->sprevious_sibling;
34 return [0, $show_prototype]
35 if not $invocant
36 or not ($invocant->isa('PPI::Token::Symbol') || $invocant->isa('PPI::Token::Word'));
37  
38 return [1, $show_prototype, $last, $invocant];
39 }
40  
41 sub _is_soot_object {
42 my $var = shift;
43 return()
44 if not blessed($var)
45 or not ($var->isa('TObject') or SOOT::API->is_soot_class(ref($var)));
46 return 1;
47 }
48  
49 sub _show_matching_methods {
50 my ($self, $var, $invocant, $methname, $document, $show_prototype) = @_;
51  
52 my $class = $var->Class;
53 if ($show_prototype) {
54 my @meth = $class->soot_method_complete_proto_str($methname->content, 1);
55 print "\n" if @meth;
56 for (@meth) {
57 print LOCALCOLOR YELLOW $_;
58 print "\n";
59 }
60 local $| = 1;
61 print LOCALCOLOR UNDERLINE $self->prompt;
62 print $document->content;
63 return [];
64 }
65 else {
66 my @meth = $class->soot_method_complete_name($methname->content);
67 return [map "$_(", @meth];
68 }
69 }
70  
71 sub _try_complete_lexical {
72 my ($self, $invocant, $methname, $document, $show_prototype) = @_;
73
74 my $invocant_str = $invocant->content;
75 my $lexenv = $self->lexical_environment();
76 my $cxt = $lexenv->get_context('_');
77 my $var = $cxt->{$invocant_str};
78  
79 return if not _is_soot_object($var);
80  
81 return $self->_show_matching_methods($var, $invocant, $methname, $document, $show_prototype);
82 }
83  
84 sub _try_complete_global {
85 my ($self, $invocant, $methname, $document, $show_prototype) = @_;
86
87 my $invocant_str = $invocant->content;
88  
89 my $sigil = $invocant_str =~ s/^([\$\@\%\&\*])// ? $1 : 0; # remove sigil
90  
91 my @package_fragments = split qr/::|'/, $invocant_str;
92  
93 # Almost verbatim from the ::Globals completion driver...
94  
95 # split drops the last fragment if it's empty
96 push @package_fragments, '' if $invocant_str =~ /(?:'|::)$/;
97  
98 # the beginning of the variable, or an incomplete package name
99 my $incomplete = pop @package_fragments;
100  
101 # recurse for the complete package fragments
102 my $stash = \%::;
103 for (@package_fragments) {
104 $stash = $stash->{"$_\::"};
105 }
106  
107 # collect any variables from this stash
108 my @found = grep { $_ eq $invocant_str }
109 keys %$stash;
110 return if not @found;
111  
112 my $varname = shift(@found);
113 my $var = $stash->{$varname};
114 $var = *{$var}{SCALAR};
115 $var = $$var if $var;
116  
117 return if not _is_soot_object($var);
118  
119 return $self->_show_matching_methods($var, $invocant, $methname, $document, $show_prototype);
120 }
121  
122 around complete => sub {
123 my $orig = shift;
124 my ($self, $text, $document) = @_;
125  
126 # The last token the user has entered (will dive as deep as possible into the PPI structure)
127 my $last = $self->last_ppi_element($document);
128  
129 my $rv = $self->_match_method_call($last);
130 my ($ok, $show_prototype, $methname, $invocant) = @{$rv||[]};
131 return if not $ok;
132  
133 my $completions;
134  
135 $completions = $self->_try_complete_lexical($invocant, $methname, $document, $show_prototype);
136 return $orig->(@_), @$completions if $completions;
137  
138 $completions = $self->_try_complete_global($invocant, $methname, $document, $show_prototype);
139 return $orig->(@_), @$completions if $completions;
140  
141 return $orig->(@_);
142 };
143  
144 1;
145  
146 __END__