Perl6/StdColorizerTask.pm untested

filter: Statementstmt 0.0% 0 / 155 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 257 CC 22 SCAR 42.7
CC
22
Cov
0%
run
182.0
finish
72.0
prepare
12.0
CRAP 71.8
1 package Padre::Plugin::Perl6::StdColorizerTask;
2 BEGIN {
3 $Padre::Plugin::Perl6::StdColorizerTask::VERSION = '0.71';
4 }
5  
6 # ABSTRACT: Perl 6 STD.pm Colorizer Task
7  
8 use strict;
9 use warnings;
10 use base 'Padre::Task';
11 use Scalar::Util ();
12 use Padre::Constant ();
13 use Padre::Logger;
14 use Padre::Util ();
15  
16 our $thread_running = 0;
17  
18 # This is run in the main thread before being handed
19 # off to a worker (background) thread. The Wx GUI can be
20 # polled for information here.
21 # If you don't need it, just inherit this default no-op.
22 sub prepare {
23 my $self = shift;
24  
25 # it is not running yet.
26 $self->{broken} = 0;
27  
28 return if $self->{_editor};
29 $self->{_editor} = Scalar::Util::refaddr( Padre::Current->editor );
30  
31 # assign a place in the work queue
32 if ($thread_running) {
33  
34 # single thread instance at a time please. aborting...
35 $self->{broken} = 1;
36 return "break";
37 }
38 $thread_running = 1;
39 return 1;
40 }
41  
42 sub is_broken {
43 my $self = shift;
44 return $self->{broken};
45 }
46  
47 my %colors = (
48 'comp_unit' => Padre::Constant::PADRE_BLUE,
49 'scope_declarator' => Padre::Constant::PADRE_RED,
50 'routine_declarator' => Padre::Constant::PADRE_RED,
51 'regex_declarator' => Padre::Constant::PADRE_RED,
52 'package_declarator' => Padre::Constant::PADRE_RED,
53 'statement_control' => Padre::Constant::PADRE_RED,
54 'block' => Padre::Constant::PADRE_BLACK,
55 'regex_block' => Padre::Constant::PADRE_BLACK,
56 'noun' => Padre::Constant::PADRE_BLACK,
57 'sigil' => Padre::Constant::PADRE_GREEN,
58 'variable' => Padre::Constant::PADRE_GREEN,
59 'assertion' => Padre::Constant::PADRE_GREEN,
60 'quote' => Padre::Constant::PADRE_MAGENTA,
61 'number' => Padre::Constant::PADRE_ORANGE,
62 'infix' => Padre::Constant::PADRE_DIM_GRAY,
63 'methodop' => Padre::Constant::PADRE_BLACK,
64 'pod_comment' => Padre::Constant::PADRE_GREEN,
65 'param_var' => Padre::Constant::PADRE_CRIMSON,
66 '_scalar' => Padre::Constant::PADRE_RED,
67 '_array' => Padre::Constant::PADRE_BROWN,
68 '_hash' => Padre::Constant::PADRE_ORANGE,
69 '_comment' => Padre::Constant::PADRE_GREEN,
70 );
71  
72 # This is run in the main thread after the task is done.
73 # It can update the GUI and do cleanup.
74 # You don't have to implement this if you don't need it.
75 sub finish {
76 my $self = shift;
77 my $mainwindow = shift;
78  
79 my $editor = Padre::Current->editor;
80 my $addr = delete $self->{_editor};
81 if ( not $addr or not $editor or $addr ne Scalar::Util::refaddr($editor) ) {
82  
83 # shall we try to locate the editor ?
84 $thread_running = 0;
85 return 1;
86 }
87  
88 my $doc = Padre::Current->document;
89 if ( not $doc ) {
90 $thread_running = 0;
91 return 1;
92 }
93  
94 if ( $self->{tokens} ) {
95 $doc->remove_color;
96 my @tokens = @{ $self->{tokens} };
97 for my $htoken (@tokens) {
98 my %token = %{$htoken};
99 my $color = $colors{ $token{rule} };
100 if ( defined $color ) {
101 my $len = length $token{buffer};
102 my $start = $token{last_pos} - $len;
103 $editor->StartStyling( $start, $color );
104 $editor->SetStyling( $len, $color );
105 } elsif ( $token{rule} eq '0' ) {
106  
107 # Why are there such cases?
108 } else {
109 warn "No color found for '$token{rule}'";
110 }
111 }
112 $doc->{tokens} = $self->{tokens};
113 } else {
114 $doc->{tokens} = [];
115 }
116  
117 if ( $self->{issues} ) {
118  
119 # pass errors/warnings to document...
120 $doc->{issues} = $self->{issues};
121 } else {
122 $doc->{issues} = [];
123 }
124  
125 # TODO: understand the case when opening Padre, if there
126 # is a Perl 6 file this will be called and it won't find the method
127 # probably this happens as the current file is different when the finish is called
128 # from the one that was called at the beginning (this is at open time with many
129 # buffers opening). There is a safeguard at the beginning of this sub but it seems
130 # to be failing its job, Maybe the Padre::Task should have a built in suppport to
131 # associate a task with a certain buffer
132 eval {
133 $doc->check_syntax_in_background( force => 1 );
134 $doc->get_outline( force => 1 );
135 };
136 if ($@) {
137  
138 #print "$doc\n";
139 }
140  
141 # finished here
142 $thread_running = 0;
143  
144 return 1;
145 }
146  
147 # Task thread subroutine
148 sub run {
149 my $self = shift;
150  
151 # temporary file for the process STDIN
152 require File::Temp;
153 my $tmp_in = File::Temp->new( SUFFIX => '_p6_in.txt' );
154 binmode $tmp_in, ':utf8';
155 print $tmp_in $self->{text};
156 delete $self->{text};
157 close $tmp_in or warn "cannot close $tmp_in\n";
158  
159 # temporary file for the process STDOUT
160 my $tmp_out = File::Temp->new( SUFFIX => '_p6_out.txt' );
161 close $tmp_out or warn "cannot close $tmp_out\n";
162  
163 # temporary file for the process STDERR
164 my $tmp_err = File::Temp->new( SUFFIX => '_p6_err.txt' );
165 close $tmp_err or warn "cannot close $tmp_out\n";
166  
167 my $tmp_dir = File::Spec->catfile( Padre::Constant::PLUGIN_DIR,
168 'Padre-Plugin-Perl6'
169 );
170 if ( not -e $tmp_dir ) {
171 require File::Path;
172 File::Path::mkpath($tmp_dir);
173 }
174 $tmp_dir .= '/';
175  
176 # construct the command
177 require Cwd;
178 require File::Basename;
179 require File::Spec;
180 my $cmd =
181 Padre::Perl->perl . " "
182 . File::Spec->catfile( Padre::Util::share('Perl6'), 'p6tokens.p5' )
183 . qq( "$tmp_in" "$tmp_out" "$tmp_err" "$tmp_dir");
184  
185 # all this is needed to prevent win32 platforms from:
186 # 1. popping out a command line on each run...
187 # 2. STD.pm uses Storable
188 # 3. Padre TaskManager does not like tasks that do Storable operations...
189 if (Padre::Constant::WIN32) {
190  
191 # on win32 platforms, we need to use this to prevent command line popups when using wperl.exe
192 require Win32;
193 require Win32::Process;
194  
195 sub print_error {
196 print Win32::FormatMessage( Win32::GetLastError() );
197 }
198  
199 my $p_obj;
200 Win32::Process::Create( $p_obj, Padre::Perl->perl, $cmd, 0, Win32::Process::DETACHED_PROCESS(), '.' )
201 or warn &print_error;
202 $p_obj->Wait( Win32::Process::INFINITE() );
203 } else {
204  
205 # On other platforms, we will simply use the perl way of calling a command
206 `$cmd`;
207 }
208  
209 my ( $out, $err );
210 {
211 local $/ = undef; #enable localized slurp mode
212  
213 # slurp the process output...
214 open my $CHLD_OUT, '<', $tmp_out or warn "Could not open $tmp_out";
215 binmode $CHLD_OUT;
216 $out = <$CHLD_OUT>;
217 close $CHLD_OUT or warn "Could not close $tmp_out\n";
218  
219 open my $CHLD_ERR, '<', $tmp_err or warn "Cannot open $tmp_err\n";
220 binmode $CHLD_ERR, ':utf8';
221 $err = <$CHLD_ERR>;
222 close $CHLD_ERR or warn "Could not close $tmp_err\n";
223 }
224  
225 if ($err) {
226  
227 # remove ANSI color escape sequences...
228 $err =~ s/\033\[\d+(?:;\d+(?:;\d+)?)?m//g;
229 TRACE(qq{STD.pm warning/error:\n$err\n}) if DEBUG;
230 my @messages = split /\n/, $err;
231 my ( $lineno, $type );
232 my $issues = [];
233 my $prefix = '';
234 for my $msg (@messages) {
235 if ( $msg =~ /^===SORRY!===/i ) {
236  
237 # the following lines are errors until we see the warnings section
238 $type = 'F';
239 } elsif ( $msg =~ /^Potential difficulties/i ) {
240  
241 # all rest are warnings...
242 $type = 'W';
243 $lineno = undef;
244 } elsif ( $msg =~ /^Undeclared routine/i ) {
245  
246 # all rest are warnings...
247 $prefix = 'Undeclared routine: ';
248 $lineno = undef;
249 $type = 'W';
250 } elsif ( $msg =~ /^\s+(.+?)\s+used at (\d+)/i ) {
251  
252 # record the line number
253 $lineno = $2;
254 } elsif ( $msg =~ /line (\d+):$/i ) {
255  
256 # record the line number
257 $lineno = $1;
258 }
259 if ($lineno) {
260 push @{$issues}, { line => $lineno, message => $prefix . $msg, type => $type, };
261 }
262 }
263 $self->{issues} = $issues;
264 }
265  
266 if ($out) {
267 eval {
268 require YAML::XS;
269 $self->{tokens} = YAML::XS::Load($out);
270 };
271 if ($@) {
272 warn "Exception: $@";
273 }
274 }
275  
276 return 1;
277 }
278  
279 1;
280  
281 __END__