Changeset 11120 for trunk/Padre/lib/Padre/Wx/Editor.pm
- Timestamp:
- 03/15/10 04:51:54 (2 years ago)
- File:
-
- 1 edited
-
trunk/Padre/lib/Padre/Wx/Editor.pm (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Padre/lib/Padre/Wx/Editor.pm
r11106 r11120 44 44 # Brace* methods 45 45 # always altern opening and starting braces in the constant 46 my $BRACES = '{}[]()';47 my $STC_INVALID_POSITION = Wx::wxSTC_INVALID_POSITION;46 my $BRACES = '{}[]()'; 47 my $STC_INVALID_POSITION = Wx::wxSTC_INVALID_POSITION; 48 48 49 49 my $data; … … 169 169 # return the character at a given position as a perl string 170 170 sub get_character_at { 171 my ( $self, $pos) = @_;171 my ( $self, $pos ) = @_; 172 172 return chr( $self->GetCharAt($pos) ); 173 173 } … … 424 424 425 425 426 427 428 429 426 430 427 … … 456 453 457 454 sub get_brace_info { 458 my ( $self, $pos) = @_;455 my ( $self, $pos ) = @_; 459 456 $pos = $self->GetCurrentPos unless defined $pos; 460 457 461 458 # try the after position first (default one for BraceMatch) 462 459 my $is_after = 1; 463 my $brace = $self->get_character_at($pos);460 my $brace = $self->get_character_at($pos); 464 461 my $is_brace = $self->get_brace_type($brace); 465 if ( !$is_brace && $pos > 0) { # try the before position466 $brace = $self->get_character_at(--$pos);467 $is_brace = $self->get_brace_type($brace) or return undef;462 if ( !$is_brace && $pos > 0 ) { # try the before position 463 $brace = $self->get_character_at( --$pos ); 464 $is_brace = $self->get_brace_type($brace) or return undef; 468 465 $is_after = 0; 469 466 } 470 467 my $is_opening = $is_brace % 2; # odd values are opening 471 return [ $pos, $brace, $is_after, $is_opening];468 return [ $pos, $brace, $is_after, $is_opening ]; 472 469 } 473 470 … … 485 482 486 483 =cut 484 487 485 my %_cached_braces; 486 488 487 sub get_brace_type { 489 my ( $self, $char) = @_;488 my ( $self, $char ) = @_; 490 489 unless (%_cached_braces) { 491 490 my $i = 1; # start from one so that all values are true 492 $_cached_braces{$_} = $i++ foreach ( split //, $BRACES);491 $_cached_braces{$_} = $i++ foreach ( split //, $BRACES ); 493 492 } 494 493 my $v = $_cached_braces{$char} or return 0; … … 501 500 # {} : never highlighted 502 501 # { } : always correct 503 #504 502 # 503 # 505 504 506 505 sub apply_style { 507 my ( $self, $style_info) = @_;506 my ( $self, $style_info ) = @_; 508 507 my %previous_style = %$style_info; 509 $previous_style{style} = $self->GetStyleAt( $style_info->{start});510 508 $previous_style{style} = $self->GetStyleAt( $style_info->{start} ); 509 511 510 $self->StartStyling( $style_info->{start}, 0xFF ); 512 511 $self->SetStyling( $style_info->{len}, $style_info->{style} ); 513 512 514 513 return \%previous_style; 515 514 } … … 517 516 518 517 my $previous_expr_hiliting_style; 518 519 519 sub highlight_braces { 520 520 my ($self) = @_; 521 521 522 522 my $expression_highlighting = $self->get_config->editor_brace_expression_highlighting; 523 523 524 524 # remove current highlighting if any 525 $self->BraceHighlight( $STC_INVALID_POSITION, $STC_INVALID_POSITION );525 $self->BraceHighlight( $STC_INVALID_POSITION, $STC_INVALID_POSITION ); 526 526 if ($previous_expr_hiliting_style) { 527 527 $self->apply_style($previous_expr_hiliting_style); 528 528 $previous_expr_hiliting_style = undef; 529 529 } 530 531 my $pos1 = $self->GetCurrentPos;532 my $info1 = $self->get_brace_info($pos1) or return;530 531 my $pos1 = $self->GetCurrentPos; 532 my $info1 = $self->get_brace_info($pos1) or return; 533 533 my ($actual_pos1) = @$info1; 534 534 535 535 my $actual_pos2 = $self->BraceMatch($actual_pos1); 536 # return if abs( $pos1 - $pos2 ) < 2; 536 537 # return if abs( $pos1 - $pos2 ) < 2; 537 538 538 539 return if $actual_pos2 == $STC_INVALID_POSITION; #Wx::wxSTC_INVALID_POSITION #???? 539 540 540 541 $self->BraceHighlight( $actual_pos1, $actual_pos2 ); 541 542 542 543 if ($expression_highlighting) { 543 my $pos2 = $self->find_matching_brace($pos1) or return; 544 my %style = (start => $pos1 < $pos2 ? $pos1 : $pos2, 545 len => abs($pos1-$pos2), style => Wx::wxSTC_STYLE_DEFAULT); 546 $previous_expr_hiliting_style = $self->apply_style(\%style); 544 my $pos2 = $self->find_matching_brace($pos1) or return; 545 my %style = ( 546 start => $pos1 < $pos2 ? $pos1 : $pos2, 547 len => abs( $pos1 - $pos2 ), style => Wx::wxSTC_STYLE_DEFAULT 548 ); 549 $previous_expr_hiliting_style = $self->apply_style( \%style ); 547 550 } 548 551 … … 564 567 565 568 =cut 569 566 570 sub find_matching_brace { 567 my ( $self, $pos) = @_;571 my ( $self, $pos ) = @_; 568 572 $pos = $self->GetCurrentPos unless defined $pos; 569 573 my $info1 = $self->get_brace_info($pos) or return; 570 my ( $actual_pos1, $brace, $is_after, $is_opening) = @$info1;571 574 my ( $actual_pos1, $brace, $is_after, $is_opening ) = @$info1; 575 572 576 my $actual_pos2 = $self->BraceMatch($actual_pos1); 573 577 return if $actual_pos2 == $STC_INVALID_POSITION; … … 587 591 588 592 =cut 593 589 594 sub goto_matching_brace { 590 my ( $self, $pos) = @_;591 my $pos2 = $self->find_matching_brace($pos) or return;595 my ( $self, $pos ) = @_; 596 my $pos2 = $self->find_matching_brace($pos) or return; 592 597 $self->GotoPos($pos2); 593 598 } … … 606 611 607 612 sub select_to_matching_brace { 608 my ( $self, $pos) = @_;613 my ( $self, $pos ) = @_; 609 614 $pos = $self->GetCurrentPos unless defined $pos; 610 my $pos2 = $self->find_matching_brace($pos) or return;611 my $start = ( $pos < $pos2) ? $self->GetSelectionStart() : $self->GetSelectionEnd();612 $self->SetSelection( $start, $pos2);613 614 } 615 615 my $pos2 = $self->find_matching_brace($pos) or return; 616 my $start = ( $pos < $pos2 ) ? $self->GetSelectionStart() : $self->GetSelectionEnd(); 617 $self->SetSelection( $start, $pos2 ); 618 619 } 620 616 621 # currently if there are 9 lines we set the margin to 1 width and then 617 622 # if another line is added it is not seen well.
Note: See TracChangeset
for help on using the changeset viewer.
