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

filter: Statementstmt 91.8% 112 / 122 Branchbran 87.5% 14 / 16 Conditioncond 46.1% 12 / 26 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 93.3% 14 / 15 Podpod 100% 10 / 10 totaltotal 80.9% 166 / 205 CC 20 SCAR 14.3
CC
20
Cov
84%
GetDoCommandList
12.0
Redo
6.0
Undo
5.0
CRAP 4.2
« blib/lib/Text/Editor/Vip/Buffer/List.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/Movements.pm »
1  
2 package Text::Editor::Vip::Buffer::DoUndoRedo;
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 =
13 qw (
14 DecrementUndoStackLevel
15 GetDoScript
16 GetDoPosition
17 GetUndoScript
18 IncrementUndoStackLevel
19 PushUndoStep
20 Redo
21 Undo
22 );
23
24 18 @EXPORT_OK = qw ();
25 18 %EXPORT_TAGS = ();
26 }
27  
28 =head1 NAME
29  
30 Text::Editor::Vip::Buffer::DoUndoRedo - non optional plugin for a Text::Editor::Vip::Buffer
31  
32 =head1 DESCRIPTION
33  
34 The do, undo, redo functionality of Text::Editor::Vip::Buffer is implemented by this package.
35 This package automatically extends a Text::Editor::Vip::Buffer when a Text::Editor::Vip::Buffer
36 instance is created.
37  
38 This package manipulated the data structures used to implement a do, undo and redo functionality.
39 Text::Editor::Vip uses perl as it building block to implement such functionality. Perl "scripts" are the base
40 for implementing this functionality.
41  
42 =head1 MEMBER FUNCTIONS
43  
44 =cut
45  
46 #-------------------------------------------------------------------------------
47  
48 sub IncrementUndoStackLevel
49 10839 ▶{
Subroutine IncrementUndoStackLevel: called CC=1 SCAR=0.0
Pod documented
50  
51 =head2 IncrementUndoStackLevel
52  
53 Increments the do and undo prefix for the perl "scripts' used to implement do and undo.
54  
55 =cut
56  
57 10839 my ($buffer, $do_prefix, $undo_prefix) = @_;
58  
59 10839 $buffer->{DO_PREFIX} .= $do_prefix ;
60 10839 $buffer->{UNDO_PREFIX} .= $undo_prefix;
61 }
62  
63 #-------------------------------------------------------------------------------
64  
65 sub DecrementUndoStackLevel
66 10839 ▶{
Subroutine DecrementUndoStackLevel: called CC=1 SCAR=0.0
Pod documented
67  
68 =head2 DecrementUndoStackLevel
69  
70 Decrements the do and undo prefix for the perl "scripts' used to implement do and undo.
71  
72 =cut
73  
74 10839 my ($buffer, $do_prefix, $undo_prefix) = @_;
75  
76 10839 substr $buffer->{DO_PREFIX}, -(length($do_prefix)), length($do_prefix), '' ;
77 10839 substr $buffer->{UNDO_PREFIX}, -(length($undo_prefix)), length($undo_prefix), '';
78 }
79  
80 #-------------------------------------------------------------------------------
81  
82 sub PushUndoStep
83 26150 ▶{
Subroutine PushUndoStep: called CC=3 SCAR=11.1
Pod documented
84  
85 =head2 PushUndoStep
86  
87 Adds a do "script" to the do command list and an undo "script" to the undo command list.
88 The scripts are prepended with the prefixes defined by L<IncrementUndoStackLevel> and L<DecrementUndoStackLevel>
89  
90 =cut
91  
92 26150 my $buffer = shift ;
93 26150 my $do = shift ;
94 26150 my $undo = shift ;
95  
96 26150 my ($package, $file_name, $line, $sub) = caller(1) ;
97 26150 my $description = "'$sub' @ $file_name:$line" ;
98  
99 26150 my $do_text = '' ;
100 26150 ▶if('ARRAY' eq ref $do)
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $do) { } 0 26150 26150
101 {
102 0 $do_text .= join(" #continued\n", map{"$buffer->{DO_PREFIX}$_"} @$do) ;
103 }
104 else
105 {
106 26150 $do_text = "$buffer->{DO_PREFIX}$do" ;
107 }
108
109 #~ $do_text = "#$description\n$do_text" ;
110 26150 push @{$buffer->{DO_STACK}}, $do_text ;
111  
112 26150 my $undo_text = '' ;
113 26150 ▶if('ARRAY' eq ref $undo)
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $undo) { } 1391 24759 26150
114 {
115 1391 $undo_text .= join(" #continued\n", map{"$buffer->{UNDO_PREFIX}$_"} @$undo) ;
116 }
117 else
118 {
119 24759 $undo_text = "$buffer->{UNDO_PREFIX}$undo" ;
120 }
121  
122 #~ $undo_text = "#$description\n$undo_text" ;
123 26150 push @{$buffer->{UNDO_STACK}}, $undo_text ;
124 }
125  
126 #-------------------------------------------------------------------------------
127  
128 sub Undo
129 7 ▶{
Subroutine Undo: called CC=5 SCAR=16.1
Pod documented
130  
131 =head2 Undo
132  
133 Undoes the commands the commands that have been executed on a buffer.
134  
135 $buffer->Undo(1) ; # undoes one high level command
136
137 =cut
138  
139 7 my $buffer = shift ;
140 7 my $number_of_undo_steps = shift ;
141  
142 # not very effective to copy what could be a huge undo stack
143 # specialy since we modify it directely later and it's trivial to write better !
144 7 my $undo_command_list = $buffer->GetUndoCommandList() ;
145  
146 7 my $undo_steps = 0 ;
147 7 my $uncommented_undo_script = '' ;
148 7 my $comments = 0 ;
149  
150 7 ▶while(defined (my $undo_step = shift @$undo_command_list) && $number_of_undo_steps)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $undo_step = shift @$undo_command_list) and $number_of_undo_steps) 58 7 65
Condition66%2/3
defined(my $undo_step = shift @$undo_command_list) and $number_of_undo_steps
!ll&&!rl&&r
0758
MC/DC50%1/2
defined(my $undo_step = shift @$undo_command_list) and $number_of_undo_steps
defined(my $undo_step = shift @$undo_command_list) $number_of_undo_steps
Truth table66%2/3
A and B
A= defined(my $undo_step = shift @$undo_command_list)B= $number_of_undo_steps
ABresult
0X0
100
111
151 {
152 58 $undo_steps++ ;
153
154 58 ▶ if($undo_step =~ /^\s*#/)
Branch: 1 branch
BranchTrueFalseTotal
if ($undo_step =~ /^\s*#/) { } 30 28 58
155 {
156 30 $comments++ ;
157 }
158 else
159 {
160 28 $uncommented_undo_script .= "$undo_step\n" ;
161 }
162
163 58 ▶ unless($undo_step =~ /^\s/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($undo_step =~ /^\s/) 7 51 58
164 {
165 #~ $buffer->PrintPositionData() ;
166 #~ diag "Undo steps: $undo_steps $comments comments\n" ;
167 #~ diag "$uncommented_undo_script\n*******\n" ;
168
169 # move do stuff to redo stack
170 7 my $position = $buffer->GetDoPosition() ;
171
172 7 my @redo_step = @{$buffer->{DO_STACK}}[$position - $undo_steps .. $position - 1] ;
173 7 unshift @{$buffer->{REDO_STACK}}, @redo_step ;
174
175 # run undo script
176 7 $buffer->Do($uncommented_undo_script) ;
177
178 # remove newly added commands from do and undo stack
179 7 splice(@{$buffer->{DO_STACK}}, ($position - $undo_steps) );
180 7 splice(@{$buffer->{UNDO_STACK}}, ($position - $undo_steps) );
181
182 7 $uncommented_undo_script = '' ;
183 7 $undo_steps = 0 ;
184 7 $comments = 0 ;
185
186 7 $number_of_undo_steps-- ;
187 }
188 }
189 }
190  
191 #-------------------------------------------------------------------------------
192  
193 sub GetDoPosition
194 20 ▶{
Subroutine GetDoPosition: called CC=1 SCAR=0.0
Pod documented
195  
196 =head2 GetDoPosition
197  
198 Gets the current index of the do command list. This index can be later passes to L<GetUndoScript> to
199 get a selected amount of undo commands.
200  
201 This index can also be used to get a selected amound of do commands to implement a macro facility.
202  
203 my $start_position = $buffer->GetDoPosition() ;
204  
205 $buffer->DoLotsOfStuff() ;
206 $buffer->DoEvenMoreStuff() ;
207
208 # get scripts that would undo everything since we got the do position
209 $undo = $buffer->GetUndoScript($start_position) ;
210
211 # get scripts that correspond sto what has been done since we got the do position
212 $do = $buffer->GetDoScript($start_position) ;
213  
214 =cut
215  
216 20 my $buffer = shift ;
217  
218 20 return(scalar(@{$buffer->{DO_STACK}})) ;
219 }
220  
221 #-------------------------------------------------------------------------------
222  
223 sub GetUndoScript
224 13 ▶{
Subroutine GetUndoScript: called CC=3 SCAR=11.3
Pod documented
225  
226 =head2 GetUndoScript
227  
228 This sub is given a start and and end index. those indexes are used to retrieve undo commands.
229  
230 If not arguments are passed, the start index will be set to zero (first undo command) and the last available
231 undo command. Thus returning a list of all the commands in the undo stack.
232  
233  
234 See L<GetDoPosition>
235  
236 =cut
237  
238 13 my $buffer = shift ;
239  
240 13 ▶my $start = shift || 0 ;
Condition50%1/2
shift() || 0
l!l
130
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
241 13 ▶my $end = shift || $#{$buffer->{UNDO_STACK}} ;
Condition33%1/3
shift() || $#{$$buffer{'UNDO_STACK'};}
l!l&&r!l&&!r
0130
MC/DC0%0/2
shift() || $#{$$buffer{'UNDO_STACK'};}
shift() $#{$$buffer{'UNDO_STACK'};}
Truth table33%1/3
A || B
A= shift()B= $#{$$buffer{'UNDO_STACK'};}
ABresult
1X1
011
000
242  
243 13 my @undo_buffer = @{$buffer->{UNDO_STACK}}[$start .. $end] ;
244 13 @undo_buffer = reverse @undo_buffer ;
245  
246 13 my($modification_line, $modification_character) = $buffer->GetModificationPosition() ;
247  
248 13 return("# Current position: $modification_line, $modification_character\n" . join("\n", @undo_buffer) . "\n") ;
249 }
250  
251 #-------------------------------------------------------------------------------
252  
253 sub GetUndoCommandList
254 7 ▶{
Subroutine GetUndoCommandList: called CC=3 SCAR=11.3
Pod documented
255  
256 =head2 GetUndoCommandList
257  
258 This sub is given a start and and end index. those indexes are used to retrieve undo commands.
259  
260 If not arguments are passed, the start index will be set to zero (first undo command) and the last available
261 undo command. Thus returning a list of all the commands in the undo stack.
262  
263 See L<GetDoPosition>
264  
265 =cut
266  
267 7 my $buffer = shift ;
268  
269 7 ▶my $start = shift || 0 ;
Condition50%1/2
shift() || 0
l!l
07
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
270 7 ▶my $end = shift || $#{$buffer->{UNDO_STACK}} ;
Condition33%1/3
shift() || $#{$$buffer{'UNDO_STACK'};}
l!l&&r!l&&!r
070
MC/DC0%0/2
shift() || $#{$$buffer{'UNDO_STACK'};}
shift() $#{$$buffer{'UNDO_STACK'};}
Truth table33%1/3
A || B
A= shift()B= $#{$$buffer{'UNDO_STACK'};}
ABresult
1X1
011
000
271  
272 7 my @undo_command_list = @{$buffer->{UNDO_STACK}}[$start .. $end] ;
273 7 @undo_command_list = reverse @undo_command_list;
274  
275 7 return(\@undo_command_list) ;
276 }
277  
278 #-------------------------------------------------------------------------------
279  
280 sub GetDoScript
281 25 ▶{
Subroutine GetDoScript: called CC=3 SCAR=11.2
Pod documented
282  
283 =head2 GetDoScript
284  
285 This sub is given a start and and end index. those indexes are used to retrieve do commands.
286  
287 If not arguments are passed, the start index will be set to zero (first do command) and the last available
288 do command. Thus returning a list of all the commands in the do stack.
289  
290 See L<GetDoPosition>
291  
292 =cut
293  
294 25 my $buffer = shift ;
295  
296 25 ▶my $start = shift || 0 ;
Condition100%2/2
shift() || 0
l!l
1312
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
297 25 ▶my $end = shift || $#{$buffer->{DO_STACK}} ;
Condition33%1/3
shift() || $#{$$buffer{'DO_STACK'};}
l!l&&r!l&&!r
0250
MC/DC0%0/2
shift() || $#{$$buffer{'DO_STACK'};}
shift() $#{$$buffer{'DO_STACK'};}
Truth table33%1/3
A || B
A= shift()B= $#{$$buffer{'DO_STACK'};}
ABresult
1X1
011
000
298  
299 25 return(join("\n",@{$buffer->{DO_STACK}}[$start .. $end]) . "\n") ;
300 }
301  
302 #-------------------------------------------------------------------------------
303  
304 sub GetDoCommandList
305 0 ▶{
Subroutine GetDoCommandList: not called CC=3 SCAR=24.8
Pod documented
306  
307 =head2 GetDoCommandList
308  
309 This sub is given a start and and end index. those indexes are used to retrieve do commands.
310  
311 If not arguments are passed, the start index will be set to zero (first do command) and the last available
312 do command. Thus returning a list of all the commands in the do stack.
313  
314 See L<GetDoPosition>
315  
316 =cut
317  
318 0 my $buffer = shift ;
319  
320 0 ▶my $start = shift || 0 ;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
321 0 ▶my $end = shift || $#{$buffer->{DO_STACK}} ;
Condition0%0/3
shift() || $#{$$buffer{'DO_STACK'};}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $#{$$buffer{'DO_STACK'};}
shift() $#{$$buffer{'DO_STACK'};}
Truth table0%0/3
A || B
A= shift()B= $#{$$buffer{'DO_STACK'};}
ABresult
1X1
011
000
322  
323 0 my @do_command_list = @{$buffer->{DO_STACK}}[$start .. $end] ;
324  
325 0 return(\@do_command_list) ;
326 }
327  
328 #-------------------------------------------------------------------------------
329  
330 sub Redo
331 3 ▶{
Subroutine Redo: called CC=6 SCAR=17.9
Pod documented
332  
333 =head2 Redo
334  
335 Redoes the commands that have been undone.
336  
337 =cut
338  
339 3 my $buffer = shift ;
340 3 my $number_of_redo_steps = shift ;
341  
342 3 for( 1 .. $number_of_redo_steps)
343 {
344 # do this inline to avoid using memory!
345 6 my @redo_command_list = @{$buffer->{REDO_STACK}} ;
346  
347 6 my $uncommented_redo_script = shift @redo_command_list ;
348 6 my $redo_steps = 1 ;
349 6 my $comments = 0 ;
350
351 6 my $in_same_command = 1 ;
352 6 ▶ while(defined (my $redo_step = shift @redo_command_list) && $in_same_command)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $redo_step = shift @redo_command_list) and $in_same_command) 48 6 54
Condition100%3/3
defined(my $redo_step = shift @redo_command_list) and $in_same_command
!ll&&!rl&&r
2448
MC/DC100%2/2
defined(my $redo_step = shift @redo_command_list) and $in_same_command
defined(my $redo_step = shift @redo_command_list) $in_same_command
Truth table100%3/3
A and B
A= defined(my $redo_step = shift @redo_command_list)B= $in_same_command
ABresult
0X0
100
111
353 {
354 48 ▶ if($redo_step =~ /^\s/)
Branch: 1 branch
BranchTrueFalseTotal
if ($redo_step =~ /^\s/) { } 44 4 48
355 {
356 44 $redo_steps++ ;
357
358 44 ▶ if($redo_step =~ /^\s+#/)
Branch: 1 branch
BranchTrueFalseTotal
if ($redo_step =~ /^\s+#/) { } 44 0 44
359 {
360 44 $comments++ ;
361 }
362 else
363 {
364 0 $uncommented_redo_script .= "$redo_step\n" ;
365 }
366 }
367 else
368 {
369 4 unshift @redo_command_list, $redo_step ;
370 4 $in_same_command = 0 ;
371 }
372 }
373
374 #~ $buffer->PrintPositionData() ;
375 #~ diag "redo steps: $redo_steps $comments comments\n" ;
376 #~ diag "$uncommented_redo_script\n*******\n" ;
377
378 # run redo script
379 6 $buffer->Do($uncommented_redo_script) ;
380
381 # remove commands from redo stack
382 6 splice(@{$buffer->{REDO_STACK}}, 0, $redo_steps);
383 }
384 }
385  
386 #-------------------------------------------------------------------------------
387  
388 18 1 ;
389  
390 =head1 AUTHOR
391  
392 Khemir Nadim ibn Hamouda
393 CPAN ID: NKH
394 mailto:nadim@khemir.net
395 http:// no web site
396  
397 =head1 COPYRIGHT
398  
399 This program is free software; you can redistribute
400 it and/or modify it under the same terms as Perl itself.
401  
402 The full text of the license can be found in the
403 LICENSE file included with this module.
404  
405 =cut
406  
« blib/lib/Text/Editor/Vip/Buffer/List.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/Movements.pm »