Changeset 11106


Ignore:
Timestamp:
03/13/10 13:31:35 (2 years ago)
Author:
karl.forner
Message:

added the find_matching_brace method and implemented brace expression highlighting like xemacs paren highlighting expression mode.
You have to enable it via the editor_brace_expression_highlighting setting.

Location:
trunk/Padre/lib/Padre
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Padre/lib/Padre/Config.pm

    r11057 r11106  
    673673    default => 0, 
    674674); 
     675 
     676setting( 
     677    name    => 'editor_brace_expression_highlighting', 
     678    type    => Padre::Constant::BOOLEAN, 
     679    store   => Padre::Constant::HUMAN, 
     680    default => 0, 
     681); 
     682 
    675683setting( 
    676684    name    => 'save_autoclean', 
  • trunk/Padre/lib/Padre/Wx/Editor.pm

    r11105 r11106  
    504504# 
    505505 
     506sub apply_style { 
     507    my ($self, $style_info) = @_; 
     508    my %previous_style = %$style_info; 
     509    $previous_style{style} = $self->GetStyleAt($style_info->{start}); 
     510     
     511    $self->StartStyling( $style_info->{start}, 0xFF ); 
     512    $self->SetStyling( $style_info->{len}, $style_info->{style} ); 
     513     
     514    return \%previous_style; 
     515} 
     516 
     517 
     518my $previous_expr_hiliting_style; 
    506519sub highlight_braces { 
    507520    my ($self) = @_; 
    508521     
     522    my $expression_highlighting = $self->get_config->editor_brace_expression_highlighting; 
     523     
    509524    # remove current highlighting if any 
    510525    $self->BraceHighlight( $STC_INVALID_POSITION, $STC_INVALID_POSITION  ); 
     526    if ($previous_expr_hiliting_style) { 
     527        $self->apply_style($previous_expr_hiliting_style); 
     528        $previous_expr_hiliting_style = undef; 
     529    } 
    511530     
    512531    my $pos1 = $self->GetCurrentPos; 
     
    520539 
    521540    $self->BraceHighlight( $actual_pos1, $actual_pos2 ); 
    522  
    523     return; 
    524 } 
    525  
    526 =head2 goto_matching_brace 
    527  
    528 Move the cursor to the matching brace if any. If the cursor is inside the braces the destination  
     541     
     542    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); 
     547    } 
     548 
     549 
     550    return; 
     551} 
     552 
     553 
     554=head2 find_matching_brace 
     555 
     556Find the position of to the matching brace if any. If the cursor is inside the braces the destination  
    529557will be inside too, same it is outside. 
    530558 
     
    532560        pos - the cursor position in the editor [defaults to cursor position) : int 
    533561         
    534  
     562    Return: 
     563        matching_pos - the matching position, or undef if none 
    535564 
    536565=cut 
    537 sub goto_matching_brace { 
     566sub find_matching_brace { 
    538567    my ($self, $pos) = @_; 
    539568    $pos = $self->GetCurrentPos unless defined $pos; 
     
    543572    my $actual_pos2 = $self->BraceMatch($actual_pos1); 
    544573    return if $actual_pos2 == $STC_INVALID_POSITION; 
    545      
    546     # several cases: 
    547     my $pos2 = $actual_pos2; 
    548     $pos2++ if $is_after; # ensure is stays inside if origin is inside, same four outside 
    549  
    550     $self->GotoPos($pos2); 
    551 } 
    552  
    553 =head2 select_to_matching_brace 
    554  
    555 Select to the matching opening or closing brace. If the cursor is inside the braces the destination  
     574    $actual_pos2++ if $is_after; # ensure is stays inside if origin is inside, same four outside 
     575    return $actual_pos2; 
     576} 
     577 
     578 
     579=head2 goto_matching_brace 
     580 
     581Move the cursor to the matching brace if any. If the cursor is inside the braces the destination  
    556582will be inside too, same it is outside. 
    557583 
     
    560586         
    561587 
     588=cut 
     589sub goto_matching_brace { 
     590    my ($self, $pos) = @_; 
     591    my $pos2  = $self->find_matching_brace($pos) or return; 
     592    $self->GotoPos($pos2); 
     593} 
     594 
     595=head2 select_to_matching_brace 
     596 
     597Select to the matching opening or closing brace. If the cursor is inside the braces the destination  
     598will be inside too, same it is outside. 
     599 
     600    Params: 
     601        pos - the cursor position in the editor [defaults to cursor position) : int 
     602         
     603 
    562604 
    563605=cut 
     
    566608    my ($self, $pos) = @_; 
    567609    $pos = $self->GetCurrentPos unless defined $pos; 
    568  
    569  
    570     my $info1 = $self->get_brace_info($pos) or return; 
    571     my ($actual_pos1, $brace, $is_after, $is_opening) = @$info1; 
    572      
    573     my $actual_pos2 = $self->BraceMatch($actual_pos1); 
    574     return if $actual_pos2 == $STC_INVALID_POSITION; 
    575      
    576     # several cases: 
    577     my $pos2 = $actual_pos2; 
    578     $pos2++ if $is_after; # ensure is stays inside if origin is inside, same four outside 
    579  
    580     my $start = $is_opening ? $self->GetSelectionStart() : $self->GetSelectionEnd(); 
     610    my $pos2  = $self->find_matching_brace($pos) or return; 
     611    my $start = ($pos < $pos2) ? $self->GetSelectionStart() : $self->GetSelectionEnd(); 
    581612    $self->SetSelection($start, $pos2); 
    582613         
     
    10211052        foreach my $style ( @{ $self->{styles} } ) { 
    10221053            $self->StartStyling( $style->{start}, 0xFF ); 
    1023             $self->SetStyling( $style->{len}, 32 ); 
     1054            $self->SetStyling( $style->{len}, Wx::wxSTC_STYLE_DEFAULT ); 
    10241055        } 
    10251056    } 
Note: See TracChangeset for help on using the changeset viewer.