| 1 | package Padre::Plugin::Perl6::QuickFix; | ||
| 2 | BEGIN { | ||
| 3 | $Padre::Plugin::Perl6::QuickFix::VERSION = '0.71'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: Padre Perl 6 Quick Fix Provider | ||
| 7 | use 5.008; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | use Padre::Wx (); | ||
| 12 | use Padre::Plugin::Perl6::Util (); | ||
| 13 | use Padre::QuickFix (); | ||
| 14 | |||
| 15 | our @ISA = ('Padre::QuickFix'); | ||
| 16 | |||
| 17 | # | ||
| 18 | # Constructor. | ||
| 19 | # No need to override this | ||
| 20 | # | ||
| 21 | sub new { | ||
| 22 | my ($class) = @_; | ||
| 23 | |||
| 24 | # Create myself :) | ||
| 25 | my $self = bless {}, $class; | ||
| 26 | |||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | # | ||
| 31 | # Tries to find quick fixes for errors in the current line | ||
| 32 | # | ||
| 33 | sub quick_fix_list { | ||
| 34 | my ( $self, $doc, $editor ) = @_; | ||
| 35 | |||
| 36 | if ( not defined $doc->{issues} ) { | ||
| 37 | $doc->{issues} = []; | ||
| 38 | } | ||
| 39 | |||
| 40 | my $nl = Padre::Plugin::Perl6::Util::guess_newline( $editor->GetText ); | ||
| 41 | my $current_line_no = $editor->GetCurrentLine; | ||
| 42 | |||
| 43 | my @items = (); | ||
| 44 | my $num_issues = scalar @{ $doc->{issues} }; | ||
| 45 | foreach my $issue ( @{ $doc->{issues} } ) { | ||
| 46 | my $issue_line_no = $issue->{line} - 1; | ||
| 47 | if ( $issue_line_no == $current_line_no ) { | ||
| 48 | my $issue_msg = $issue->{msg}; | ||
| 49 | $issue_msg =~ s/^\s+|\s+$//g; | ||
| 50 | if ( $issue_msg =~ /^Variable\s+(.+?)\s+is not predeclared at/i ) { | ||
| 51 | |||
| 52 | my $var_name = $1; | ||
| 53 | |||
| 54 | # Fixes the following: | ||
| 55 | # $foo = 1; | ||
| 56 | # into: | ||
| 57 | # my $foo; | ||
| 58 | # $foo = 1; | ||
| 59 | push @items, { | ||
| 60 | text => sprintf( Wx::gettext('Insert declaration for %s'), $var_name ), | ||
| 61 | listener => sub { | ||
| 62 | |||
| 63 | #Insert a variable declaration before the start of the current line | ||
| 64 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 65 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 66 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 67 | my $indent = ( $line_text =~ /(^\s+)/ ) ? $1 : ''; | ||
| 68 | $line_text = "${indent}my $var_name;$nl" . $line_text; | ||
| 69 | $editor->SetSelection( $line_start, $line_end ); | ||
| 70 | $editor->ReplaceSelection($line_text); | ||
| 71 | }, | ||
| 72 | }; | ||
| 73 | |||
| 74 | } elsif ( $issue_msg =~ /^Undeclared routine:\s+(.+?)\s+used/i ) { | ||
| 75 | |||
| 76 | my $routine_name = $1; | ||
| 77 | |||
| 78 | #flow control keywords | ||
| 79 | my @flow_control_keywords = ( | ||
| 80 | 'for', 'given', 'if', 'loop', 'repeat', | ||
| 81 | 'unless', 'until', 'when', 'while', | ||
| 82 | ); | ||
| 83 | foreach my $keyword (@flow_control_keywords) { | ||
| 84 | if ( $keyword eq $routine_name ) { | ||
| 85 | |||
| 86 | # Fixes the following: | ||
| 87 | # if() { }; | ||
| 88 | # into: | ||
| 89 | # if () { }; | ||
| 90 | push @items, { | ||
| 91 | text => sprintf( Wx::gettext('Insert a space after %s'), $keyword ), | ||
| 92 | listener => sub { | ||
| 93 | |||
| 94 | #Insert a space before brace | ||
| 95 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 96 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 97 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 98 | $line_text =~ s/$keyword\(/$keyword \(/; | ||
| 99 | $editor->SetSelection( $line_start, $line_end ); | ||
| 100 | $editor->ReplaceSelection($line_text); | ||
| 101 | }, | ||
| 102 | }; | ||
| 103 | |||
| 104 | last; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | # Fixes the following: | ||
| 109 | # foo(); | ||
| 110 | # into: | ||
| 111 | # sub foo() { | ||
| 112 | # #XXX-implement | ||
| 113 | # } | ||
| 114 | # foo(); | ||
| 115 | push @items, { | ||
| 116 | text => sprintf( Wx::gettext('Insert routine %s'), $routine_name ), | ||
| 117 | listener => sub { | ||
| 118 | |||
| 119 | #Insert an empty routine definition before the current line | ||
| 120 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 121 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 122 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 123 | my $indent = ( $line_text =~ /(^\s+)/ ) ? $1 : ''; | ||
| 124 | $line_text = | ||
| 125 | "${indent}sub $routine_name {$nl" | ||
| 126 | . "${indent}\t#XXX-implement$nl" | ||
| 127 | . "${indent}}$nl" | ||
| 128 | . $line_text; | ||
| 129 | $editor->SetSelection( $line_start, $line_end ); | ||
| 130 | $editor->ReplaceSelection($line_text); | ||
| 131 | }, | ||
| 132 | }; | ||
| 133 | |||
| 134 | } elsif ( $issue_msg =~ /^Obsolete use of . to concatenate strings/i ) { | ||
| 135 | |||
| 136 | # Fixes the following: | ||
| 137 | # $string = "a" . "b"; | ||
| 138 | # into: | ||
| 139 | # $string = "a" ~ "b"; | ||
| 140 | push @items, { | ||
| 141 | text => Wx::gettext('Use ~ instead of . for string concatenation'), | ||
| 142 | listener => sub { | ||
| 143 | |||
| 144 | #replace first '.' with '~' in the current line | ||
| 145 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 146 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 147 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 148 | $line_text =~ s/\./~/; | ||
| 149 | $editor->SetSelection( $line_start, $line_end ); | ||
| 150 | $editor->ReplaceSelection($line_text); | ||
| 151 | }, | ||
| 152 | }; | ||
| 153 | |||
| 154 | } elsif ( $issue_msg =~ /^Obsolete use of -> to call a method/i ) { | ||
| 155 | |||
| 156 | # Fixes the following: | ||
| 157 | # P->foo; | ||
| 158 | # into: | ||
| 159 | # P.foo; | ||
| 160 | push @items, { | ||
| 161 | text => Wx::gettext('Use . for method call'), | ||
| 162 | listener => sub { | ||
| 163 | |||
| 164 | #Replace first '->' with '.' in the current line | ||
| 165 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 166 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 167 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 168 | $line_text =~ s/\-\>/\./; | ||
| 169 | $editor->SetSelection( $line_start, $line_end ); | ||
| 170 | $editor->ReplaceSelection($line_text); | ||
| 171 | }, | ||
| 172 | }; | ||
| 173 | |||
| 174 | } elsif ( $issue_msg =~ /^Obsolete use of C\+\+ constructor syntax/i ) { | ||
| 175 | |||
| 176 | # Fixes the following: | ||
| 177 | # new Foo; | ||
| 178 | # into: | ||
| 179 | # Foo.new; | ||
| 180 | push @items, { | ||
| 181 | text => Wx::gettext('Use Perl 6 constructor syntax'), | ||
| 182 | listener => sub { | ||
| 183 | |||
| 184 | #Replace first 'new Foo' with 'Foo.new' in the current line | ||
| 185 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 186 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 187 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 188 | |||
| 189 | #new Point/new Point::Bar/new Point-In-Box | ||
| 190 | $line_text =~ s/new\s+([\w\-\:\:]+)?/$1.new/; | ||
| 191 | $editor->SetSelection( $line_start, $line_end ); | ||
| 192 | $editor->ReplaceSelection($line_text); | ||
| 193 | }, | ||
| 194 | }; | ||
| 195 | |||
| 196 | } elsif ( $issue_msg =~ /^Obsolete use of C-style "for \(;;\)" loop/i ) { | ||
| 197 | |||
| 198 | # Fixes the following: | ||
| 199 | # for(;;) { }; | ||
| 200 | # into: | ||
| 201 | # loop(;;) { }; | ||
| 202 | push @items, { | ||
| 203 | text => Wx::gettext('Use loop (;;) for looping'), | ||
| 204 | listener => sub { | ||
| 205 | |||
| 206 | #Replace first 'for (;;)' with 'loop (;;)' in the current line | ||
| 207 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 208 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 209 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 210 | $line_text =~ s/for\s+\(/loop (/; | ||
| 211 | $editor->SetSelection( $line_start, $line_end ); | ||
| 212 | $editor->ReplaceSelection($line_text); | ||
| 213 | }, | ||
| 214 | }; | ||
| 215 | |||
| 216 | } elsif ( $issue_msg =~ /^Obsolete use of \[-1\] subscript to access final element/i ) { | ||
| 217 | |||
| 218 | # Fixes the following: | ||
| 219 | # [-1]; | ||
| 220 | # into: | ||
| 221 | # [*-1]; | ||
| 222 | push @items, { | ||
| 223 | text => Wx::gettext('Use [*-1] to access final element'), | ||
| 224 | listener => sub { | ||
| 225 | |||
| 226 | #Replace first '[-1]' with '[*-1]' in the current line | ||
| 227 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 228 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 229 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 230 | $line_text =~ s/\[\s*-1\s*\]/[*-1]/; | ||
| 231 | $editor->SetSelection( $line_start, $line_end ); | ||
| 232 | $editor->ReplaceSelection($line_text); | ||
| 233 | }, | ||
| 234 | }; | ||
| 235 | |||
| 236 | } elsif ( $issue_msg =~ /^Obsolete use of rand\(N\)/i ) { | ||
| 237 | |||
| 238 | # Fixes the following: | ||
| 239 | # rand(10); | ||
| 240 | # into: | ||
| 241 | # 10.pick; | ||
| 242 | push @items, { | ||
| 243 | text => Wx::gettext('Use N.pick for a random number'), | ||
| 244 | listener => sub { | ||
| 245 | |||
| 246 | #Replace rand(N) with N.pick' in the current line | ||
| 247 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 248 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 249 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 250 | $line_text =~ s/rand\s*\(\s*(.+?)\s*\)/$1.pick/; | ||
| 251 | $editor->SetSelection( $line_start, $line_end ); | ||
| 252 | $editor->ReplaceSelection($line_text); | ||
| 253 | }, | ||
| 254 | }; | ||
| 255 | |||
| 256 | # Fixes the following: | ||
| 257 | # rand(10); | ||
| 258 | # into: | ||
| 259 | # (1..10).pick; | ||
| 260 | push @items, { | ||
| 261 | text => Wx::gettext('Use (1..N).pick for a random number'), | ||
| 262 | listener => sub { | ||
| 263 | |||
| 264 | #Replace rand(N) with (1..N).pick' in the current line | ||
| 265 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 266 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 267 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 268 | $line_text =~ s/rand\s*\(\s*(.+?)\s*\)/(1..$1).pick/; | ||
| 269 | $editor->SetSelection( $line_start, $line_end ); | ||
| 270 | $editor->ReplaceSelection($line_text); | ||
| 271 | }, | ||
| 272 | }; | ||
| 273 | |||
| 274 | } elsif ( $issue_msg =~ /^Please use \.\.\* for indefinite range/i ) { | ||
| 275 | |||
| 276 | # Fixes the following: | ||
| 277 | # [1..]; | ||
| 278 | # into: | ||
| 279 | # [1..*]; | ||
| 280 | push @items, { | ||
| 281 | text => Wx::gettext('Use [N..*] for indefinite range'), | ||
| 282 | listener => sub { | ||
| 283 | |||
| 284 | #Replace first '[1..]' with '[1..*]' in the current line | ||
| 285 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 286 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 287 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 288 | $line_text =~ s/\[\s*(.+?)\.\.\s*\]/\[$1..*\]/; | ||
| 289 | $editor->SetSelection( $line_start, $line_end ); | ||
| 290 | $editor->ReplaceSelection($line_text); | ||
| 291 | }, | ||
| 292 | }; | ||
| 293 | |||
| 294 | } elsif ( $issue_msg =~ /^Please use \!\! rather than \:\:/i ) { | ||
| 295 | |||
| 296 | # Fixes the following: | ||
| 297 | # 1 == 2 ?? 1 :: 2; | ||
| 298 | # into: | ||
| 299 | # 1 == 2 ?? 1 !! 2; | ||
| 300 | push @items, { | ||
| 301 | text => Wx::gettext('Use !! for conditional operator'), | ||
| 302 | listener => sub { | ||
| 303 | |||
| 304 | #Replace first '!!' with '::' in the current line | ||
| 305 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 306 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 307 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 308 | $line_text =~ s/\:\:/!!/; | ||
| 309 | $editor->SetSelection( $line_start, $line_end ); | ||
| 310 | $editor->ReplaceSelection($line_text); | ||
| 311 | }, | ||
| 312 | }; | ||
| 313 | |||
| 314 | } elsif ( $issue_msg =~ /^Precedence too loose within \?\?\!\!/i ) { | ||
| 315 | |||
| 316 | # Fixes errors like: | ||
| 317 | # 42 ?? 1,2,3 Z 4,5,6 !! 1,2,3 X 4,5,6; | ||
| 318 | # into: | ||
| 319 | # 42 ?? (1,2,3 Z 4,5,6) !! 1,2,3 X 4,5,6; | ||
| 320 | push @items, { | ||
| 321 | text => Wx::gettext('Use ?? (...) !! to avoid precedence bugs'), | ||
| 322 | listener => sub { | ||
| 323 | |||
| 324 | #Replace '?? ... !!' with '?? (...) !!' in the current line | ||
| 325 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 326 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 327 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 328 | |||
| 329 | #XXX- handle multiple lines... | ||
| 330 | $line_text =~ s/\?\?(.+?)\!\!/??($1)!!/; | ||
| 331 | $editor->SetSelection( $line_start, $line_end ); | ||
| 332 | $editor->ReplaceSelection($line_text); | ||
| 333 | }, | ||
| 334 | }; | ||
| 335 | |||
| 336 | } elsif ( $issue_msg =~ /^Obsolete use of \?\: for the conditional operator/i ) { | ||
| 337 | |||
| 338 | # Fixes the following: | ||
| 339 | # (1 == 1) ? 1 : 2 | ||
| 340 | # into: | ||
| 341 | # (1 == 1) ?? 1 !! 2 | ||
| 342 | push @items, { | ||
| 343 | text => Wx::gettext('Use ?? !! for the conditional operator'), | ||
| 344 | listener => sub { | ||
| 345 | |||
| 346 | #Replace first '? ... :' with '?? ... !!' in the current line | ||
| 347 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 348 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 349 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 350 | $line_text =~ s/\?\s*(.+?)\s*\:/?? $1 !!/; | ||
| 351 | $editor->SetSelection( $line_start, $line_end ); | ||
| 352 | $editor->ReplaceSelection($line_text); | ||
| 353 | }, | ||
| 354 | }; | ||
| 355 | |||
| 356 | } elsif ( $issue_msg =~ /^Possible obsolete use of \.\= as append operator/i ) { | ||
| 357 | |||
| 358 | # Fixes the following: | ||
| 359 | # $string .= "a"; | ||
| 360 | # into: | ||
| 361 | # $string ~= "a"; | ||
| 362 | push @items, { | ||
| 363 | text => Wx::gettext('Use ~= for string concatenation'), | ||
| 364 | listener => sub { | ||
| 365 | |||
| 366 | #Replace first '=.' with '=~' in the current line | ||
| 367 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 368 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 369 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 370 | $line_text =~ s/\.\=/~=/; | ||
| 371 | $editor->SetSelection( $line_start, $line_end ); | ||
| 372 | $editor->ReplaceSelection($line_text); | ||
| 373 | }, | ||
| 374 | }; | ||
| 375 | |||
| 376 | } elsif ( $issue_msg =~ /^Obsolete use of \=\~ to do pattern matching/i ) { | ||
| 377 | |||
| 378 | # Fixes the following: | ||
| 379 | # $string =~ /abc/; | ||
| 380 | # into: | ||
| 381 | # $string ~~ /abc/; | ||
| 382 | push @items, { | ||
| 383 | text => Wx::gettext('Use ~~ for pattern matching'), | ||
| 384 | listener => sub { | ||
| 385 | |||
| 386 | #Replace first '=.' with '=~' in the current line | ||
| 387 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 388 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 389 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 390 | $line_text =~ s/\=\~/~~/; | ||
| 391 | $editor->SetSelection( $line_start, $line_end ); | ||
| 392 | $editor->ReplaceSelection($line_text); | ||
| 393 | }, | ||
| 394 | }; | ||
| 395 | |||
| 396 | } elsif ( $issue_msg =~ /^Obsolete use of \!\~ to do negated pattern matching/i ) { | ||
| 397 | |||
| 398 | # Fixes the following: | ||
| 399 | # $string !~ /abc/; | ||
| 400 | # into: | ||
| 401 | # $string !~~ /abc/; | ||
| 402 | push @items, { | ||
| 403 | text => Wx::gettext('Use !~~ for negated pattern matching'), | ||
| 404 | listener => sub { | ||
| 405 | |||
| 406 | #Replace first '!~' with '!~~' in the current line | ||
| 407 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 408 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 409 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 410 | $line_text =~ s/\!\~/!~~/; | ||
| 411 | $editor->SetSelection( $line_start, $line_end ); | ||
| 412 | $editor->ReplaceSelection($line_text); | ||
| 413 | }, | ||
| 414 | }; | ||
| 415 | |||
| 416 | } elsif ( $issue_msg =~ /^Obsolete use of >> to do right shift/i ) { | ||
| 417 | |||
| 418 | # Fixes the following: | ||
| 419 | # 2 >> 1; | ||
| 420 | # into: | ||
| 421 | # 2 +> 1; | ||
| 422 | push @items, { | ||
| 423 | text => Wx::gettext('Use +> for numeric right shift'), | ||
| 424 | listener => sub { | ||
| 425 | |||
| 426 | #Replace first '>>' with '+>' in the current line | ||
| 427 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 428 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 429 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 430 | $line_text =~ s/\>\>/+>/; | ||
| 431 | $editor->SetSelection( $line_start, $line_end ); | ||
| 432 | $editor->ReplaceSelection($line_text); | ||
| 433 | }, | ||
| 434 | }; | ||
| 435 | |||
| 436 | # Fixes the following: | ||
| 437 | # 100 >> 1; | ||
| 438 | # into: | ||
| 439 | # 100 ~> 1; | ||
| 440 | push @items, { | ||
| 441 | text => Wx::gettext('Use ~> for string right shift'), | ||
| 442 | listener => sub { | ||
| 443 | |||
| 444 | #Replace first '>>' with '~>' in the current line | ||
| 445 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 446 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 447 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 448 | $line_text =~ s/\>\>/~>/; | ||
| 449 | $editor->SetSelection( $line_start, $line_end ); | ||
| 450 | $editor->ReplaceSelection($line_text); | ||
| 451 | }, | ||
| 452 | }; | ||
| 453 | } elsif ( $issue_msg =~ /^Obsolete use of << to do left shift/i ) { | ||
| 454 | |||
| 455 | # Fixes the following: | ||
| 456 | # 2 << 1; | ||
| 457 | # into: | ||
| 458 | # 2 +< 1; | ||
| 459 | push @items, { | ||
| 460 | text => Wx::gettext('Use +< for numeric left shift'), | ||
| 461 | listener => sub { | ||
| 462 | |||
| 463 | #Replace first '<<' with '+<' in the current line | ||
| 464 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 465 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 466 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 467 | $line_text =~ s/\<\</+</; | ||
| 468 | $editor->SetSelection( $line_start, $line_end ); | ||
| 469 | $editor->ReplaceSelection($line_text); | ||
| 470 | }, | ||
| 471 | }; | ||
| 472 | |||
| 473 | # Fixes the following: | ||
| 474 | # 100 << 1; | ||
| 475 | # into: | ||
| 476 | # 100 ~< 1; | ||
| 477 | push @items, { | ||
| 478 | text => Wx::gettext('Use ~< for string left shift'), | ||
| 479 | listener => sub { | ||
| 480 | |||
| 481 | #Replace first '<<' with '~<' in the current line | ||
| 482 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 483 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 484 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 485 | $line_text =~ s/\<\</~</; | ||
| 486 | $editor->SetSelection( $line_start, $line_end ); | ||
| 487 | $editor->ReplaceSelection($line_text); | ||
| 488 | }, | ||
| 489 | }; | ||
| 490 | |||
| 491 | } elsif ( $issue_msg =~ /^Obsolete use of \$\@ variable as eval error/i ) { | ||
| 492 | |||
| 493 | # Fixes the following: | ||
| 494 | # $@; | ||
| 495 | # into: | ||
| 496 | # $!; | ||
| 497 | push @items, { | ||
| 498 | text => Wx::gettext('Use $! for eval errors'), | ||
| 499 | listener => sub { | ||
| 500 | |||
| 501 | #Replace first '$@' with '$!' in the current line | ||
| 502 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 503 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 504 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 505 | $line_text =~ s/\$\@/\$!/; | ||
| 506 | $editor->SetSelection( $line_start, $line_end ); | ||
| 507 | $editor->ReplaceSelection($line_text); | ||
| 508 | }, | ||
| 509 | }; | ||
| 510 | |||
| 511 | } elsif ( $issue_msg =~ /^Obsolete use of \$\] variable/i ) { | ||
| 512 | |||
| 513 | # Fixes the following: | ||
| 514 | # $]; | ||
| 515 | # into: | ||
| 516 | # $::PERL_VERSION; | ||
| 517 | push @items, { | ||
| 518 | text => Wx::gettext('Use $::PERL_VERSION'), | ||
| 519 | listener => sub { | ||
| 520 | |||
| 521 | #Replace first '$]' with '$::PERL_VERSION' in the current line | ||
| 522 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 523 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 524 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 525 | $line_text =~ s/\$\]/\$::PERL_VERSION/; | ||
| 526 | $editor->SetSelection( $line_start, $line_end ); | ||
| 527 | $editor->ReplaceSelection($line_text); | ||
| 528 | }, | ||
| 529 | }; | ||
| 530 | |||
| 531 | } | ||
| 532 | |||
| 533 | } | ||
| 534 | |||
| 535 | } | ||
| 536 | |||
| 537 | if ($num_issues) { | ||
| 538 | |||
| 539 | # add "comment error line" as the last resort to solving an issue | ||
| 540 | foreach my $issue ( @{ $doc->{issues} } ) { | ||
| 541 | my $issue_line_no = $issue->{line} - 1; | ||
| 542 | if ( $issue_line_no == $current_line_no ) { | ||
| 543 | |||
| 544 | # Fixes the following: | ||
| 545 | # some_weird_error(); | ||
| 546 | # into: | ||
| 547 | # # some_weird_error(); | ||
| 548 | push @items, { | ||
| 549 | text => Wx::gettext('Comment error line'), | ||
| 550 | listener => sub { | ||
| 551 | |||
| 552 | # comment current error by putting a hash and a space | ||
| 553 | # since #( is an embedded comment in Perl 6! see S02:166 | ||
| 554 | my $line_start = $editor->PositionFromLine($current_line_no); | ||
| 555 | my $line_end = $editor->GetLineEndPosition($current_line_no); | ||
| 556 | my $line_text = $editor->GetTextRange( $line_start, $line_end ); | ||
| 557 | $line_text = "# ${line_text}"; | ||
| 558 | $editor->SetSelection( $line_start, $line_end ); | ||
| 559 | $editor->ReplaceSelection($line_text); | ||
| 560 | }, | ||
| 561 | }; | ||
| 562 | last; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | |||
| 566 | } else { | ||
| 567 | |||
| 568 | # No issues; let us provide a some helpful quick fixes | ||
| 569 | my $selected_text = $editor->GetSelectedText; | ||
| 570 | if ( $selected_text && $selected_text =~ /[\n\r]/ ) { | ||
| 571 | |||
| 572 | # Fixes the following: | ||
| 573 | # faulty_code(); | ||
| 574 | # into: | ||
| 575 | # try { | ||
| 576 | # faulty_code(); | ||
| 577 | # | ||
| 578 | # CATCH { | ||
| 579 | # warn "oops: $!"; | ||
| 580 | # } | ||
| 581 | # } | ||
| 582 | |||
| 583 | push @items, { | ||
| 584 | text => Wx::gettext('Surround with try { ... }'), | ||
| 585 | listener => sub { | ||
| 586 | |||
| 587 | # Surround the 'selection' with a try { 'selection' CATCH { } } | ||
| 588 | my $line_start = | ||
| 589 | $editor->PositionFromLine( $editor->LineFromPosition( $editor->GetSelectionStart ) ); | ||
| 590 | my $line_end = $editor->PositionFromLine( $editor->LineFromPosition( $editor->GetSelectionEnd ) ); | ||
| 591 | |||
| 592 | my $indent = ( $selected_text =~ /(^\s+)/ ) ? $1 : ''; | ||
| 593 | $selected_text =~ s/^/\t/gm; | ||
| 594 | my $line_text = | ||
| 595 | "${indent}try {$nl" | ||
| 596 | . "$selected_text$nl" | ||
| 597 | . "${indent}\tCATCH {$nl" | ||
| 598 | . "${indent}\t\twarn \"oops: \$!\";$nl" | ||
| 599 | . "${indent}\t}$nl" | ||
| 600 | . "${indent}}$nl"; | ||
| 601 | $editor->SetSelection( $line_start, $line_end ); | ||
| 602 | $editor->ReplaceSelection($line_text); | ||
| 603 | }, | ||
| 604 | }; | ||
| 605 | |||
| 606 | } | ||
| 607 | |||
| 608 | # Not really a fix but a helper: | ||
| 609 | # Converts POD6 to XHTML | ||
| 610 | push @items, { | ||
| 611 | text => Wx::gettext('Convert POD6 to XHTML'), | ||
| 612 | listener => sub { | ||
| 613 | |||
| 614 | # Convert POD6 to XHTML using App::Grok | ||
| 615 | my $text = $self->text_get; | ||
| 616 | return if not defined $text; | ||
| 617 | |||
| 618 | require File::Temp; | ||
| 619 | my $tmp_input = File::Temp->new( SUFFIX => '.p6' ); | ||
| 620 | binmode( $tmp_input, ":utf8" ); | ||
| 621 | print $tmp_input $text; | ||
| 622 | close $tmp_input or warn "cannot close $tmp_input\n"; | ||
| 623 | |||
| 624 | my $main = $editor->main; | ||
| 625 | eval { | ||
| 626 | require App::Grok; | ||
| 627 | my $grok = App::Grok->new; | ||
| 628 | my $grok_text = $grok->render_target( $tmp_input->filename, 'xhtml' ); | ||
| 629 | |||
| 630 | # create a temporary HTML file | ||
| 631 | my $tmp_output = File::Temp->new( SUFFIX => '.html' ); | ||
| 632 | $tmp_output->unlink_on_destroy(0); | ||
| 633 | print $tmp_output $grok_text; | ||
| 634 | my $filename = $tmp_output->filename; | ||
| 635 | close $tmp_output or warn "Could not close $filename"; | ||
| 636 | |||
| 637 | # try to open the HTML file | ||
| 638 | $main->setup_editor($filename); | ||
| 639 | |||
| 640 | # launch the HTML file in your default browser | ||
| 641 | require URI::file; | ||
| 642 | my $file_url = URI::file->new($filename); | ||
| 643 | Wx::LaunchDefaultBrowser($file_url); | ||
| 644 | }; | ||
| 645 | if ($@) { | ||
| 646 | Wx::MessageBox( | ||
| 647 | Wx::gettext('Operation failed!'), | ||
| 648 | Wx::gettext('Error'), | ||
| 649 | Wx::wxOK, | ||
| 650 | $main, | ||
| 651 | ); | ||
| 652 | } | ||
| 653 | }, | ||
| 654 | }; | ||
| 655 | |||
| 656 | # Not really a fix but a helper: | ||
| 657 | # Converts POD6 to Text | ||
| 658 | push @items, { | ||
| 659 | text => Wx::gettext('Convert POD6 to Text'), | ||
| 660 | listener => sub { | ||
| 661 | |||
| 662 | # Convert POD6 to Text using App::Grok | ||
| 663 | my $text = $self->text_get; | ||
| 664 | return if not defined $text; | ||
| 665 | |||
| 666 | require File::Temp; | ||
| 667 | my $tmp_input = File::Temp->new( SUFFIX => '.p6' ); | ||
| 668 | binmode( $tmp_input, ":utf8" ); | ||
| 669 | print $tmp_input $text; | ||
| 670 | close $tmp_input or warn "cannot close $tmp_input\n"; | ||
| 671 | |||
| 672 | my $main = $editor->main; | ||
| 673 | eval { | ||
| 674 | require App::Grok; | ||
| 675 | my $grok = App::Grok->new; | ||
| 676 | my $grok_text = $grok->render_target( $tmp_input->filename, 'text' ); | ||
| 677 | |||
| 678 | # create a temporary text file | ||
| 679 | my $tmp_output = File::Temp->new( SUFFIX => '.txt' ); | ||
| 680 | $tmp_output->unlink_on_destroy(0); | ||
| 681 | print $tmp_output $grok_text; | ||
| 682 | my $filename = $tmp_output->filename; | ||
| 683 | close $tmp_output or warn "Could not close $filename"; | ||
| 684 | |||
| 685 | # try to open the text file | ||
| 686 | $main->setup_editor($filename); | ||
| 687 | }; | ||
| 688 | if ($@) { | ||
| 689 | Wx::MessageBox( | ||
| 690 | Wx::gettext('Operation failed!'), | ||
| 691 | Wx::gettext('Error'), | ||
| 692 | Wx::wxOK, | ||
| 693 | $main, | ||
| 694 | ); | ||
| 695 | } | ||
| 696 | }, | ||
| 697 | }; | ||
| 698 | } | ||
| 699 | |||
| 700 | return @items; | ||
| 701 | } | ||
| 702 | |||
| 703 | 1; | ||
| 704 | |||
| 705 | |||
| 706 | |||
| 707 | =pod | ||
| 708 | |||
| 709 | =head1 NAME | ||
| 710 | |||
| 711 | Padre::Plugin::Perl6::QuickFix - Padre Perl 6 Quick Fix Provider | ||
| 712 | |||
| 713 | =head1 VERSION | ||
| 714 | |||
| 715 | version 0.71 | ||
| 716 | |||
| 717 | =head1 DESCRIPTION | ||
| 718 | |||
| 719 | Perl 6 quick fixes are implemented here | ||
| 720 | |||
| 721 | =head1 AUTHORS | ||
| 722 | |||
| 723 | =over 4 | ||
| 724 | |||
| 725 | =item * | ||
| 726 | |||
| 727 | Ahmad M. Zawawi <ahmad.zawawi@gmail.com> | ||
| 728 | |||
| 729 | =item * | ||
| 730 | |||
| 731 | Gabor Szabo L<http://szabgab.com/> | ||
| 732 | |||
| 733 | =back | ||
| 734 | |||
| 735 | =head1 COPYRIGHT AND LICENSE | ||
| 736 | |||
| 737 | This software is copyright (c) 2010 by Ahmad M. Zawawi. | ||
| 738 | |||
| 739 | This is free software; you can redistribute it and/or modify it under | ||
| 740 | the same terms as the Perl 5 programming language system itself. | ||
| 741 | |||
| 742 | =cut | ||
| 743 | |||
| 744 | |||
| 745 | __END__ |