blib/lib/Padre/Plugin/PerlTidy.pm untested

filter: Statementstmt 0.0% 0 / 211 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 330 CC 31 SCAR 36.8
CC
31
Cov
0%
_tidy
132.0
_export
56.0
_get_filename
30.0
CRAP 39.8
1 package Padre::Plugin::PerlTidy;
2  
3 use 5.008002;
4 use strict;
5 use warnings;
6 use File::Spec ();
7 use File::Basename ();
8 use Params::Util 1.04 ();
9 use Padre::Current ();
10 use Padre::Wx ();
11 use Padre::Plugin 0.92 ();
12 use FindBin qw($Bin);
13  
14 our $VERSION = '0.22';
15 our @ISA = 'Padre::Plugin';
16  
17 # This constant is used when storing and restoring the cursor position.
18 # Keep it small to limit resource use.
19 use constant SELECTIONSIZE => 40;
20  
21 sub padre_interfaces {
22 return (
23 'Padre::Plugin' => '0.92',
24 'Padre::Wx::Main' => '0.92',
25 );
26 }
27  
28 sub plugin_name {
29 return Wx::gettext('Perl Tidy');
30 }
31  
32 sub menu_plugins_simple {
33 my $self = shift;
34 return $self->plugin_name => [
35 Wx::gettext("Tidy the active document\tAlt+Shift+F") => sub {
36 $self->tidy_document;
37 },
38 Wx::gettext("Tidy the selected text\tAlt+Shift+G") => sub {
39 $self->tidy_selection;
40 },
41 '---' => undef,
42 Wx::gettext('Export active document to HTML file') => sub {
43 $self->export_document;
44 },
45 Wx::gettext('Export selected text to HTML file') => sub {
46 $self->export_selection;
47 },
48 ];
49 }
50  
51  
52  
53  
54  
55 ######################################################################
56 # Menu Handlers
57  
58 sub tidy_document {
59 my $self = shift;
60 my $main = $self->main;
61 my $document = $self->current->document;
62 my $text = $document->text_get;
63  
64 # Tidy the entire current document
65 $self->{over_ride} = 0;
66 my $perltidyrc = $self->_which_tidyrc;
67 my $tidy = $self->_tidy( $text, $perltidyrc );
68 unless ( defined Params::Util::_STRING($tidy) ) {
69 return;
70 }
71  
72 # Overwrite the entire document
73 my ( $regex, $start ) = $self->_get_cursor_position;
74 $document->text_set($tidy);
75 $self->_set_cursor_position( $regex, $start );
76 }
77  
78 sub tidy_selection {
79 my $self = shift;
80 my $main = $self->main;
81 my $current = $self->current;
82 my $text = $current->text;
83  
84 # Tidy the current selected text
85 $self->{over_ride} = 0;
86 my $perltidyrc = $self->_which_tidyrc;
87  
88 my $tidy = $self->_tidy( $text, $perltidyrc );
89 unless ( defined Params::Util::_STRING($tidy) ) {
90 return;
91 }
92  
93 # If the selected text does not have a newline at the end,
94 # trim off any that Perl::Tidy has added.
95 unless ( $text =~ /\n\z/ ) {
96 $tidy =~ s{\n\z}{};
97 }
98  
99 # Overwrite the selected text
100 $current->editor->ReplaceSelection($tidy);
101 }
102  
103 sub export_selection {
104 my $self = shift;
105 my $text = $self->current->text or return;
106 $self->_export($text);
107 }
108  
109 sub export_document {
110 my $self = shift;
111 my $document = $self->current->document or return;
112 $self->_export( $document->text_get );
113 }
114  
115  
116  
117  
118  
119 ######################################################################
120 # Support Methods
121  
122 sub _tidy {
123 my $self = shift;
124 my $source = shift;
125 my $perltidyrc = shift;
126 my $main = $self->main;
127 my $document = $self->current->document;
128  
129 # Check for problems
130 unless ( defined $source ) {
131 return;
132 }
133 unless ( $document->isa('Padre::Document::Perl') ) {
134 $main->error( Wx::gettext('Document is not a Perl document') );
135 return;
136 }
137  
138 my $destination = undef;
139 my $errorfile = undef;
140 my %tidyargs = (
141 argv => \'-nse -nst',
142 source => \$source,
143 destination => \$destination,
144 errorfile => \$errorfile,
145 );
146 if ( $self->{over_ride} ) {
147 $tidyargs{'perltidyrc'} = $perltidyrc;
148 }
149 if ( $self->{over_ride} ) {
150 $tidyargs{'perltidyrc'} = $perltidyrc;
151 }
152  
153 my $output;
154 if ( $main->config->info_on_statusbar ) {
155 $main->info( Wx::gettext("Running Tidy, don't forget to save changes.") );
156 } else {
157  
158 #Make sure output is visible...
159 $main->show_output(1);
160 $output = $main->output;
161 }
162  
163 # TODO: suppress the senseless warning from PerlTidy
164 eval {
165 require Perl::Tidy;
166 Perl::Tidy::perltidy(%tidyargs);
167 };
168  
169 if ( defined $errorfile ) {
170 $main->show_output(1);
171 $output = $main->output;
172 my $filename =
173 $document->filename
174 ? $document->filename
175 : $document->get_title;
176 my $width = length($filename) + 2;
177 $output->AppendText( "\n\n" . "-" x $width . "\n" . $filename . "\n" . "-" x $width . "\n" );
178 $output->AppendText("$errorfile\n");
179 }
180  
181 if ($@) {
182 $main->error( Wx::gettext("PerlTidy Error") . ":\n" . $@ );
183 return;
184 }
185  
186 if ( defined $errorfile ) {
187 $main->show_output(1);
188 $output = $main->output;
189 my $filename =
190 $document->filename
191 ? $document->filename
192 : $document->get_title;
193 my $width = length($filename) + 2;
194 $output->AppendText( "\n\n" . "-" x $width . "\n" . $filename . "\n" . "-" x $width . "\n" );
195 $output->AppendText("$errorfile\n");
196 }
197  
198 return $destination;
199 }
200  
201 sub _export {
202 my $self = shift;
203 my $src = shift;
204 my $main = $self->main;
205 my $document = $main->current->document;
206 return unless defined $src;
207  
208 if ( !$document->isa('Padre::Document::Perl') ) {
209 $main->error( Wx::gettext('Document is not a Perl document') );
210 return;
211 }
212  
213 my $filename = $self->_get_filename;
214 return unless defined $filename;
215  
216 my ( $output, $error );
217 my %tidyargs = (
218 argv => \'-html -nnn -nse -nst',
219 source => \$src,
220 destination => $filename,
221 errorfile => \$error,
222 );
223  
224 # Make sure output window is visible...
225 $main->show_output(1);
226 $output = $main->output;
227  
228 if ( my $tidyrc = $document->project->config->config_perltidy ) {
229 $tidyargs{perltidyrc} = $tidyrc;
230 $output->AppendText("Perl\::Tidy running with project-specific configuration $tidyrc\n");
231 } else {
232 $output->AppendText("Perl::Tidy running with default or user configuration\n");
233 }
234  
235 # TODO: suppress the senseless warning from PerlTidy
236 eval {
237 require Perl::Tidy;
238 Perl::Tidy::perltidy(%tidyargs);
239 };
240 if ($@) {
241 $main->error( Wx::gettext("PerlTidy Error") . ":\n" . $@ );
242 return;
243 }
244  
245 if ( defined $error ) {
246 my $width = length( $document->filename ) + 2;
247 my $main = Padre::Current->main;
248  
249 $output->AppendText( "\n\n" . "-" x $width . "\n" . $document->filename . "\n" . "-" x $width . "\n" );
250 $output->AppendText("$error\n");
251 $main->show_output(1);
252 }
253  
254 return;
255 }
256  
257 sub _get_filename {
258 my $self = shift;
259 my $main = $self->main;
260 my $document = $self->current->document or return;
261 my $filename = $document->filename;
262 my $dir = '';
263  
264 if ( defined $filename ) {
265 $dir = File::Basename::dirname($filename);
266 }
267  
268 while (1) {
269 my $dialog = Wx::FileDialog->new(
270 $main,
271 Wx::gettext("Save file as..."),
272 $dir,
273 ( $filename or $document->get_title ) . '.html',
274 "*.*",
275 Wx::wxFD_SAVE,
276 );
277 if ( $dialog->ShowModal == Wx::wxID_CANCEL ) {
278 return;
279 }
280 my $filename = $dialog->GetFilename;
281 $dir = $dialog->GetDirectory;
282 my $path = File::Spec->catfile( $dir, $filename );
283 if ( -e $path ) {
284 return $path
285 if $main->yes_no(
286 Wx::gettext("File already exists. Overwrite it?"),
287 Wx::gettext("Exist")
288 );
289 } else {
290 return $path;
291 }
292 }
293 }
294  
295 # parameter: $main, compiled regex
296 sub _set_cursor_position {
297 my $self = shift;
298 my $regex = shift;
299 my $start = shift;
300 my $editor = $self->current->editor or return;
301 my $text = $editor->GetTextRange(
302 ( $start - SELECTIONSIZE ) > 0 ? $start - SELECTIONSIZE
303 : 0,
304 ( $start + SELECTIONSIZE < $editor->GetLength ) ? $start + SELECTIONSIZE
305 : $editor->GetLength
306 );
307 eval {
308 if ( $text =~ /($regex)/ )
309 {
310 my $pos = $start + length $1;
311 $editor->SetCurrentPos($pos);
312 $editor->SetSelection( $pos, $pos );
313 }
314 };
315 $editor->goto_line_centerize( $editor->GetCurrentLine );
316 return;
317 }
318  
319 # parameter: $current
320 # returns: compiled regex, start position
321 # compiled regex is /^./ if no valid regex can be reconstructed.
322 sub _get_cursor_position {
323 my $self = shift;
324 my $editor = $self->current->editor or return;
325 my $pos = $editor->GetCurrentPos;
326  
327 my $start;
328 if ( ( $pos - SELECTIONSIZE ) > 0 ) {
329 $start = $pos - SELECTIONSIZE;
330 } else {
331 $start = 0;
332 }
333  
334 my $prefix = $editor->GetTextRange( $start, $pos );
335 my $regex;
336 eval {
337  
338 # Escape non-word chars
339 $prefix =~ s/(\W)/\\$1/gm;
340  
341 # Replace whitespace by regex \s+
342 $prefix =~ s/(\\\s+)/(\\s+|\\r*\\n)*/gm;
343  
344 $regex = qr{$prefix};
345 };
346 if ($@) {
347 $regex = qw{^.};
348 print STDERR @_;
349 }
350 return ( $regex, $start );
351 }
352  
353 # Pick the revelant tidyrc file
354 sub _which_tidyrc {
355 my $self = shift;
356  
357 # perl tidy Padre/tools
358 if ( $ENV{'PADRE_DEV'} ) {
359 my $perltidyrc = eval { File::Spec->catfile( $Bin, '../../tools/perltidyrc' ); };
360 if ( -e $perltidyrc ) {
361 $self->{over_ride} = 1;
362 return $perltidyrc;
363 }
364  
365 my $main = $self->main;
366 Wx::MessageBox(
367 Wx::gettext("You need to install from SVN Padre/tools."),
368 Wx::gettext("tools/perltidyrc missing"),
369 Wx::wxCANCEL,
370 $main,
371 );
372 }
373  
374 return;
375 }
376  
377 1;
378  
379 =pod
380  
381 =head1 NAME
382  
383 Padre::Plugin::PerlTidy - Format perl files using Perl::Tidy
384  
385 =head1 DESCRIPTION
386  
387 This is a simple plugin to run L<Perl::Tidy> on your source code.
388  
389 Currently there are no customisable options (since the Padre plugin system
390 doesn't support that yet) - however Perl::Tidy will use your normal .perltidyrc
391 file if it exists (see Perl::Tidy documentation).
392  
393 =head1 METHODS
394  
395 =head2 padre_interfaces
396  
397 Indicates our compatibility with Padre.
398  
399 =head2 plugin_name
400  
401 A simple accessor for the name of the plugin.
402  
403 =head2 menu_plugins_simple
404  
405 Menu items for this plugin.
406  
407 =head2 tidy_document
408  
409 Runs Perl::Tidy on the current document.
410  
411 =head2 export_document
412  
413 Export the current document as html.
414  
415 =head2 tidy_selection
416  
417 Runs Perl::Tidy on the current code selection.
418  
419 =head2 export_selection
420  
421 Export the current code selection as html.
422  
423 =cut