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

filter: Statementstmt 98.3% 61 / 62 Branchbran 71.4% 10 / 14 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 88.6% 86 / 97 CC 11 SCAR 15.8
CC
11
Cov
90%
InsertConstruct
8.2
InsertAlignedWithTab
4.0
BEGIN
1.0
CRAP 4.9
« lib/Text/Editor/Vip/Buffer/Plugins/Clipboard.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/List.pm »
1  
2 package Text::Editor::Vip::Buffer::Plugins::InsertConstruct ;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN
8 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $VERSION = 0.01;
13 1 @ISA = qw (Exporter);
14 1 @EXPORT = qw ();
15 1 @EXPORT_OK = qw ();
16 1 %EXPORT_TAGS = ();
17 }
18  
19 1 ▶use Text::Editor::Vip::Buffer::Constants ;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 Text::Editor::Vip::Buffer::Plugins::InsertConstruct- Vip::Buffer plugin
24  
25 =head1 SYNOPSIS
26  
27 $buffer->InsertAlignedWithTab("XXXXX") ;
28  
29 $buffer->Insert($text) ;
30 $buffer->SetModificationPosition(0, 4) ;
31 $buffer->SetSelectionBoundaries(1, 0, 3, 0) ;
32 $buffer->InsertConstruct($text_to_insert) ;
33  
34 =head1 DESCRIPTION
35  
36 Text::Editor::Vip::Buffer::Plugins::InsertConstruct- Vip::Buffer plugin
37  
38 =head1 FUNCTIONS
39  
40 =cut
41  
42 #-------------------------------------------------------------------------------
43  
44 sub InsertAlignedWithTab
45 7 ▶{
Subroutine InsertAlignedWithTab: called CC=4 SCAR=13.9
Pod documented
46  
47 =head2 InsertAlignedWithTab
48  
49 InsertAligned takes a string as argument
50  
51 =cut
52  
53 7 my $buffer = shift ;
54 7 ▶my $text_to_insert = shift || return ;
Condition50%1/2
shift() || (return)
l!l
70
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
55  
56 7 ▶die unless '' eq ref $text_to_insert ;
Branch: 1 branch
BranchTrueFalseTotal
unless "" eq ref $text_to_insert 0 7 7
57  
58 7 $buffer->ExpandedWithOrLoad('GetTabSize', 'Text::Editor::Vip::Buffer::Plugins::Display') ;
59  
60 7 my $modification_character = $buffer->GetModificationCharacter() ;
61  
62 7 my $tab_size = $buffer->GetTabSize() ;
63 7 my $line_text = substr($buffer->GetLineText(), 0, $modification_character) ;
64 7 my $tabs_in_line = $line_text =~ tr/\t/\t/ ;
65  
66 7 my $raw_indent = "\t" x ((($modification_character - $tabs_in_line)/ $tab_size) + $tabs_in_line) ;
67 7 $raw_indent .= ' ' x (($modification_character - $tabs_in_line) % $tab_size) ;
68  
69 7 my @text_to_insert = split(qr"(\n)", $text_to_insert) ;
70 7 ▶@text_to_insert = map {$_ eq "\n" ? "\n$raw_indent" : $_} @text_to_insert ;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "\n" ? : 16 20 36
71  
72 7 $buffer->Insert(\@text_to_insert , NO_SMART_INDENTATION) ;
73 }
74  
75 #-------------------------------------------------------------------------------
76  
77 sub InsertConstruct
78 5 ▶{
Subroutine InsertConstruct: called CC=8 SCAR=21.0
Pod documented
79  
80 =head2 InsertConstruct
81  
82 Inserts a construct at the cursor position or around the selection if any if a selection is present
83 the construct is aligned on the block and the selection is indented.
84  
85 $if_construct= <<EOT ;
86 if()
87 \t{
88 \t}
89 else
90 \t{
91 \tSELECTION
92 \t}
93 EOT
94  
95 $buffer->Insert(<<EOT) ;
96 line 1 - 1
97 line 2 - 2 2
98 line 3 - 3 3 3
99 EOT
100
101 $buffer->SetModificationPosition(3, 4) ;
102 $buffer->SetSelectionBoundaries(1, 0, 2, 0) ; # select the second line
103 $buffer->InsertConstruct($if_construct) ;
104  
105 Buffer content is now
106  
107 line 1 - 1
108 if()
109 \t{
110 \t}
111 else
112 \t{
113 \tline 2 - 2 2 # this line was moved in the construct and aligned
114 \t}
115 line 3 - 3 3 3
116  
117 =cut
118  
119 5 my $buffer = shift ;
120 5 ▶my $construct = shift || return ;
Condition50%1/2
shift() || (return)
l!l
50
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
121  
122 5 $buffer->ExpandedWithOrLoad('InsertTab', 'Text::Editor::Vip::Buffer::Plugins::InsertDelete') ;
123  
124 5 my ($head, $selection_indentation, $foot) ;
125  
126 5 ▶if($construct =~ /(.*)(^\t*)SELECTION[^\n]*\n(.*)/ms)
Branch: 1 branch
BranchTrueFalseTotal
if ($construct =~ /(.*)(^\t*)SELECTION[^\n]*\n(.*)/ms) 5 0 5
127 {
128 5 ($head, $selection_indentation, $foot) = ($1, $2, $3) ;
129 }
130
131 5 ▶unless($buffer->GetSelection()->IsEmpty())
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer->GetSelection->IsEmpty) { } 1 4 5
132 {
133 my (
134 4 $selection_start_line, $selection_start_character
135 , $selection_end_line, $selection_end_character
136 ) = $buffer->GetSelectionBoundaries() ;
137
138 4 ▶ if($selection_start_character != 0 && $selection_end_character != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($selection_start_character != 0 and $selection_end_character != 0) 1 3 4
Condition66%2/3
$selection_start_character != 0 and $selection_end_character != 0
!ll&&!rl&&r
301
MC/DC50%1/2
$selection_start_character != 0 and $selection_end_character != 0
$selection_start_character != 0 $selection_end_character != 0
Truth table66%2/3
A and B
A= $selection_start_character != 0B= $selection_end_character != 0
ABresult
0X0
100
111
139 {
140 1 $buffer->PrintError("Please Select entire lines") ;
141 0 last ;
142 }
143
144 # compute new construct indentation base on the first non empty selection line
145 3 ▶ for
Branch: 1 branch
BranchTrueFalseTotal
if ($selection_line_index < $selection_end_line) 3 0 3
146 (
147 my $selection_line_index = $selection_start_line
148 ; $selection_line_index < $selection_end_line
149 ; $selection_line_index++
150 )
151 {
152 3 my $line = $buffer->GetLineText($selection_line_index) ;
153
154 3 ▶ if($line =~ /^(\t|\s*)[^\n]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\t|\s*)[^\n]/) 3 0 3
155 {
156 3 my $construct_indentation = $1 ;
157
158 3 $head =~ s/^/$construct_indentation/mg ;
159 3 $foot =~ s/^/$construct_indentation/mg ;
160
161 3 last ;
162 }
163 }
164
165 # indent the selection
166 3 $buffer->InsertTab() ;
167 3 $buffer->GetSelection()->Clear() ;
168
169 3 $buffer->SetModificationPosition($selection_end_line, 0) ;
170 3 $buffer->Insert($foot, NO_SMART_INDENTATION) ;
171
172 3 $buffer->SetModificationPosition($selection_start_line, 0) ;
173 3 $buffer->Insert($head, NO_SMART_INDENTATION) ;
174 }
175 else
176 {
177 1 $buffer->GetSelection()->Clear() ;
178
179 1 $buffer->SetModificationPosition($buffer->GetModificationLine(), 0) ;
180 1 $buffer->Insert($head . $foot) ;
181 }
182  
183 }
184  
185 #-------------------------------------------------------------------------------
186  
187 1 1 ;
188  
189 =head1 AUTHOR
190  
191 Khemir Nadim ibn Hamouda
192 CPAN ID: NKH
193 mailto:nadim@khemir.net
194 http:// no web site
195  
196 =head1 COPYRIGHT
197  
198 This program is free software; you can redistribute
199 it and/or modify it under the same terms as Perl itself.
200  
201 The full text of the license can be found in the
202 LICENSE file included with this module.
203  
204 =cut
« lib/Text/Editor/Vip/Buffer/Plugins/Clipboard.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/List.pm »