File Coverage

blib/lib/File/CodeSearch/Replacer.pm
Criterion Covered Total %
statement 45 45 100.0
branch 6 6 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 61 61 100.0


line stmt bran cond sub pod time code
1             package File::CodeSearch::Replacer;
2              
3             # Created on: 2009-08-07 18:42:16
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   83136 use Moose;
  2         482834  
  2         19  
10 2     2   12980 use warnings;
  2         4  
  2         63  
11 2     2   751 use version;
  2         1888  
  2         15  
12 2     2   164 use Carp;
  2         5  
  2         148  
13 2     2   595 use English qw/ -no_match_vars /;
  2         3556  
  2         18  
14 2     2   703 use Term::ANSIColor qw/:constants/;
  2         3  
  2         1362  
15              
16             our $VERSION = version->new('0.7.6');
17              
18             extends 'File::CodeSearch::Highlighter';
19              
20             has replace_re => (
21             is => 'rw',
22             );
23             has replace => (
24             is => 'rw',
25             );
26             has all => (
27             is => 'rw',
28             isa => 'Int',
29             );
30              
31             sub make_replace_re {
32 4     4 1 1190 my ($self) = @_;
33              
34 4 100       96 return $self->replace_re if $self->replace_re;
35              
36 3         11 my $re = $self->make_regex;
37              
38             # make sure that all brackets are for non capture groups
39 3         10 $re =~ s/ (?<! \\ | \[ ) [(] (?! [?] ) /(?:/gxms;
40              
41 3         81 return $self->replace_re($re);
42             }
43              
44             sub highlight {
45 3     3 1 2592 my ($self, $string) = @_;
46 3         11 my $re = $self->make_highlight_re;
47 3         7 my $replace_re = $self->make_replace_re;
48 3         67 my $replace = $self->replace;
49 3         5 my $before = '';
50 3         4 my $after = '';
51 3         5 my $changed = '';
52              
53 3         37 my @parts = split /($re)/, $string;
54              
55 3         13 for my $i ( 0 .. @parts - 1 ) {
56 11 100       21 if ( $i % 2 ) {
57 4         96 $before .= $self->before_match . $parts[$i] . $self->after_match;
58 4         6 my $part = $parts[$i];
59 4         28 $part =~ s/$replace_re/$replace/;
60 4         100 $after .= $self->before_match . $part . $self->after_match;
61 4         9 $changed .= $part;
62             }
63             else {
64 7         189 $before .= $self->before_nomatch . $parts[$i] . $self->after_nomatch;
65 7         166 $after .= $self->before_nomatch . $parts[$i] . $self->after_nomatch;
66 7         14 $changed .= $parts[$i];
67             }
68             }
69              
70 3 100       8 if ($string !~ /\n/xms) {
71 2         4 $before .= "\\N\n";
72 2         3 $after .= "\\N\n";
73             }
74              
75 3         13 return ( '', $before, $after, $changed );
76             }
77              
78             1;
79              
80             __END__
81              
82             =head1 NAME
83              
84             File::CodeSearch::Replacer - Sorts out file content that should be changed.
85              
86             =head1 VERSION
87              
88             This documentation refers to File::CodeSearch::Replacer version 0.7.6.
89              
90             =head1 SYNOPSIS
91              
92             use File::CodeSearch::Replacer;
93              
94             # Brief but working code example(s) here showing the most common usage(s)
95             # This section will be as far as many users bother reading, so make it as
96             # educational and exemplary as possible.
97              
98             =head1 DESCRIPTION
99              
100             =head1 ATTRIBUTES
101              
102             =over 4
103              
104             =item C<replace_re>
105              
106             The regular expression to replace text with
107              
108             =item C<replace>
109              
110             The text to be used for replacement
111              
112             =item C<all (Int)>
113              
114             Answer yes to all questions
115              
116             =back
117              
118             =head1 SUBROUTINES/METHODS
119              
120             =head3 C<highlight ( $search, )>
121              
122             Param: C<$search> - type (detail) - description
123              
124             Return: File::CodeSearch::Replacer -
125              
126             Description:
127              
128             =head3 C<make_replace_re ( )>
129              
130             Creates the regular expression for replacing the searched for text.
131              
132             =head1 DIAGNOSTICS
133              
134             =head1 CONFIGURATION AND ENVIRONMENT
135              
136             =head1 DEPENDENCIES
137              
138             =head1 INCOMPATIBILITIES
139              
140             =head1 BUGS AND LIMITATIONS
141              
142             There are no known bugs in this module.
143              
144             Please report problems to Ivan Wills (ivan.wills@gmail.com).
145              
146             Patches are welcome.
147              
148             =head1 AUTHOR
149              
150             Ivan Wills - (ivan.wills@gmail.com)
151              
152             =head1 LICENSE AND COPYRIGHT
153              
154             Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
155             All rights reserved.
156              
157             This module is free software; you can redistribute it and/or modify it under
158             the same terms as Perl itself. See L<perlartistic>. This program is
159             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
160             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
161             PARTICULAR PURPOSE.
162              
163             =cut