blib/lib/Text/Editor/Vip/Buffer/Plugins/FindReplace.pm

filter: Statementstmt 96.8% 185 / 191 Branchbran 88.7% 87 / 98 Conditioncond 75.3% 49 / 65 MC/DCmcdc 50.0% 14 / 28 Subroutinesub 100% 16 / 16 Podpod 100% 9 / 9 totaltotal 88.4% 360 / 407 CC 72 SCAR 27.5
CC
72
Cov
91%
ReplaceOccurenceWithinBoundaries
28.4
FindOccurenceWithinBoundaries
16.1
ReplaceOccurence
12.1
CRAP 15.6
« blib/lib/Text/Editor/Vip/Buffer/Test.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/Display.pm »
1  
2 package Text::Editor::Vip::Buffer::Plugins::FindReplace ;
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN
8 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 $VERSION = 0.01;
13 4 @ISA = qw (Exporter);
14 4 @EXPORT = qw ();
15 4 @EXPORT_OK = qw ();
16 4 %EXPORT_TAGS = ();
17 }
18  
19 4 ▶use constant CASE_SENSITIVE => 0 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use constant IGNORE_CASE => 1 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 Text::Editor::Vip::Buffer::Plugins::FindReplace- Find and replace functionality plugin for Vip::Buffer
25  
26 =head1 SYNOPSIS
27  
28 is_deeply([$buffer->FindOccurence('1', 0, 0)], [0, 5, '1'], 'Found occurence 1') ;
29 is($buffer->GetText(), $text, 'Text still the same') ;
30 is($buffer->GetSelectionText(), '', 'GetSelectionText empty') ;
31
32 #FindNextOccurence
33 $buffer->SetModificationPosition(0, 0) ;
34 is_deeply([$buffer->FindNextOccurence()], [0, 5, '1'], 'FindNextOccurence') ;
35
36 $buffer->SetModificationPosition(0, 5) ;
37 is_deeply([$buffer->FindNextOccurence()], [0, 9, '1'], 'FindNextOccurence') ;
38
39 # FindNextOccurenceForCurrentWord
40 $buffer->SetModificationPosition(0, 0) ;
41 is_deeply([$buffer->FindNextOccurenceForCurrentWord()], [1, 0, 'line'], 'FindNextOccurenceForCurrentWord') ;
42
43 $buffer->SetModificationPosition(1, 0) ;
44 is_deeply([$buffer->FindNextOccurenceForCurrentWord()], [2, 0, 'line'], 'FindNextOccurenceForCurrentWord') ;
45
46 $buffer->SetModificationPosition(4, 0) ;
47
48 is_deeply([$buffer->FindNextOccurenceForCurrentWord()], [undef, undef, undef], 'FindNextOccurenceForCurrentWord') ;
49
50 # Regex search
51 is_deeply([$buffer->FindOccurence(qr/..n[a-z]/, 0, 0)], [0, 0, 'line'], 'Found occurence with regex') ;
52  
53 =head1 DESCRIPTION
54  
55 Find and replace functionality plugin for Vip::Buffer
56  
57 =head1 FUNCTIONS
58  
59 =cut
60  
61 #-------------------------------------------------------------------------------
62  
63 sub FindOccurence
64 80 ▶{
Subroutine FindOccurence: called CC=12 SCAR=24.8
Pod documented
65  
66 =head2 FindOccurence
67  
68 Finds the text matching the B<regex> argument starting at the B<line> and B<character> arguments.
69 If no B<line> argument is passed, the modification position is used.
70  
71 This sub returns an array containing: ($match_line, $match_position, $match_word)
72  
73 The Selection is not modified by this sub. To set the modification at the match position:
74  
75 my ($match_line, $match_position, $match_word) = $buffer->FindOccurence($regex, $line, $character) ;
76
77 if(defined $match_line)
78 {
79 $buffer->SetModificationPosition($match_line, $match_position + length($match_word)) ;
80 $buffer->{SELECTION}->Set($match_line, $match_position,$match_line, $match_position + length($match_word)) ;
81 }
82  
83 =cut
84  
85 80 my $buffer = shift ;
86 80 my $search_regex = shift ;
87  
88 80 my $line = shift ;
89 80 ▶$line = $buffer->GetModificationLine() unless defined $line ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 5 75 80
90  
91 # weird return to make comparison function on the calling side happy
92 80 ▶return(undef, undef, undef) if($line > ($buffer->GetNumberOfLines() - 1)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line > $buffer->GetNumberOfLines - 1 1 79 80
93  
94 79 my $character = shift ;
95 79 ▶$character = $buffer->GetModificationCharacter() unless defined $character ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $character 5 74 79
96  
97 79 ▶$character = $character < 0 ? 0 : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character < 0 ? : 0 79 79
98  
99 79 my $line_length = $buffer->GetLineLength($line) ;
100 79 ▶$character = $character > $line_length ? $line_length : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character > $line_length ? : 1 78 79
101  
102 79 my ($match_line, $match_position, $match_word) ;
103  
104 79 my $start_line = $line ;
105  
106 79 ▶if(defined $search_regex && '' ne $search_regex)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $search_regex and "" ne $search_regex) 75 4 79
Condition100%3/3
defined $search_regex and "" ne $search_regex
!ll&&!rl&&r
2275
MC/DC100%2/2
defined $search_regex and "" ne $search_regex
defined $search_regex "" ne $search_regex
Truth table100%3/3
A and B
A= defined $search_regexB= "" ne $search_regex
ABresult
0X0
100
111
107 {
108 75 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'} = $search_regex ;
109  
110 75 my $text = substr($buffer->GetLineText($line), $character) ;
111
112 eval
113 75 {
114 75 ▶ if($text =~ /($search_regex)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($search_regex)/) { } 25 48 73
115 {
116 25 $match_line = $line ;
117 25 $match_position = index($text, $1) + $character ;
118 25 $match_word = $1 ;
119 }
120 else
121 {
122 48 my $number_of_lines_in_document = $buffer->GetNumberOfLines() ;
123
124 48 ▶ for(my $current_line = $line + 1 ; $current_line < $number_of_lines_in_document; $current_line++)
Branch: 1 branch
BranchTrueFalseTotal
if ($current_line < $number_of_lines_in_document) 98 13 111
125 {
126 98 $text = $buffer->GetLineText($current_line) ;
127
128 98 ▶ if($text =~ /($search_regex)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($search_regex)/) 35 63 98
129 {
130 35 $match_line = $current_line ;
131 35 $match_position = index($text, $1) ;
132 35 $match_word = $1 ;
133 35 last ;
134 }
135 }
136 }
137 } ;
138
139 75 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 73 75
140 {
141 2 $buffer->PrintError("Error in FindOccurence: $@") ;
142 2 return(undef, undef, undef) ;
143 }
144 }
145
146 77 return($match_line, $match_position, $match_word) ;
147 }
148  
149 #-------------------------------------------------------------------------------
150  
151 sub FindOccurenceWithinBoundaries
152 24 ▶{
Subroutine FindOccurenceWithinBoundaries: called CC=16 SCAR=27.8
Pod documented
153  
154 =head2 FindOccurenceWithinBoundaries
155  
156 Finds the text matching the B<regex> argument within tha passed boundaries starting at the B<line> and B<character> arguments.
157 If no B<line> argument is passed, the modification position is used.
158  
159 B<Arguments:>
160  
161 =over 2
162  
163 =item * search regex, a perl qr or a string
164  
165 =item * start_line (boundary)
166  
167 =item * start_character (boundary)
168  
169 =item * end_line (boundary)
170  
171 =item * end_character (boundary)
172  
173 =item * line, where to start looking
174  
175 =item * character, where to start looking
176  
177 =back
178  
179  
180 This sub returns an array containing: ($match_line, $match_character, $match_word)
181  
182 The Selection an the current modification position are not modified by this sub.
183  
184 $buffer->FindOccurenceWithinBoundaries('line', @boundaries) ;
185  
186 =cut
187  
188 my
189 (
190 24 $buffer, $search_regex
191 , $start_line, $start_character, $end_line, $end_character
192 , $line, $character
193 ) = @_ ;
194  
195 24 ▶unless
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line > 0 and $start_line < $end_line and $start_character > 0) and $end_line < $buffer->GetNumberOfLines) 8 16 24
Condition85%18/21
defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line > 0 and $start_line < $end_line and $start_character > 0) and $end_line < $buffer->GetNumberOfLines
!ll&&!rl&&r
6216
defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line > 0 and $start_line < $end_line and $start_character > 0)
!ll&&!rl&&r
2418
defined $start_line and defined $start_character and defined $end_line and defined $end_character
!ll&&!rl&&r
2022
defined $start_line and defined $start_character and defined $end_line
!ll&&!rl&&r
2022
defined $start_line and defined $start_character
!ll&&!rl&&r
2022
$start_line > 0 and $start_line < $end_line and $start_character > 0
!ll&&!rl&&r
3118
$start_line > 0 and $start_line < $end_line
!ll&&!rl&&r
1219
MC/DC62%5/8
defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line > 0 and $start_line < $end_line and $start_character > 0) and $end_line < $buffer->GetNumberOfLines
defined $start_line defined $start_character defined $end_line defined $end_character $start_line > 0 $start_line < $end_line $start_character > 0 $end_line < $buffer->GetNumberOfLines
Truth table66%6/9
A and B and C and D and E and F and G and H
A= defined $start_lineB= defined $start_characterC= defined $end_lineD= defined $end_characterE= $start_line > 0F= $start_line < $end_lineG= $start_character > 0H= $end_line < $buffer->GetNumberOfLines
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
196 (
197 (defined $start_line && defined $start_character && defined $end_line && defined $end_character)
198 && ($start_line > 0 && $start_line < $end_line && $start_character > 0)
199 && $end_line < $buffer->GetNumberOfLines()
200 )
201 {
202 8 $buffer->PrintError("Invalid boundaries passed to ReplaceOccurenceWithinBoundaries") ;
203 0 return(undef) ;
204 }
205  
206 16 my $selection = new Text::Editor::Vip::Selection() ;
207 16 $selection->Set($start_line, $start_character, $end_line, $end_character) ;
208  
209 16 ▶unless(defined $line)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 7 9 16
210 {
211 7 $line = $buffer->GetModificationLine() ;
212 7 $character = 0 ;
213 }
214
215 16 ▶$character = $buffer->GetModificationCharacter() unless defined $character ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $character 2 14 16
216  
217 16 ▶unless($selection->IsCharacterSelected($line, $character))
Branch: 1 branch
BranchTrueFalseTotal
unless ($selection->IsCharacterSelected($line, $character)) 11 5 16
218 {
219 11 ($line, $character) = ($start_line, $start_character) ;
220 }
221
222 16 ▶$character = $character < 0 ? 0 : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character < 0 ? : 1 15 16
223  
224 16 my $line_length = $buffer->GetLineLength($line) ;
225 16 ▶$character = $character > $line_length ? $line_length : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character > $line_length ? : 1 15 16
226  
227 16 my ($match_line, $match_character, $match_word) = $buffer->FindOccurence($search_regex, $line, $character) ;
228  
229 16 ▶if(defined $match_line)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_line) { } 13 3 16
230 {
231 13 ▶ if($selection->IsCharacterSelected($match_line, $match_character))
Branch: 1 branch
BranchTrueFalseTotal
if ($selection->IsCharacterSelected($match_line, $match_character)) { } 12 1 13
232 {
233 12 return($match_line, $match_character, $match_word) ;
234 }
235 else
236 {
237 1 return(undef) ;
238 }
239 }
240 else
241 {
242 3 return(undef) ;
243 }
244 }
245  
246 #----------------------------------------------------------------------------------------------------
247  
248 sub FindNextOccurence
249 5 ▶{
Subroutine FindNextOccurence: called CC=2 SCAR=7.0
Pod documented
250  
251 =head2 FindNextOccurence
252  
253 Find the next occurence matching the search regex.
254  
255 =cut
256  
257 5 my $buffer = shift ;
258 5 my $line = $buffer->GetModificationLine();
259  
260 # weird return to make comparison function on the calling side happy
261 5 ▶return(undef, undef, undef) if($line > ($buffer->GetNumberOfLines() - 1)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line > $buffer->GetNumberOfLines - 1 0 5 5
262  
263 5 my $character = $buffer->GetModificationCharacter() ;
264  
265 $buffer->FindOccurence
266 (
267 5 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'}
268 , $line
269 , $character + 1
270 ) ;
271 }
272  
273 #-------------------------------------------------------------------------------
274  
275 sub FindNextOccurenceForCurrentWord
276 3 ▶{
Subroutine FindNextOccurenceForCurrentWord: called CC=1 SCAR=0.0
Pod documented
277  
278 =head2 FindNextOccurenceForCurrentWord
279  
280 Finds the next occurence for the word at the modification position.
281  
282 =cut
283  
284 3 my $buffer = shift ;
285 3 $buffer->ExpandedWithOrLoad('GetCurrentWord', 'Text::Editor::Vip::Buffer::Plugins::GetWord') ;
286  
287 3 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'} = $buffer->GetCurrentWord() ;
288  
289 3 $buffer->FindNextOccurence() ;
290 }
291  
292 #-------------------------------------------------------------------------------
293  
294 sub FindOccurenceBackwards
295 23 ▶{
Subroutine FindOccurenceBackwards: called CC=12 SCAR=24.9
Pod documented
296  
297 =head2 FindOccurenceBackwards
298  
299 Searches for the B<regex> going backwards in the buffer. Intricate regexes might not work.
300  
301 =cut
302  
303 23 my $buffer = shift ;
304 23 my $search_regex = shift ;
305  
306 23 my $line = shift ;
307 23 ▶$line = $buffer->GetModificationLine() unless defined $line ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 9 14 23
308  
309 # allow search backwards from after the buffer
310 23 ▶if($line > ($buffer->GetNumberOfLines() - 1))
Branch: 1 branch
BranchTrueFalseTotal
if ($line > $buffer->GetNumberOfLines - 1) 1 22 23
311 {
312 1 $line = $buffer->GetNumberOfLines() - 1 ;
313 }
314  
315 23 my $character = shift ;
316 23 ▶$character = $buffer->GetModificationCharacter() unless defined $character ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $character 9 14 23
317  
318 23 ▶$character = $character < 0 ? 0 : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character < 0 ? : 0 23 23
319  
320 23 my $line_length = $buffer->GetLineLength($line) ;
321 23 ▶$character = $character > $line_length ? $line_length : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character > $line_length ? : 1 22 23
322  
323 23 my($match_line, $match_position, $match_word) ;
324 23 my $start_line = $line ;
325  
326 23 ▶if(defined $search_regex && '' ne $search_regex)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $search_regex and "" ne $search_regex) 21 2 23
Condition100%3/3
defined $search_regex and "" ne $search_regex
!ll&&!rl&&r
1121
MC/DC100%2/2
defined $search_regex and "" ne $search_regex
defined $search_regex "" ne $search_regex
Truth table100%3/3
A and B
A= defined $search_regexB= "" ne $search_regex
ABresult
0X0
100
111
327 {
328 21 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'} = $search_regex ;
329
330 21 my ($extended_pattern) = $search_regex =~ /^(\(\?[^)]*\))/ ;
331 21 ▶ $extended_pattern ||= '' ;
Condition50%1/2
$extended_pattern ||= ""
l!l
021
MC/DC0%0/1
$extended_pattern ||= ""
$extended_pattern
Truth table50%1/2
A
A= $extended_pattern
Aresult
11
00
332
333 21 $search_regex =~ s/^(\(\?[^)]*\))// ;
334
335 21 $search_regex = $extended_pattern . reverse $search_regex ;
336
337 21 my $text = reverse substr($buffer->GetLineText($line), 0, $character) ;
338
339 21 ▶ if($text =~ /($search_regex)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($search_regex)/) { } 10 11 21
340 {
341 10 $match_line = $line ;
342 10 $match_position = $character - (length($1) +index($text, $1)) ;
343 10 $match_word = reverse($1) ;
344 }
345 else
346 {
347 11 ▶ for(my $current_line = $line - 1 ; $current_line >= 0; $current_line--)
Branch: 1 branch
BranchTrueFalseTotal
if ($current_line >= 0) 19 5 24
348 {
349 19 $text = reverse $buffer->GetLineText($current_line) ;
350
351 19 ▶ if($text =~ /($search_regex)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($search_regex)/) 6 13 19
352 {
353 6 $match_line = $current_line ;
354 6 $match_position = length($text) - (length($1) + index($text, $1)) ;
355 6 $match_word = reverse($1) ;
356 6 last ;
357 }
358 }
359 }
360 }
361  
362 23 return($match_line, $match_position, $match_word) ;
363 }
364  
365 #-------------------------------------------------------------------------------
366  
367 sub FindPreviousOccurence
368 3 ▶{
Subroutine FindPreviousOccurence: called CC=1 SCAR=0.0
Pod documented
369  
370 =head2 FindPreviousOccurence
371  
372 Searches for the next occurence going backwards in the buffer
373  
374 =cut
375  
376 3 my $buffer = shift ;
377  
378 3 my $line = $buffer->GetModificationLine();
379 3 my $character = $buffer->GetModificationCharacter() ;
380  
381 $buffer->FindOccurenceBackwards
382 (
383 3 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'}
384 , $line
385 , $character - 1
386 ) ;
387 }
388  
389 #-------------------------------------------------------------------------------
390  
391 sub FindPreviousOccurenceForCurrentWord
392 3 ▶{
Subroutine FindPreviousOccurenceForCurrentWord: called CC=1 SCAR=0.0
Pod documented
393  
394 =head2 FindPreviousOccurenceForCurrentWord
395  
396 Finds the previous occurence for the word at the modification position.
397  
398 =cut
399  
400 3 my $buffer = shift ;
401  
402 3 $buffer->ExpandedWithOrLoad('GetCurrentWord', 'Text::Editor::Vip::Buffer::Plugins::GetWord') ;
403  
404 3 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'} = $buffer->GetCurrentWord() ;
405 3 $buffer->FindOccurenceBackwards($buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'}) ;
406 }
407  
408 #-------------------------------------------------------------------------------
409  
410 sub ReplaceOccurenceWithinBoundaries
411 16 ▶{
Subroutine ReplaceOccurenceWithinBoundaries: called CC=23 SCAR=33.5
Pod documented
412  
413 =head2 ReplaceOccurenceWithinBoundaries
414  
415 Finds a match for the B<search_regex> argument and replaces it with the B<replacement>.
416  
417 B<Arguments:>
418  
419 =over 2
420  
421 =item * search regex, a perl qr or a string
422  
423 =item * replacement, a regex, a string or a sub reference (see bellow)
424  
425 =item * start_line (boundary)
426  
427 =item * start_character (boundary)
428  
429 =item * end_line (boundary)
430  
431 =item * end_character (boundary)
432  
433 =item * line, where to start looking, optional
434  
435 =item * character, where to start looking, optional
436  
437 =back
438  
439 If line or character are undefined or invalid, the current modification position is used.
440  
441 Valid boudaries must be passed to this sub or it will return undef. No replacement is done if the searched regex doesn't match
442 within the boudaries.
443  
444 This sub returns an array containing: ($match_line, $match_position, $match_word, $replacement)
445  
446 =head3 String replacement
447  
448 $buffer->ReplaceOccurence(qr/..(n[a-z])/, 'replacement') ;
449  
450 =head3 Regex replacement
451  
452 if the replacement is a regex, parenthesis can be be used to assign $1, $2, ..
453  
454 $buffer->ReplaceOccurence(qr/..(n[a-z])/, 'xx$1', @boundaries) ;
455
456 =head3 Sub replacement
457  
458 B<Arguments:>
459  
460 =over 2
461  
462 =item * the buffer
463  
464 =item * matching line
465  
466 =item * matching character
467  
468 =item * the match
469  
470 =back
471  
472 the sub returns a string which will be be inserted at the matching position.
473  
474 sub replacement_sub
475 {
476 my($buffer, $match_line, $match_character, $match_word) = @_ ;
477
478 join(', ', ref($buffer), $match_line, $match_character, $match_word, "\n") ;
479 }
480
481 $buffer->ReplaceOccurenceWithinBoundaries('match_this', \&sub_reference, @boundaries)
482  
483 =cut
484  
485 my
486 (
487 16 $buffer
488 , $search_regex, $replacement_regex
489 , $start_line, $start_character, $end_line, $end_character
490 , $line, $character
491 ) = @_ ;
492  
493 16 ▶unless
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line >= 0 and $start_line <= $end_line and $start_character >= 0) and $end_line < $buffer->GetNumberOfLines and $end_character >= 0) 8 8 16
Condition62%15/24
defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line >= 0 and $start_line <= $end_line and $start_character >= 0) and $end_line < $buffer->GetNumberOfLines and $end_character >= 0
!ll&&!rl&&r
808
defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line >= 0 and $start_line <= $end_line and $start_character >= 0) and $end_line < $buffer->GetNumberOfLines
!ll&&!rl&&r
808
defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line >= 0 and $start_line <= $end_line and $start_character >= 0)
!ll&&!rl&&r
808
defined $start_line and defined $start_character and defined $end_line and defined $end_character
!ll&&!rl&&r
268
defined $start_line and defined $start_character and defined $end_line
!ll&&!rl&&r
2014
defined $start_line and defined $start_character
!ll&&!rl&&r
2014
$start_line >= 0 and $start_line <= $end_line and $start_character >= 0
!ll&&!rl&&r
008
$start_line >= 0 and $start_line <= $end_line
!ll&&!rl&&r
008
MC/DC22%2/9
defined $start_line and defined $start_character and defined $end_line and defined $end_character and ($start_line >= 0 and $start_line <= $end_line and $start_character >= 0) and $end_line < $buffer->GetNumberOfLines and $end_character >= 0
defined $start_line defined $start_character defined $end_line defined $end_character $start_line >= 0 $start_line <= $end_line $start_character >= 0 $end_line < $buffer->GetNumberOfLines $end_character >= 0
Truth table30%3/10
A and B and C and D and E and F and G and H and I
A= defined $start_lineB= defined $start_characterC= defined $end_lineD= defined $end_characterE= $start_line >= 0F= $start_line <= $end_lineG= $start_character >= 0H= $end_line < $buffer->GetNumberOfLinesI= $end_character >= 0
ABCDEFGHIresult
0XXXXXXXX0
10XXXXXXX0
110XXXXXX0
1110XXXXX0
11110XXXX0
111110XXX0
1111110XX0
11111110X0
1111111100
1111111111
494 (
495 (defined $start_line && defined $start_character && defined $end_line && defined $end_character)
496 && ($start_line >= 0 && $start_line <= $end_line && $start_character >= 0)
497 && $end_line < $buffer->GetNumberOfLines()
498 && $end_character >= 0
499 )
500 {
501 8 $buffer->PrintError("Invalid boundaries passed to ReplaceOccurenceWithinBoundaries") ;
502 0 return(undef) ;
503 }
504  
505 8 my $selection = new Text::Editor::Vip::Selection() ;
506 8 $selection->Set($start_line, $start_character, $end_line, $end_character) ;
507  
508 # verify the input data
509 8 ▶$line = $buffer->GetModificationLine() unless defined $line ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 8 0 8
510  
511 8 my $line_length ;
512 8 ▶if($line > ($buffer->GetNumberOfLines() - 1))
Branch: 1 branch
BranchTrueFalseTotal
if ($line > $buffer->GetNumberOfLines - 1) { } 0 8 8
513 {
514 0 $line_length = 0 ;
515 }
516 else
517 {
518 8 $line_length = $buffer->GetLineLength($line) ;
519 }
520
521 8 ▶$character = $buffer->GetModificationCharacter() unless defined $character ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $character 8 0 8
522  
523 8 ▶$character = $character < 0 ? 0 : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character < 0 ? : 0 8 8
524 8 ▶$character = $character > $line_length ? $line_length : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character > $line_length ? : 0 8 8
525  
526 8 ▶unless($selection->IsCharacterSelected($line, $character))
Branch: 1 branch
BranchTrueFalseTotal
unless ($selection->IsCharacterSelected($line, $character)) 7 1 8
527 {
528 7 ($line, $character) = $selection->GetBoundaries() ;
529 }
530  
531 8 ▶if(defined $search_regex && '' ne $search_regex && defined $replacement_regex)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $search_regex and '' ne $search_regex and defined $replacement_regex) { } 6 2 8
Condition83%5/6
defined $search_regex and "" ne $search_regex and defined $replacement_regex
!ll&&!rl&&r
206
defined $search_regex and "" ne $search_regex
!ll&&!rl&&r
116
MC/DC66%2/3
defined $search_regex and "" ne $search_regex and defined $replacement_regex
defined $search_regex "" ne $search_regex defined $replacement_regex
Truth table75%3/4
A and B and C
A= defined $search_regexB= "" ne $search_regexC= defined $replacement_regex
ABCresult
0XX0
10X0
1100
1111
532 {
533 6 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'} = $search_regex ;
534 6 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::REPLACEMENT_REGEX'} = $replacement_regex ;
535
536 6 my ($match_line, $match_character, $match_word) = $buffer->FindOccurence($search_regex, $line, $character) ;
537
538 6 ▶ if(defined $match_line)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_line) { } 5 1 6
539 {
540 5 ▶ if($selection->IsCharacterSelected($match_line, $match_character))
Branch: 1 branch
BranchTrueFalseTotal
if ($selection->IsCharacterSelected($match_line, $match_character)) { } 4 1 5
541 {
542 4 $buffer->SetModificationPosition($match_line, $match_character + length($match_word)) ;
543 4 $buffer->SetSelectionBoundaries($match_line, $match_character, $match_line, $match_character + length($match_word)) ;
544
545 # perl will display a warning if the replacement is undef
546 # this can happend if '$2' is given as a replacement but nothing matches for it.
547
548 4 my $replaced_by = $match_word ;
549
550 4 ▶ if('CODE' eq ref $replacement_regex)
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $replacement_regex) { } 1 3 4
551 {
552 eval
553 1 {
554 1 $replaced_by = $replacement_regex->($buffer, $match_line, $match_character, $match_word) ;
555 } ;
556 }
557 else
558 {
559 3 eval "#line " . __LINE__ . "'" . __FILE__ . "'\n\$replaced_by =~ s/$search_regex/$replacement_regex/ ;" ;
560 }
561
562 4 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 4 4
563 {
564 0 $buffer->PrintError("Error in ReplaceOccurenceWithinBoundaries: $@") ;
565 0 return(undef, undef, undef) ;
566 }
567 else
568 {
569 4 $buffer->Delete() ;
570 4 $buffer->Insert($replaced_by) ;
571 4 return($match_line, $match_character, $match_word, $replaced_by) ;
572 }
573 }
574 else
575 {
576 # found something but outside selection
577 1 return(undef) ;
578 }
579 }
580 else
581 {
582 1 return(undef) ;
583 }
584 }
585 else
586 {
587 2 return(undef) ;
588 }
589 }
590  
591 #-------------------------------------------------------------------------------------------------------------
592  
593 sub ReplaceOccurence
594 42 ▶{
Subroutine ReplaceOccurence: called CC=12 SCAR=25.0
Pod documented
595  
596 =head2 ReplaceOccurence
597  
598 Finds a match for the B<search_regex> argument and replaces it with the B<replacement>.
599  
600 B<Arguments:>
601  
602 =over 2
603  
604 =item * search regex, a perl qr or a string
605  
606 =item * replacement, a regex, string or sub reference
607  
608 =item * line, where to start looking, optional
609  
610 =item * character, where to start looking, optional
611  
612 =back
613  
614 If line or character are undefined or invalid, the current modification position is used.
615  
616 This sub returns an array containing: ($match_line, $match_position, $match_word, $replacement)
617  
618 =head3 String replacement
619  
620 $buffer->ReplaceOccurence(qr/..(n[a-z])/, 'replacement') ;
621  
622 =head3 Regex replacement
623  
624 $buffer->ReplaceOccurence(qr/..(n[a-z])/, 'xx$1') ;
625
626 if the replacement is a regex, parenthesis can be be used to assign $1, $2, ..
627  
628 $buffer->ReplaceOccurence(qr/..(n[a-z])/, 'xx$1') ;
629
630 =head3 Sub replacement
631  
632 B<Arguments:>
633  
634 =over 2
635  
636 =item * the buffer
637  
638 =item * matching line
639  
640 =item * matching character
641  
642 =item * the match
643  
644 =back
645  
646 the sub returns a string which will be be inserted at the matching position.
647  
648 sub replacement_sub
649 {
650 my($buffer, $match_line, $match_character, $match_word) = @_ ;
651
652 join(', ', ref($buffer), $match_line, $match_character, $match_word, "\n") ;
653 }
654
655 $buffer->ReplaceOccurenceWithinBoundaries('match_this', \&sub_reference)
656  
657 =cut
658  
659 42 my ($buffer, $search_regex, $replacement_regex, $line, $character) = @_;
660  
661 # verify input
662 42 ▶$line = $buffer->GetModificationLine() unless defined $line ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 37 5 42
663  
664 42 ▶if($line > ($buffer->GetNumberOfLines() - 1))
Branch: 1 branch
BranchTrueFalseTotal
if ($line > $buffer->GetNumberOfLines - 1) 0 42 42
665 {
666 0 $line = $buffer->GetNumberOfLines() - 1 ;
667 }
668  
669 42 ▶$character = $buffer->GetModificationCharacter() unless defined $character ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $character 37 5 42
670  
671 42 ▶$character = $character < 0 ? 0 : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character < 0 ? : 0 42 42
672  
673 42 my $line_length = $buffer->GetLineLength($line) ;
674 42 ▶$character = $character > $line_length ? $line_length : $character ;
Branch: 1 branch
BranchTrueFalseTotal
$character > $line_length ? : 2 40 42
675  
676 42 my ($match_line, $match_character, $match_word, $replaced_by) ;
677  
678 42 ▶if(defined $search_regex && '' ne $search_regex && defined $replacement_regex)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $search_regex and "" ne $search_regex and defined $replacement_regex) 36 6 42
Condition66%4/6
defined $search_regex and "" ne $search_regex and defined $replacement_regex
!ll&&!rl&&r
6036
defined $search_regex and "" ne $search_regex
!ll&&!rl&&r
6036
MC/DC33%1/3
defined $search_regex and "" ne $search_regex and defined $replacement_regex
defined $search_regex "" ne $search_regex defined $replacement_regex
Truth table50%2/4
A and B and C
A= defined $search_regexB= "" ne $search_regexC= defined $replacement_regex
ABCresult
0XX0
10X0
1100
1111
679 {
680 36 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::SEARCH_REGEX'} = $search_regex ;
681 36 $buffer->{'Text::Editor::Vip::Buffer::Plugins::FindReplace::REPLACEMENT_REGEX'} = $replacement_regex ;
682
683 36 ($match_line, $match_character, $match_word) = $buffer->FindOccurence($search_regex, $line, $character) ;
684
685 36 ▶ if(defined $match_line)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_line) 29 7 36
686 {
687 29 $buffer->SetModificationPosition($match_line, $match_character + length($match_word)) ;
688 29 $buffer->SetSelectionBoundaries($match_line, $match_character, $match_line, $match_character + length($match_word)) ;
689
690 # perl will display a warning if the replacement is undef
691 # this can happend if '$2' is given as a replacement but nothing matches for it.
692
693 29 $replaced_by = $match_word ;
694
695 29 ▶ if('CODE' eq ref $replacement_regex)
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $replacement_regex) { } 6 23 29
696 {
697 eval
698 6 {
699 6 $replaced_by = $replacement_regex->($buffer, $match_line, $match_character, $match_word) ;
700 } ;
701 }
702 else
703 {
704 23 eval "#line " . __LINE__ . "'" . __FILE__ . "'\n\$replaced_by =~ s/$search_regex/$replacement_regex/ ;" ;
705 }
706
707 29 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 28 29
708 {
709 1 $buffer->PrintError("Error in ReplaceOccurence: $@") ;
710 1 return(undef, undef, undef) ;
711 }
712 else
713 {
714 28 $buffer->Delete() ;
715 28 $buffer->Insert($replaced_by) ;
716 }
717 }
718 }
719  
720 41 return($match_line, $match_character, $match_word, $replaced_by) ;
721 }
722  
723 #-------------------------------------------------------------------------------
724  
725 4 1 ;
726  
727 =head1 AUTHOR
728  
729 Khemir Nadim ibn Hamouda
730 CPAN ID: NKH
731 mailto:nadim@khemir.net
732 http:// no web site
733  
734 =head1 COPYRIGHT
735  
736 This program is free software; you can redistribute
737 it and/or modify it under the same terms as Perl itself.
738  
739 The full text of the license can be found in the
740 LICENSE file included with this module.
741  
742 =cut
« blib/lib/Text/Editor/Vip/Buffer/Test.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/Display.pm »