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

filter: Statementstmt 97.6% 84 / 86 Branchbran 87.5% 14 / 16 Conditioncond 44.4% 8 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 23 / 23 Podpod 82.3% 14 / 17 totaltotal 85.4% 147 / 172 CC 15 SCAR 10.7
CC
15
Cov
88%
IsCharacterSelected
7.0
IsLineSelected
4.7
GetBoundaries
4.0
CRAP 2.9
« blib/lib/Text/Editor/Vip/CommandBlock.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/InsertDelete.pm »
1  
2 package Text::Editor::Vip::Selection;
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 BEGIN
7 18 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 $VERSION = 0.01_1;
11 18 @ISA = qw (Exporter);
12 18 @EXPORT = qw ();
13 18 @EXPORT_OK = qw ();
14 18 %EXPORT_TAGS = ();
15 }
16  
17 =head1 NAME
18  
19 Text::Editor::Vip::Selection - Selection Range
20  
21 =head1 SYNOPSIS
22  
23 use Text::Editor::Vip::Selection
24 $selection = new Text::Editor::Vip::Selection()
25
26 $selection->IsEmpty() ;
27 ...
28
29 =head1 DESCRIPTION
30  
31 This class handles the boundaries of a selection and provides helper sub to manipulate the selection.
32  
33 =head1 MEMBER FUNCTIONS
34  
35 =cut
36  
37 #-------------------------------------------------------------------------------
38  
39 sub new
40 274 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
41 =head2 new
42  
43 Crete a selection object .
44  
45 =cut
46  
47 274 my $invocant = shift ;
48 274 ▶my $class = ref($invocant) || $invocant ;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
02740
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
49  
50 274 my $object_reference = bless {}, $class ;
51 274 $object_reference->Setup(@_) ;
52  
53 274 return($object_reference) ;
54 }
55  
56 #-------------------------------------------------------------------------------
57  
58 sub Setup
59 1044 ▶{
Subroutine Setup: called CC=1 SCAR=0.0
Pod documented
60  
61 =head2 Setup
62  
63 Setup a newly created object. This sub is private and shouldn't be used directely.
64  
65 =cut
66  
67 1044 my $this = shift ;
68  
69 1044 %$this =
70 (
71 START_LINE => -1
72 , START_CHARACTER => -1
73 , END_LINE => -1
74 , END_CHARACTER => -1
75 , @_
76 ) ;
77 }
78  
79 #-------------------------------------------------------------------------------
80  
81 18 ▶use Clone ;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 sub Clone
84 101 ▶{
Subroutine Clone: called CC=1 SCAR=0.0
Pod documented
85  
86 =head2 Clone
87  
88 Returns a copy of the object.
89  
90 =cut
91  
92 101 my $this = shift ;
93  
94 101 return(Clone::clone($this)) ;
95 }
96  
97 #-------------------------------------------------------------------------------
98  
99 sub IsEmpty
100 3577 ▶{
Subroutine IsEmpty: called CC=3 SCAR=11.0
Pod documented
101  
102 =head2 IsEmpty
103  
104 Returns 1 (one) if the selection boundaries are the same.
105  
106 Retuns 0 (zero) if the object defined a non empty selection.
107  
108 =cut
109  
110 3577 my $this = shift ;
111 3577 ▶if
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'START_LINE'} == $this->{'END_LINE'} and $this->{'START_CHARACTER'} == $this->{'END_CHARACTER'}) { } 3099 478 3577
Condition100%3/3
$this->{"START_LINE"} == $this->{"END_LINE"} and $this->{"START_CHARACTER"} == $this->{"END_CHARACTER"}
!ll&&!rl&&r
2372413099
MC/DC100%2/2
$this->{"START_LINE"} == $this->{"END_LINE"} and $this->{"START_CHARACTER"} == $this->{"END_CHARACTER"}
$this->{"START_LINE"} == $this->{"END_LINE"} $this->{"START_CHARACTER"} == $this->{"END_CHARACTER"}
Truth table100%3/3
A and B
A= $this->{"START_LINE"} == $this->{"END_LINE"}B= $this->{"START_CHARACTER"} == $this->{"END_CHARACTER"}
ABresult
0X0
100
111
112 (
113 $this->{START_LINE} == $this->{END_LINE}
114 && $this->{START_CHARACTER} == $this->{END_CHARACTER}
115 )
116 {
117 3099 return(1) ;
118 }
119 else
120 {
121 478 return(0) ;
122 }
123 }
124  
125 #-------------------------------------------------------------------------------
126  
127 sub Clear
128 286 ▶{
Subroutine Clear: called CC=1 SCAR=0.0
Pod undocumented
129 =head2 Clear
130  
131 Resets, the selection boundaries. The selection will be empty after calling this sub.
132  
133 =cut
134  
135 286 my $this = shift ;
136 286 $this->Setup() ;
137 }
138  
139 #-------------------------------------------------------------------------------
140  
141 sub Set($$$$) # Expects line and character
142 432 ▶{
Subroutine Set: called CC=1 SCAR=0.0
Pod documented
143  
144 =head2 Set
145  
146 Sets the selection boundaries. Four arguments are expected:
147  
148 =over 2
149  
150 =item * A start line
151  
152 =item * A start character
153  
154 =item * An end line
155  
156 =item * An end character
157  
158 =back
159  
160 =cut
161  
162 432 my $this = shift ;
163 432 $this->Setup() ;
164  
165 432 $this->{START_LINE} = shift ;
166 432 $this->{START_CHARACTER} = shift ;
167 432 $this->{END_LINE} = shift ;
168 432 $this->{END_CHARACTER} = shift ;
169 }
170  
171 #-------------------------------------------------------------------------------
172  
173 sub SetAnchor($$) # Expects line and character
174 52 ▶{
Subroutine SetAnchor: called CC=1 SCAR=0.0
Pod documented
175  
176 =head2 SetAnchor
177  
178 Sets the selection anchor. Two arguments are expected:
179  
180 =over 2
181  
182 =item * An anchor line
183  
184 =item * An anchor character
185  
186 =back
187  
188 =cut
189  
190 52 my $this = shift ;
191 52 $this->Setup() ;
192  
193 52 $this->{START_LINE} = shift ;
194 52 $this->{START_CHARACTER} = shift ;
195 }
196  
197 sub GetAnchor
198 40 ▶{
Subroutine GetAnchor: called CC=1 SCAR=0.0
Pod documented
199  
200 =head2 GetAnchor
201  
202 Returns the anchor position.
203  
204 =cut
205  
206 40 my $this = shift ;
207  
208 40 return($this->{START_LINE}, $this->{START_CHARACTER} ) ;
209 }
210  
211 #-------------------------------------------------------------------------------
212  
213 sub SetLine($$) # Expects line and character
214 142 ▶{
Subroutine SetLine: called CC=1 SCAR=0.0
Pod documented
215  
216 =head2 SetLine
217  
218 This sub set the other boundary of a selection. The other booundary being set by B<SetAnchor>.
219  
220 Two arguments are expected:
221  
222 =over 2
223  
224 =item * A line
225  
226 =item * A character
227  
228 =back
229  
230 =cut
231  
232 142 my $this = shift ;
233  
234 142 $this->{END_LINE} = shift ;
235 142 $this->{END_CHARACTER} = shift ;
236 }
237  
238 sub GetLine
239 130 ▶{
Subroutine GetLine: called CC=1 SCAR=0.0
Pod documented
240  
241 =head2 GetLine
242  
243 Returns the other boundarie of a selection.
244  
245 =cut
246  
247 130 my $this = shift ;
248  
249 130 return($this->{END_LINE}, $this->{END_CHARACTER} ) ;
250 }
251  
252 #-------------------------------------------------------------------------------
253  
254 sub GetBoundaries
255 939 ▶{
Subroutine GetBoundaries: called CC=4 SCAR=13.9
Pod documented
256  
257 =head2 GetBoundaries
258  
259 Returns the boundaries of a selection as a list consisting of
260  
261 =over 2
262  
263 =item * selection start line
264  
265 =item * selection start character
266  
267 =item * selection end line
268  
269 =item * selection end character
270  
271 =back
272  
273 the selection start line will always be lower or equal to the selection end line. The selection
274 anchoring is handled automatically.
275  
276 =cut
277  
278 939 my $this = shift ;
279 939 ▶if($this->{START_LINE} < $this->{END_LINE})
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'START_LINE'} < $this->{'END_LINE'}) { } 452 487 939
280 {
281 return
282 (
283 $this->{START_LINE}, $this->{START_CHARACTER}
284 , $this->{END_LINE}, $this->{END_CHARACTER}
285 452 ) ;
286 }
287 else
288 {
289 487 ▶ if($this->{START_LINE} == $this->{END_LINE})
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'START_LINE'} == $this->{'END_LINE'}) { } 456 31 487
290 {
291 456 ▶ if($this->{START_CHARACTER} < $this->{END_CHARACTER})
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'START_CHARACTER'} < $this->{'END_CHARACTER'}) { } 251 205 456
292 {
293 return
294 (
295 $this->{START_LINE}, $this->{START_CHARACTER}
296 , $this->{END_LINE}, $this->{END_CHARACTER}
297 251 ) ;
298 }
299 else
300 {
301 return
302 (
303 $this->{START_LINE}, $this->{END_CHARACTER}
304 , $this->{END_LINE}, $this->{START_CHARACTER}
305 205 ) ;
306 }
307 }
308 else
309 {
310 return
311 (
312 $this->{END_LINE}, $this->{END_CHARACTER}
313 , $this->{START_LINE}, $this->{START_CHARACTER}
314 31 ) ;
315 }
316 }
317 }
318  
319 #-------------------------------------------------------------------------------
320  
321 sub GetStartLine
322 1 ▶{
Subroutine GetStartLine: called CC=1 SCAR=0.0
Pod documented
323  
324 =head2 GetStartLine
325  
326 Gets the selection start line.
327  
328 =cut
329  
330 1 my $this = shift ;
331 1 return(($this->GetBoundaries())[0]) ;
332 }
333  
334 #-------------------------------------------------------------------------------
335  
336 sub GetStartCharacter
337 1 ▶{
Subroutine GetStartCharacter: called CC=1 SCAR=0.0
Pod documented
338  
339 =head2 GetStartCharacter
340  
341 Gets the selection start character.
342  
343 =cut
344  
345 1 my $this = shift ;
346 1 return(($this->GetBoundaries())[1]) ;
347 }
348  
349 #-------------------------------------------------------------------------------
350  
351 sub GetEndLine
352 58 ▶{
Subroutine GetEndLine: called CC=1 SCAR=0.0
Pod documented
353  
354 =head2 GetEndLine
355  
356 Gets the selection end line.
357  
358 =cut
359  
360 58 my $this = shift ;
361 58 return(($this->GetBoundaries())[2]) ;
362 }
363  
364 #-------------------------------------------------------------------------------
365  
366 sub GetEndCharacter
367 16 ▶{
Subroutine GetEndCharacter: called CC=1 SCAR=0.0
Pod documented
368  
369 =head2 GetEndCharacter
370  
371 Gets the selection end character.
372  
373 =cut
374  
375 16 my $this = shift ;
376 16 return(($this->GetBoundaries())[3]) ;
377 }
378  
379 #-------------------------------------------------------------------------------
380  
381 sub IsCharacterSelected($$) # Expects a line and a character index
382 49 ▶{
Subroutine IsCharacterSelected: called CC=6 SCAR=19.5
Pod documented
383  
384 =head2 IsCharacterSelected
385  
386 Given a line and character indexes, this sub returns 'true' if the given character is within
387 the selection boundaries. It otherwise returns 'false'.
388  
389 =cut
390  
391 49 my $this = shift ;
392 49 my $a_line_index = shift ;
393 49 my $a_character_index = shift ;
394  
395 49 my ($start_line, $start_character, $end_line, $end_character) = $this->GetBoundaries() ;
396  
397  
398 49 ▶if($start_line == $end_line && $start_line == $a_line_index)
Branch: 1 branch
BranchTrueFalseTotal
if ($start_line == $end_line and $start_line == $a_line_index) 0 49 49
Condition33%1/3
$start_line == $end_line and $start_line == $a_line_index
!ll&&!rl&&r
4900
MC/DC0%0/2
$start_line == $end_line and $start_line == $a_line_index
$start_line == $end_line $start_line == $a_line_index
Truth table33%1/3
A and B
A= $start_line == $end_lineB= $start_line == $a_line_index
ABresult
0X0
100
111
399 {
400 0 ▶ return($start_character <= $a_character_index && $a_character_index < $end_character) ;
Condition0%0/3
$start_character <= $a_character_index && $a_character_index < $end_character
!ll&&!rl&&r
000
MC/DC0%0/2
$start_character <= $a_character_index && $a_character_index < $end_character
$start_character <= $a_character_index $a_character_index < $end_character
Truth table0%0/3
A && B
A= $start_character <= $a_character_indexB= $a_character_index < $end_character
ABresult
0X0
100
111
401 }
402 49 ▶if($start_line == $a_line_index)
Branch: 1 branch
BranchTrueFalseTotal
if ($start_line == $a_line_index) 5 44 49
403 {
404 5 return($start_character <= $a_character_index) ;
405 }
406 44 ▶if($end_line == $a_line_index)
Branch: 1 branch
BranchTrueFalseTotal
if ($end_line == $a_line_index) { } 6 38 44
407 {
408 6 return($a_character_index < $end_character) ;
409 }
410 else
411 {
412 38 return($this->IsLineSelected($a_line_index)) ;
413 }
414 }
415  
416 #-------------------------------------------------------------------------------
417  
418 sub IsLineSelected($)
419 38 ▶{
Subroutine IsLineSelected: called CC=4 SCAR=15.5
Pod undocumented
420 =head2 IsLineSelected
421  
422 Given a line index, this sub returns 'true' if the given line is completely selected.
423 It otherwise returns 'false'.
424  
425 =cut
426  
427 38 my $this = shift ;
428 38 my $a_line_index = shift ;
429  
430 38 my ($start_line, undef, $end_line, $end_character) = $this->GetBoundaries() ;
431  
432 38 ▶if($end_character == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($end_character == 0) { } 0 38 38
433 {
434 0 ▶ return($start_line <= $a_line_index && $a_line_index < $end_line) ;
Condition0%0/3
$start_line <= $a_line_index && $a_line_index < $end_line
!ll&&!rl&&r
000
MC/DC0%0/2
$start_line <= $a_line_index && $a_line_index < $end_line
$start_line <= $a_line_index $a_line_index < $end_line
Truth table0%0/3
A && B
A= $start_line <= $a_line_indexB= $a_line_index < $end_line
ABresult
0X0
100
111
435 }
436 else
437 {
438 38 ▶ return($start_line <= $a_line_index && $a_line_index <= $end_line) ;
Condition100%3/3
$start_line <= $a_line_index && $a_line_index <= $end_line
!ll&&!rl&&r
61319
MC/DC100%2/2
$start_line <= $a_line_index && $a_line_index <= $end_line
$start_line <= $a_line_index $a_line_index <= $end_line
Truth table100%3/3
A && B
A= $start_line <= $a_line_indexB= $a_line_index <= $end_line
ABresult
0X0
100
111
439 }
440 }
441  
442 #-------------------------------------------------------------------------------
443  
444 18 1 ;
445  
446  
447 =head1 AUTHOR
448  
449 Khemir Nadim ibn Hamouda
450 CPAN ID: NKH
451 mailto:nadim@khemir.net
452 http:// no web site
453  
454 =head1 COPYRIGHT
455  
456 This program is free software; you can redistribute
457 it and/or modify it under the same terms as Perl itself.
458  
459 The full text of the license can be found in the
460 LICENSE file included with this module.
461  
462 =cut
« blib/lib/Text/Editor/Vip/CommandBlock.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/InsertDelete.pm »