Changeset 5312


Ignore:
Timestamp:
06/18/09 07:59:49 (3 years ago)
Author:
adamk
Message:

Refactoring and adding the search regex generation

File:
1 edited

Legend:

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

    r5257 r5312  
    1111  # Create the search object 
    1212  my $search = Padre::Search->new( 
    13       find_text => 'foo', 
     13      find_term => 'foo', 
    1414  );   
    1515   
    1616  # Execute the search on an editor object 
    17   $search->find_down($editor); 
     17  $search->find_next(Padre::Current->editor); 
    1818 
    1919=head2 DESCRIPTION 
     
    3636use Class::XSAccessor 
    3737getters => { 
    38     find_text    => 'find_text', 
     38    find_term    => 'find_term', 
    3939    find_case    => 'find_case', 
    4040    find_regex   => 'find_regex', 
    4141    find_reverse => 'find_reverse', 
     42    replace_term => 'replace_term', 
     43    search_regex => 'search_regex', 
    4244}; 
    4345 
     
    4547    my $class = shift; 
    4648    my $self  = bless { @_ }, $class; 
    47     unless ( defined $self->find_text ) { 
    48         die("Did not provide 'find_text' search term"); 
     49    unless ( defined $self->find_term ) { 
     50        die("Did not provide 'find_term' search term"); 
    4951    } 
    5052    unless ( defined $self->find_case ) { 
     
    5860    } 
    5961 
    60     # Generate the regex form of the search 
    61     my $term = ''; 
     62    # Escape the raw search term 
     63    my $term = $self->find_term; 
     64    if ( $self->find_regex ) { 
     65        # Escape non-trailing $ so they won't interpolate 
     66        $term =~ s/\$(?!\z)/\\\$/g; 
     67    } else { 
     68        # Escape everything 
     69        $term = quotemeta $term; 
     70    } 
     71 
     72    # Compile the regex 
     73    $self->{search_regex} = eval { 
     74        $self->find_case ? qr/$term/m : qr/$term/mi 
     75    }; 
     76    if ( $@ ) { 
     77        # The regex doesn't compile 
     78        return; 
     79    } 
    6280 
    6381    return $self; 
     
    7795 
    7896##################################################################### 
    79 # Direction Abstraction 
     97# Command Abstraction 
    8098 
    8199sub search_next { 
     
    98116 
    99117sub replace_next { 
    100     my $self   = shift; 
    101     my $editor = shift; 
    102     unless ( _INSTANCE($editor, 'Padre::Wx::Editor') ) { 
    103         die("Failed to provide editor object to replace in"); 
    104     } 
     118    my $self = shift; 
    105119 
    106120    # Replace the currently selected match 
    107     $self->replace($editor); 
     121    $self->replace(@_); 
    108122 
    109123    # Select and move to the next match 
    110124    if ( $self->config->find_reverse ) { 
    111         return $self->search_down($editor); 
    112     } else { 
    113         return $self->search_up($editor); 
     125        return $self->search_down(@_); 
     126    } else { 
     127        return $self->search_up(@_); 
    114128    } 
    115129} 
     
    117131sub replace_previous { 
    118132    my $self   = shift; 
    119     my $editor = shift; 
    120     unless ( _INSTANCE($editor, 'Padre::Wx::Editor') ) { 
    121         die("Failed to provide editor object to replace in"); 
    122     } 
    123133 
    124134    # Replace the currently selected match 
    125     $self->replace($editor); 
     135    $self->replace(@_); 
    126136 
    127137    # Select and move to the next match 
    128138    if ( $self->config->find_reverse ) { 
    129         return $self->search_up($editor); 
    130     } else { 
    131         return $self->search_down($editor); 
     139        return $self->search_up(@_); 
     140    } else { 
     141        return $self->search_down(@_); 
    132142    } 
    133143} 
     
    138148 
    139149##################################################################### 
    140 # Search Methods 
     150# Content Abstraction 
    141151 
    142152sub search_down { 
     153    my $self = shift; 
     154    if ( _INSTANCE($_[0], 'Padre::Wx::Editor') ) { 
     155        return $self->editor_search_down(@_); 
     156    } 
     157    die("Missing or invalid content object to search in"); 
     158} 
     159 
     160sub search_up { 
     161    my $self = shift; 
     162    if ( _INSTANCE($_[0], 'Padre::Wx::Editor') ) { 
     163        return $self->editor_search_up(@_); 
     164    } 
     165    die("Missing or invalid content object to search in"); 
     166} 
     167 
     168sub replace { 
     169    my $self = shift; 
     170    if ( _INSTANCE($_[0], 'Padre::Wx::Editor') ) { 
     171        return $self->editor_replace(@_); 
     172    } 
     173    die("Missing or invalid content object to search in"); 
     174} 
     175 
     176sub replace_all { 
     177    my $self = shift; 
     178    if ( _INSTANCE($_[0], 'Padre::Wx::Editor') ) { 
     179        return $self->editor_replace_all(@_); 
     180    } 
     181    die("Missing or invalid content object to search in"); 
     182} 
     183 
     184 
     185 
     186 
     187 
     188##################################################################### 
     189# Editor Interaction 
     190 
     191sub editor_search_down { 
    143192    my $self   = shift; 
    144193    my $editor = shift; 
     
    160209} 
    161210 
    162 sub search_up { 
     211sub editor_search_up { 
    163212    my $self   = shift; 
    164213    my $editor = shift; 
     
    181230} 
    182231 
    183 sub replace { 
     232sub editor_replace { 
    184233    my $self   = shift; 
    185234    my $editor = shift; 
     
    204253} 
    205254 
    206 sub replace_all { 
     255sub editor_replace_all { 
    207256    my $self   = shift; 
    208257    my $editor = shift; 
     
    236285 
    237286 
    238  
    239287##################################################################### 
    240 # The Actual Search 
     288# Core Search 
    241289 
    242290=pod 
Note: See TracChangeset for help on using the changeset viewer.