Perl6/Outline.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 174 CC 20 SCAR 55.9
CC
20
Cov
0%
find
380.0
new
6.0
BEGIN
2.0
CRAP 268.3
1 package Padre::Plugin::Perl6::Outline;
2 BEGIN {
3 $Padre::Plugin::Perl6::Outline::VERSION = '0.71';
4 }
5  
6 # ABSTRACT: Perl 6 Outline background task
7  
8 use strict;
9 use warnings;
10  
11 use Params::Util ('_INSTANCE');
12 use Padre::Task ();
13  
14 our @ISA = 'Padre::Task';
15  
16 sub new {
17 my $self = shift->SUPER::new(@_);
18  
19 # Just convert the document to text for now.
20 # Later, we'll suck in more data from the project and
21 # other related documents to do syntax checks more awesomely.
22 unless ( _INSTANCE( $self->{document}, 'Padre::Document' ) ) {
23 die "Failed to provide a document to the syntax check task";
24 }
25  
26 # Remove the document entirely as we do this,
27 # as it won't be able to survive serialisation.
28 my $document = delete $self->{document};
29 $self->{tokens} = $document->{tokens};
30  
31 return $self;
32 }
33  
34 sub run {
35 my $self = shift;
36  
37 # Generate the outline
38 $self->{data} = $self->find();
39  
40 return 1;
41 }
42  
43 sub find {
44 my $self = shift;
45  
46 my @outline = ();
47  
48 if ( $self->{tokens} ) {
49 my $cur_pkg = {};
50 my @tokens = @{ $self->{tokens} };
51 my $symbol_type = 'package';
52 my $symbol_name = '';
53 my $symbol_line = -1;
54 my $symbol_suffix = '';
55 my $symbol_context = '';
56 my $context = 'GLOBAL';
57 for my $htoken (@tokens) {
58 my %token = %{$htoken};
59 my $tree = $token{tree};
60 if ($tree) {
61 my $buffer = $token{buffer};
62 my $lineno = $token{lineno};
63 if ( $tree
64 =~ /package_declarator__S_\d+(class|grammar|module|package|role|knowhow|slang) package_def longname/
65 )
66 {
67  
68 # (classes, grammars, modules, packages, roles) or main are always parent nodes
69 $symbol_type = $1;
70 $symbol_name .= $buffer;
71 $symbol_line = $lineno;
72 } elsif ( $tree
73 =~ /(package_declarator__S_\d+require module_name)|(statement_control__S_\d+use module_name)/ )
74 {
75  
76 # require/use a module
77 $symbol_type = "modules";
78 $symbol_name .= $buffer;
79 $symbol_line = $lineno;
80 } elsif ( $tree =~ /routine_declarator__S_\d+sub routine_def deflongname/ ) {
81  
82 # a subroutine
83 $symbol_type = "methods";
84 $symbol_suffix = " (subroutine)";
85 $symbol_name .= $buffer;
86 $symbol_line = $lineno;
87 } elsif ( $tree =~ /routine_declarator__\w+_\d+method method_def (longname|$)/ ) {
88  
89 # a method
90 if ( $buffer eq '!' ) {
91  
92 # private method...
93 $symbol_suffix = " (private)";
94 } elsif ( $buffer eq '^' ) {
95  
96 # class or .HOW method
97 $symbol_suffix = " (class)";
98 }
99 $symbol_type = "methods";
100 $symbol_name .= $buffer;
101 $symbol_line = $lineno;
102 } elsif ( $tree =~ /routine_declarator__\w+_\d+submethod method_def longname/ ) {
103  
104 # a submethod
105 $symbol_type = "methods";
106 $symbol_suffix = " (submethod)";
107 $symbol_name .= $buffer;
108 $symbol_line = $lineno;
109 } elsif ( $tree =~ /routine_declarator__\w+_\d+macro macro_def deflongname/ ) {
110  
111 # a macro
112 $symbol_type = "methods";
113 $symbol_suffix = " (macro)";
114 $symbol_name .= $buffer;
115 $symbol_line = $lineno;
116 } elsif ( $tree =~ /regex_declarator__\w+_\d+(regex|token|rule) regex_def deflongname/ ) {
117  
118 # a regex, token or rule declaration
119 $symbol_type = "regexes";
120 $symbol_name .= $buffer;
121 $symbol_line = $lineno;
122 } elsif ( $tree
123 =~ /scope_declarator__\w+_\d+(our|my|has|state|constant) scoped declarator variable_declarator variable/
124 )
125 {
126  
127 # a start for an attribute declaration
128 $symbol_type = "attributes";
129 $symbol_name .= $buffer;
130 $symbol_line = $lineno;
131 $symbol_suffix = $1;
132 } else {
133 if ( $symbol_name ne '' ) {
134 if ( $symbol_type eq 'class'
135 || $symbol_type eq 'grammar'
136 || $symbol_type eq 'module'
137 || $symbol_type eq 'package'
138 || $symbol_type eq 'role'
139 || $symbol_type eq 'knowhow'
140 || $symbol_type eq 'slang' )
141 {
142 $context = $symbol_name;
143 if ( not $cur_pkg->{name} ) {
144 $cur_pkg->{name} = 'GLOBAL';
145 }
146 push @outline, $cur_pkg;
147 $cur_pkg = {};
148 $cur_pkg->{name} = $symbol_name . " ($symbol_type)";
149 $cur_pkg->{line} = $symbol_line;
150 } else {
151 if ( $symbol_type eq 'attributes' ) {
152 if ( $symbol_name !~ /\./ ) {
153 $symbol_suffix = " (private, $symbol_suffix)";
154 } else {
155 $symbol_suffix = " ($symbol_suffix)";
156 }
157 }
158 $symbol_name .= $symbol_suffix;
159 push @{ $cur_pkg->{$symbol_type} },
160 {
161 name => $symbol_name,
162 line => $symbol_line,
163 };
164 }
165 $symbol_type = '';
166 $symbol_name = '';
167 $symbol_line = -1;
168 $symbol_suffix = '';
169 }
170 }
171 }
172 }
173  
174 if ( not $cur_pkg->{name} ) {
175 $cur_pkg->{name} = 'GLOBAL';
176 }
177 push @outline, $cur_pkg;
178  
179 }
180  
181 return \@outline;
182 }
183  
184 1;
185  
186  
187  
188 =pod
189  
190 =head1 NAME
191  
192 Padre::Plugin::Perl6::Outline - Perl 6 Outline background task
193  
194 =head1 VERSION
195  
196 version 0.71
197  
198 =head1 SYNOPSIS
199  
200 # by default, the text of the current document
201 # will be fetched as will the document's notebook page.
202 my $task = Padre::Plugin::Perl6::Outline->new;
203 $task->schedule;
204  
205 =head1 DESCRIPTION
206  
207 This class implements structure info gathering of Perl6 documents in
208 the background.
209 It inherits from L<Padre::Task::Outline>.
210 Please read its documentation!
211  
212 =head1 SEE ALSO
213  
214 This class inherits from L<Padre::Task::Outline> which
215 in turn is a L<Padre::Task> and its instances can be scheduled
216 using L<Padre::TaskManager>.
217  
218 =head1 AUTHORS
219  
220 =over 4
221  
222 =item *
223  
224 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
225  
226 =item *
227  
228 Gabor Szabo L<http://szabgab.com/>
229  
230 =back
231  
232 =head1 COPYRIGHT AND LICENSE
233  
234 This software is copyright (c) 2010 by Ahmad M. Zawawi.
235  
236 This is free software; you can redistribute it and/or modify it under
237 the same terms as the Perl 5 programming language system itself.
238  
239 =cut
240  
241  
242 __END__