File Coverage

blib/lib/Perl/Tidy/IndentationItem.pm
Criterion Covered Total %
statement 106 118 89.8
branch 15 26 57.6
condition 3 3 100.0
subroutine 30 34 88.2
pod 0 28 0.0
total 154 209 73.6


line stmt bran cond sub pod time code
1             package Perl::Tidy::IndentationItem;
2              
3             #####################################################################
4             #
5             # The Perl::Tidy::IndentationItem class supplies items which contain
6             # how much whitespace should be used at the start of a line
7             #
8             #####################################################################
9              
10 44     44   238 use strict;
  44         77  
  44         1205  
11 44     44   144 use warnings;
  44         56  
  44         4275  
12              
13             our $VERSION = '20260705';
14              
15 0         0 BEGIN {
16              
17             # Array index names
18             # Do not combine with other BEGIN blocks (c101).
19 44     44   45241 my $i = 0;
20             use constant {
21 44         7577 _spaces_ => $i++,
22             _level_ => $i++,
23             _ci_level_ => $i++,
24             _available_spaces_ => $i++,
25             _closed_ => $i++,
26             _comma_count_ => $i++,
27             _lp_item_index_ => $i++,
28             _have_child_ => $i++,
29             _recoverable_spaces_ => $i++,
30             _align_seqno_ => $i++,
31             _marked_ => $i++,
32             _K_begin_line_ => $i++,
33             _arrow_count_ => $i++,
34             _standard_spaces_ => $i++,
35             _K_extra_space_ => $i++,
36 44     44   194 };
  44         125  
37             } ## end BEGIN
38              
39             sub AUTOLOAD {
40              
41             # Catch any undefined sub calls so that we are sure to get
42             # some diagnostic information. This sub should never be called
43             # except for a programming error.
44 0     0   0 our $AUTOLOAD;
45 0 0       0 return if ( $AUTOLOAD =~ /\bDESTROY$/ );
46 0         0 my ( $pkg, $fname, $lno ) = caller();
47 0         0 my $my_package = __PACKAGE__;
48 0         0 print {*STDERR} <<EOM;
  0         0  
49             ======================================================================
50             Error detected in package '$my_package', version $VERSION
51             Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
52             Called from package: '$pkg'
53             Called from File '$fname' at line '$lno'
54             This error is probably due to a recent programming change
55             ======================================================================
56             EOM
57 0         0 exit 1;
58             } ## end sub AUTOLOAD
59              
60       0     sub DESTROY {
61              
62             # required to avoid call to AUTOLOAD in some versions of perl
63             }
64              
65             sub new {
66              
67             # Create an 'indentation_item' which describes one level of leading
68             # whitespace when the '-lp' indentation is used.
69 623     623 0 3227 my ( $class, %input_hash ) = @_;
70              
71             # DEFINITIONS:
72             # spaces => # total leading white spaces
73             # level => # the indentation 'level'
74             # ci_level => # the 'continuation level'
75             # available_spaces => # how many left spaces available
76             # # for this level
77             # closed => # index where we saw closing '}'
78             # comma_count => # how many commas at this level?
79             # lp_item_index => # index in output batch list
80             # have_child => # any dependents?
81             # recoverable_spaces => # how many spaces to the right
82             # # we would like to move to get
83             # # alignment (negative if left)
84             # align_seqno => # if we are aligning with an opening structure,
85             # # this is its seqno
86             # marked => # if visited by corrector logic
87             # K_begin_line => # first token index K of this level
88             # arrow_count => # how many =>'s
89              
90 623         829 my $self = [];
91 623         843 bless $self, $class;
92              
93 623         1050 $self->[_spaces_] = $input_hash{spaces};
94 623         907 $self->[_level_] = $input_hash{level};
95 623         742 $self->[_ci_level_] = $input_hash{ci_level};
96 623         769 $self->[_available_spaces_] = $input_hash{available_spaces};
97 623         848 $self->[_closed_] = -1;
98 623         1020 $self->[_comma_count_] = 0;
99 623         857 $self->[_lp_item_index_] = $input_hash{lp_item_index};
100 623         765 $self->[_have_child_] = 0;
101 623         904 $self->[_recoverable_spaces_] = 0;
102 623         964 $self->[_align_seqno_] = $input_hash{align_seqno};
103 623         1030 $self->[_marked_] = 0;
104 623         846 $self->[_K_begin_line_] = $input_hash{K_begin_line};
105 623         853 $self->[_arrow_count_] = 0;
106 623         781 $self->[_standard_spaces_] = $input_hash{standard_spaces};
107 623         782 $self->[_K_extra_space_] = $input_hash{K_extra_space};
108              
109 623         1616 return $self;
110             } ## end sub new
111              
112             sub permanently_decrease_available_spaces {
113              
114             # make a permanent reduction in the available indentation spaces
115             # at one indentation item. NOTE: if there are child nodes, their
116             # total SPACES must be reduced by the caller.
117              
118 279     279 0 395 my ( $self, $spaces_needed ) = @_;
119 279         413 my $available_spaces = $self->get_available_spaces();
120 279 100       411 my $deleted_spaces =
121             ( $available_spaces > $spaces_needed )
122             ? $spaces_needed
123             : $available_spaces;
124              
125             # Fixed for c085; a zero value must remain unchanged unless the closed
126             # flag has been set.
127 279         386 my $closed = $self->get_closed();
128 279 100 100     1726 $self->decrease_available_spaces($deleted_spaces)
129             if ( $available_spaces != 0 || $closed >= 0 );
130 279         457 $self->decrease_SPACES($deleted_spaces);
131 279         501 $self->set_recoverable_spaces(0);
132              
133 279         523 return $deleted_spaces;
134             } ## end sub permanently_decrease_available_spaces
135              
136             sub tentatively_decrease_available_spaces {
137              
138             # We are asked to tentatively delete $spaces_needed of indentation
139             # for an indentation item. We may want to undo this later. NOTE: if
140             # there are child nodes, their total SPACES must be reduced by the
141             # caller.
142 72     72 0 117 my ( $self, $spaces_needed ) = @_;
143 72         125 my $available_spaces = $self->get_available_spaces();
144 72 100       144 my $deleted_spaces =
145             ( $available_spaces > $spaces_needed )
146             ? $spaces_needed
147             : $available_spaces;
148 72         219 $self->decrease_available_spaces($deleted_spaces);
149 72         162 $self->decrease_SPACES($deleted_spaces);
150 72         185 $self->increase_recoverable_spaces($deleted_spaces);
151 72         118 return $deleted_spaces;
152             } ## end sub tentatively_decrease_available_spaces
153              
154             # time-critical sub
155             sub get_spaces {
156 6829     6829 0 10197 return $_[0]->[_spaces_];
157             }
158              
159             sub get_standard_spaces {
160 57     57 0 72 my $self = shift;
161 57         93 return $self->[_standard_spaces_];
162             }
163              
164             # time-critical sub
165             sub get_marked {
166 3261     3261 0 5184 return $_[0]->[_marked_];
167             }
168              
169             sub set_marked {
170 623     623 0 829 my ( $self, $value ) = @_;
171 623 50       907 if ( defined($value) ) {
172 623         714 $self->[_marked_] = $value;
173             }
174 623         770 return $self->[_marked_];
175             } ## end sub set_marked
176              
177             sub get_available_spaces {
178 1029     1029 0 1143 my $self = shift;
179 1029         1389 return $self->[_available_spaces_];
180             }
181              
182             sub decrease_SPACES {
183 881     881 0 988 my ( $self, $value ) = @_;
184 881 50       1113 if ( defined($value) ) {
185 881         871 $self->[_spaces_] -= $value;
186             }
187 881         1100 return $self->[_spaces_];
188             } ## end sub decrease_SPACES
189              
190             sub decrease_available_spaces {
191 351     351 0 440 my ( $self, $value ) = @_;
192              
193 351 50       532 if ( defined($value) ) {
194 351         424 $self->[_available_spaces_] -= $value;
195             }
196 351         396 return $self->[_available_spaces_];
197             } ## end sub decrease_available_spaces
198              
199             sub get_align_seqno {
200 623     623 0 669 my $self = shift;
201 623         1060 return $self->[_align_seqno_];
202             }
203              
204             sub get_recoverable_spaces {
205 303     303 0 376 my $self = shift;
206 303         753 return $self->[_recoverable_spaces_];
207             }
208              
209             sub set_recoverable_spaces {
210 444     444 0 640 my ( $self, $value ) = @_;
211 444 50       697 if ( defined($value) ) {
212 444         530 $self->[_recoverable_spaces_] = $value;
213             }
214 444         542 return $self->[_recoverable_spaces_];
215             } ## end sub set_recoverable_spaces
216              
217             sub increase_recoverable_spaces {
218 72     72 0 108 my ( $self, $value ) = @_;
219 72 50       164 if ( defined($value) ) {
220 72         107 $self->[_recoverable_spaces_] += $value;
221             }
222 72         113 return $self->[_recoverable_spaces_];
223             } ## end sub increase_recoverable_spaces
224              
225             sub get_ci_level {
226 0     0 0 0 my $self = shift;
227 0         0 return $self->[_ci_level_];
228             }
229              
230             sub get_level {
231 0     0 0 0 my $self = shift;
232 0         0 return $self->[_level_];
233             }
234              
235             sub get_spaces_level_ci {
236 1333     1333 0 1503 my $self = shift;
237 1333         3411 return [ $self->[_spaces_], $self->[_level_], $self->[_ci_level_] ];
238             }
239              
240             sub get_lp_item_index {
241 56     56 0 77 my $self = shift;
242 56         95 return $self->[_lp_item_index_];
243             }
244              
245             sub get_K_begin_line {
246 736     736 0 862 my $self = shift;
247 736         1179 return $self->[_K_begin_line_];
248             }
249              
250             sub get_K_extra_space {
251 30     30 0 35 my $self = shift;
252 30         60 return $self->[_K_extra_space_];
253             }
254              
255             sub set_have_child {
256 506     506 0 707 my ( $self, $value ) = @_;
257 506 50       778 if ( defined($value) ) {
258 506         636 $self->[_have_child_] = $value;
259             }
260 506         672 return $self->[_have_child_];
261             } ## end sub set_have_child
262              
263             sub get_have_child {
264 70     70 0 110 my $self = shift;
265 70         116 return $self->[_have_child_];
266             }
267              
268             sub set_arrow_count {
269 623     623 0 777 my ( $self, $value ) = @_;
270 623 50       994 if ( defined($value) ) {
271 623         717 $self->[_arrow_count_] = $value;
272             }
273 623         778 return $self->[_arrow_count_];
274             } ## end sub set_arrow_count
275              
276             sub get_arrow_count {
277 69     69 0 93 my $self = shift;
278 69         95 return $self->[_arrow_count_];
279             }
280              
281             sub set_comma_count {
282 623     623 0 777 my ( $self, $value ) = @_;
283 623 50       1024 if ( defined($value) ) {
284 623         792 $self->[_comma_count_] = $value;
285             }
286 623         796 return $self->[_comma_count_];
287             } ## end sub set_comma_count
288              
289             sub get_comma_count {
290 69     69 0 104 my $self = shift;
291 69         111 return $self->[_comma_count_];
292             }
293              
294             sub set_closed {
295 623     623 0 781 my ( $self, $value ) = @_;
296 623 50       936 if ( defined($value) ) {
297 623         717 $self->[_closed_] = $value;
298             }
299 623         888 return $self->[_closed_];
300             } ## end sub set_closed
301              
302             sub get_closed {
303 1153     1153 0 1179 my $self = shift;
304 1153         1948 return $self->[_closed_];
305             }
306             1;