blib/lib/Text/Editor/Vip/Buffer/Selection.pm

filter: Statementstmt 91.6% 109 / 119 Branchbran 88.0% 44 / 50 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 83.3% 15 / 18 Podpod 87.5% 7 / 8 totaltotal 89.2% 183 / 205 CC 29 SCAR 22.3
CC
29
Cov
90%
RunSubOnSelection
18.0
BoxSelection
8.1
SetSelection
6.0
CRAP 9.3
« blib/lib/Text/Editor/Vip/Buffer/Plugins/Display.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/Tags.pm »
1  
2 package Text::Editor::Vip::Buffer::Selection;
3  
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Carp qw(cluck) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 BEGIN
9 18 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 $VERSION = 0.01;
13 18 @ISA = qw (Exporter);
14 18 @EXPORT = qw ();
15 18 @EXPORT_OK = qw ();
16 18 %EXPORT_TAGS = ();
17 }
18  
19 #-------------------------------------------------------------------------------
20  
21 sub GetSelection
22 68 ▶{
Subroutine GetSelection: called CC=1 SCAR=0.0
Pod documented
23  
24 =head2 GetSelection
25  
26 Returns the selection object used by the buffer.
27  
28 =cut
29  
30 68 my $buffer = shift ;
31 68 return($buffer->{SELECTION}) ;
32 }
33  
34 #-------------------------------------------------------------------------------
35  
36 sub SetSelection
37 0 ▶{
Subroutine SetSelection: not called CC=2 SCAR=17.9
Pod documented
38  
39 =head2 SetSelection
40  
41 Sets the selection object passed as argument to use by the buffer
42  
43 =cut
44  
45 0 my $buffer = shift ;
46 0 ▶my $new_selection = shift or die ;
Branch: 1 branch
BranchTrueFalseTotal
my $new_selection = shift() or die 0 0 0
47  
48 my
49 (
50 0 $new_selection_start_line, $new_selection_start_character
51 , $new_selection_end_line, $new_selection_end_character
52 ) = $new_selection->GetBoundaries() ;
53
54 my
55 (
56 0 $selection_start_line, $selection_start_character
57 , $selection_end_line, $selection_end_character
58 ) = $buffer->GetBoundaries() ;
59  
60  
61 0 $buffer->PushUndoStep
62 (
63 "\$buffer->SetSelectionBoundaries($new_selection_start_line, $new_selection_start_character, $new_selection_end_line, $new_selection_end_character) ;"
64 , "\$buffer->SetSelectionBoundaries($selection_start_line, $selection_start_character, $selection_end_line, $selection_end_character) ;"
65 ) ;
66  
67 0 $buffer->{SELECTION} = $new_selection ;
68 }
69  
70 #-------------------------------------------------------------------------------
71  
72 sub GetSelectionBoundaries
73 583 ▶{
Subroutine GetSelectionBoundaries: called CC=1 SCAR=0.0
Pod documented
74  
75 =head2 GetSelectionBoundaries
76  
77 Returns the selection boundaries used by the buffer.
78  
79 =cut
80  
81 583 my $buffer = shift ;
82 583 return($buffer->{SELECTION}->GetBoundaries) ;
83 }
84  
85 #-------------------------------------------------------------------------------
86  
87 sub SetSelectionBoundaries
88 165 ▶{
Subroutine SetSelectionBoundaries: called CC=1 SCAR=0.0
Pod documented
89  
90 =head2 SetSelectionBoundaries
91  
92 Sets the selection boundaries use by the buffer
93  
94 =cut
95  
96 165 my $buffer = shift ;
97  
98  
99 my
100 (
101 165 $new_selection_start_line, $new_selection_start_character
102 , $new_selection_end_line, $new_selection_end_character
103 ) = @_ ;
104
105 my
106 (
107 165 $selection_start_line, $selection_start_character
108 , $selection_end_line, $selection_end_character
109 ) = $buffer->GetSelectionBoundaries() ;
110  
111  
112 165 $buffer->PushUndoStep
113 (
114 "\$buffer->SetSelectionBoundaries($new_selection_start_line, $new_selection_start_character, $new_selection_end_line, $new_selection_end_character) ;"
115 , "\$buffer->SetSelectionBoundaries($selection_start_line, $selection_start_character, $selection_end_line, $selection_end_character) ;"
116 ) ;
117  
118 $buffer->{SELECTION}->Set
119 (
120 165 $new_selection_start_line, $new_selection_start_character
121 , $new_selection_end_line, $new_selection_end_character
122 ) ;
123 }
124  
125 #-------------------------------------------------------------------------------
126  
127 sub GetSelectionText
128 8 ▶{
Subroutine GetSelectionText: called CC=2 SCAR=6.9
Pod undocumented
129  
130 =head2
131  
132 Returns the selection contents joined with "\n" except for the last line
133  
134 =cut
135  
136 8 my $buffer = shift ;
137  
138 8 my $selection_text = '' ;
139  
140 8 ▶unless($buffer->{SELECTION}->IsEmpty())
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer->{"SELECTION"}->IsEmpty) 5 3 8
141 {
142 $buffer->RunSubOnSelection
143 (
144 sub
145 12 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
146 12 my ($text, $selection_line_index, $modification_character, $original_selection, $buffer) = @_;
147
148 my
149 (
150 12 $selection_start_line, $selection_start_character
151 , $selection_end_line, $selection_end_character
152 ) = $original_selection->GetBoundaries() ;
153
154 12 ▶ if($selection_end_line == $selection_line_index)
Branch: 1 branch
BranchTrueFalseTotal
if ($selection_end_line == $selection_line_index) { } 5 7 12
155 {
156 # last line doesn't get a \n
157 5 $selection_text .= $text ;
158 }
159 else
160 {
161 7 $selection_text .= "$text\n" ;
162 }
163
164 12 return($text) ;
165 }
166
167 0 ▶ , sub { $buffer->PrintError("Mark selection please\n") ; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 5 ) ;
169 }
170
171 8 return($selection_text) ;
172 }
173  
174 #-------------------------------------------------------------------------------
175  
176 sub DeleteSelection
177 1109 ▶{
Subroutine DeleteSelection: called CC=2 SCAR=6.9
Pod documented
178  
179 =head2 DeleteSelection
180  
181 Removes the text within the selection, if any, from the buffer. Sets the modification position to the start of the selection
182  
183 =cut
184  
185 1109 my $buffer = shift ;
186  
187 1109 my $undo_block = new Text::Editor::Vip::CommandBlock($buffer, '$buffer->DeleteSelection() ;', ' #', '# undo for $buffer->DeleteSeletion()', ' ') ;
188  
189 1109 ▶unless($buffer->{SELECTION}->IsEmpty())
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer->{"SELECTION"}->IsEmpty) 66 1043 1109
190 {
191 66 my ($start_line, $start_character) = $buffer->{SELECTION}->GetBoundaries() ;
192
193 $buffer->RunSubOnSelection
194 (
195 109 ▶ sub { return(undef) ; }
Subroutine __ANON__: called CC=1 SCAR=0.0
196 0 ▶ , sub { $buffer->PrintError("Mark selection please\n") ; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 66 ) ;
198
199 66 $buffer->SetModificationPosition($start_line, $start_character) ;
200 66 $buffer->{SELECTION}->Clear() ;
201 }
202 }
203  
204 #-------------------------------------------------------------------------------
205  
206 sub RunSubOnSelection
207 104 ▶{
Subroutine RunSubOnSelection: called CC=18 SCAR=28.9
Pod documented
208  
209 =head2 RunSubOnSelection
210  
211 Runs a user supplied sub on the selection. The sub is called for each line in the selection.
212 It can return a string or undef if the section is to be removed.
213  
214 =cut
215  
216 104 my $buffer = shift ;
217 104 my ($function, $error_sub_ref) = @_ ;
218  
219 104 ▶unless($buffer->{SELECTION}->IsEmpty())
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer->{'SELECTION'}->IsEmpty) { } 3 101 104
220 {
221 101 my $undo_block = new Text::Editor::Vip::CommandBlock($buffer, '# $buffer->RunSubOnSelection() ;', ' ', '# undo for $buffer->DeleteSeletion()', ' ') ;
222
223 101 $buffer->BoxSelection() ;
224
225 my
226 (
227 $selection_start_line, $selection_start_character
228 , $selection_end_line, $selection_end_character
229 101 ) = $buffer->{SELECTION}->GetBoundaries() ;
230
231 101 my $original_selection = $buffer->{SELECTION}->Clone() ;
232
233 101 $buffer->{SELECTION}->Clear() ; # we use buffer functionw that might call this sub otherwise
234  
235 101 my $removing_end_of_first_line = 0 ;
236  
237 101 my @lines_to_delete ;
238 101 my $wrap_first_line = -1 ; # we need two confimations to wrap the first line
239  
240 101 ▶ for
Branch: 1 branch
BranchTrueFalseTotal
if ($selection_line_index <= $selection_end_line) 194 101 295
241 (
242 my $selection_line_index = $selection_start_line
243 ; $selection_line_index <= $selection_end_line
244 ; $selection_line_index++
245 )
246 {
247 # we remove the text and replace it with the text returned by the user sub
248 194 my $text ;
249 194 eval {$text = $buffer->GetLineText($selection_line_index) ;} ;
250
251 194 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 194 194
252 {
253 0 $buffer->PrintError($@) ;
254 0 last ;
255 }
256
257 194 my $modification_character ;
258 194 my $whole_line_selected = 0 ;
259
260 194 my $line_length = $buffer->GetLineLength($selection_line_index) ;
261
262 194 ▶ my $corrected_selection_start_character = $selection_start_character < $line_length ? $selection_start_character : $line_length ;
Branch: 1 branch
BranchTrueFalseTotal
$selection_start_character < $line_length ? : 173 21 194
263 194 ▶ my $corrected_selection_end_character = $selection_end_character < $line_length ? $selection_end_character : $line_length ;
Branch: 1 branch
BranchTrueFalseTotal
$selection_end_character < $line_length ? : 149 45 194
264
265 194 ▶ if($selection_line_index == $selection_start_line && $selection_start_line == $selection_end_line)
Branch: 3 branches
BranchTrueFalseTotal
if ($selection_line_index == $selection_start_line and $selection_start_line == $selection_end_line) { } 58 136 194
elsif ($selection_line_index == $selection_start_line) { } 43 93 136
elsif ($selection_line_index == $selection_end_line) { } 43 50 93
Condition100%3/3
$selection_line_index == $selection_start_line and $selection_start_line == $selection_end_line
!ll&&!rl&&r
934358
MC/DC100%2/2
$selection_line_index == $selection_start_line and $selection_start_line == $selection_end_line
$selection_line_index == $selection_start_line $selection_start_line == $selection_end_line
Truth table100%3/3
A and B
A= $selection_line_index == $selection_start_lineB= $selection_start_line == $selection_end_line
ABresult
0X0
100
111
266 {
267 58 $text = substr($text, $corrected_selection_start_character, $corrected_selection_end_character - $corrected_selection_start_character) ;
268 58 $modification_character = $selection_start_character ;
269
270 58 ▶ $whole_line_selected++ if(length($text) == $line_length && 0 == $selection_start_character) ;
Branch: 1 branch
BranchTrueFalseTotal
if length $text == $line_length and 0 == $selection_start_character 4 54 58
Condition66%2/3
length $text == $line_length and 0 == $selection_start_character
!ll&&!rl&&r
5404
MC/DC50%1/2
length $text == $line_length and 0 == $selection_start_character
length $text == $line_length 0 == $selection_start_character
Truth table66%2/3
A and B
A= length $text == $line_lengthB= 0 == $selection_start_character
ABresult
0X0
100
111
271 }
272 elsif($selection_line_index == $selection_start_line)
273 {
274 43 $text = substr($text, $corrected_selection_start_character) ;
275 43 $modification_character = $selection_start_character ;
276 43 $wrap_first_line++ ;
277 }
278 elsif($selection_line_index == $selection_end_line)
279 {
280 43 $text = substr($text, 0, $corrected_selection_end_character) ;
281 43 $modification_character = 0 ;
282 }
283 else
284 {
285 50 $modification_character = 0 ;
286 50 $whole_line_selected++ ;
287 }
288
289 # the sub has access to the line before we modify it
290 194 my $new_text = $function->($text, $selection_line_index, $modification_character, $original_selection, $buffer) ;
291
292 # we should avoid unecessary modification if the text is the same
293 #~ if(length($new_text) != length($text) || $new_text ne $text)
294
295 {
296 194 $buffer->SetModificationPosition($selection_line_index, $modification_character) ;
297 194 $buffer->Delete(length($text)) ;
298
299 194 ▶ if(defined $new_text)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_text) { } 77 117 194
300 {
301 77 $buffer->Insert($new_text) ;
302 }
303 else
304 {
305 117 ▶ if($selection_line_index == $selection_start_line)
Branch: 1 branch
BranchTrueFalseTotal
if ($selection_line_index == $selection_start_line) 70 47 117
306 {
307 70 $wrap_first_line++ ;
308 }
309
310 # deleted lines are not taken away before all lines are processed
311
312 #~ print "$selection_line_index == $selection_end_line && $selection_end_character\n" ;
313
314 117 ▶ if($whole_line_selected)
Branch: 1 branch
BranchTrueFalseTotal
if ($whole_line_selected) 32 85 117
315 {
316 # last line is never deleted
317 32 ▶ push @lines_to_delete, $selection_line_index unless ($selection_line_index == $selection_end_line) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $selection_line_index == $selection_end_line 28 4 32
318 }
319 }
320 }
321 }
322
323 101 $buffer->DeleteLine($_) for (reverse @lines_to_delete) ;
324
325 101 ▶ if($wrap_first_line == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap_first_line == 1) 19 82 101
326 {
327 19 $buffer->SetModificationPosition($selection_start_line, $selection_start_character) ;
328 19 $buffer->Delete(1) ;
329 }
330
331 $buffer->{SELECTION}->Set
332 (
333 101 $selection_start_line, $selection_start_character
334 , $selection_end_line, $selection_end_character
335 ) ;
336 }
337 else
338 {
339 3 $error_sub_ref->("No Selection!") ;
340 }
341 }
342  
343 #-------------------------------------------------------------------------------
344  
345 sub BoxSelection
346 130 ▶{
Subroutine BoxSelection: called CC=8 SCAR=20.9
Pod documented
347  
348 =head2 BoxSelection
349  
350 Puts the selection boundaries within the buffer boundaries and returns the new selection boundaries.
351  
352 Doesn't change the selection if it is empty. See L<SelectAll>.
353  
354 =cut
355  
356 130 my $buffer = shift ;
357  
358 130 ▶unless($buffer->{SELECTION}->IsEmpty())
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer->{"SELECTION"}->IsEmpty) 129 1 130
359 {
360 129 my ($selection_start_line, $selection_start_character
361 , $selection_end_line, $selection_end_character
362 ) = $buffer->GetSelectionBoundaries() ;
363
364 129 my $number_of_lines = $buffer->GetNumberOfLines() ;
365
366 129 ▶ $selection_start_line = $selection_start_line >= $number_of_lines ? ($number_of_lines - 1): $selection_start_line ;
Branch: 1 branch
BranchTrueFalseTotal
$selection_start_line >= $number_of_lines ? : 0 129 129
367 129 ▶ $selection_start_line = 0 if $selection_start_line < 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $selection_start_line < 0 16 113 129
368
369 129 ▶ $selection_start_character = 0 if $selection_start_character < 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $selection_start_character < 0 1 128 129
370
371 129 ▶ $selection_end_line = $selection_end_line >= $number_of_lines ? ($number_of_lines - 1) : $selection_end_line ;
Branch: 1 branch
BranchTrueFalseTotal
$selection_end_line >= $number_of_lines ? : 19 110 129
372 129 ▶ $selection_end_line = 0 if $selection_end_line < 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $selection_end_line < 0 0 129 129
373
374 129 ▶ $selection_end_character = 0 if $selection_end_character < 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $selection_end_character < 0 0 129 129
375
376 $buffer->{SELECTION}->Set
377 (
378 129 $selection_start_line, $selection_start_character
379 , $selection_end_line, $selection_end_character
380 ) ;
381
382 129 return($buffer->GetSelectionBoundaries()) ;
383 }
384 }
385  
386 #-------------------------------------------------------------------------------
387  
388 18 1;
389  
390 =head1 NAME
391  
392 Text::Editor::Vip::Buffer::Selection - Selection handling for buffer
393  
394 =head1 SYNOPSIS
395  
396 use Text::Editor::Vip::Buffer::Selection
397
398  
399 =head1 DESCRIPTION
400  
401 Plugin for Vip::Buffer. It handles Selection.
402  
403 =head1 USAGE
404  
405 =head1 BUGS
406  
407 =head1 SUPPORT
408  
409 =head1 AUTHOR
410  
411 Khemir Nadim ibn Hamouda
412 CPAN ID: NKH
413 mailto:nadim@khemir.net
414 http:// no web site
415  
416 =head1 COPYRIGHT
417  
418 This program is free software; you can redistribute
419 it and/or modify it under the same terms as Perl itself.
420  
421 The full text of the license can be found in the
422 LICENSE file included with this module.
423  
424 =cut
« blib/lib/Text/Editor/Vip/Buffer/Plugins/Display.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/Tags.pm »