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             #####################################################################
2             #
3             # The Perl::Tidy::IndentationItem class supplies items which contain
4             # how much whitespace should be used at the start of a line
5             #
6             #####################################################################
7              
8             package Perl::Tidy::IndentationItem;
9 44     44   268 use strict;
  44         86  
  44         1424  
10 44     44   173 use warnings;
  44         74  
  44         4533  
11              
12             our $VERSION = '20260204';
13              
14 0         0 BEGIN {
15              
16             # Array index names
17             # Do not combine with other BEGIN blocks (c101).
18 44     44   45607 my $i = 0;
19             use constant {
20 44         5634 _spaces_ => $i++,
21             _level_ => $i++,
22             _ci_level_ => $i++,
23             _available_spaces_ => $i++,
24             _closed_ => $i++,
25             _comma_count_ => $i++,
26             _lp_item_index_ => $i++,
27             _have_child_ => $i++,
28             _recoverable_spaces_ => $i++,
29             _align_seqno_ => $i++,
30             _marked_ => $i++,
31             _K_begin_line_ => $i++,
32             _arrow_count_ => $i++,
33             _standard_spaces_ => $i++,
34             _K_extra_space_ => $i++,
35 44     44   232 };
  44         78  
36             } ## end BEGIN
37              
38             sub AUTOLOAD {
39              
40             # Catch any undefined sub calls so that we are sure to get
41             # some diagnostic information. This sub should never be called
42             # except for a programming error.
43 0     0   0 our $AUTOLOAD;
44 0 0       0 return if ( $AUTOLOAD =~ /\bDESTROY$/ );
45 0         0 my ( $pkg, $fname, $lno ) = caller();
46 0         0 my $my_package = __PACKAGE__;
47 0         0 print {*STDERR} <<EOM;
  0         0  
48             ======================================================================
49             Error detected in package '$my_package', version $VERSION
50             Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
51             Called from package: '$pkg'
52             Called from File '$fname' at line '$lno'
53             This error is probably due to a recent programming change
54             ======================================================================
55             EOM
56 0         0 exit 1;
57             } ## end sub AUTOLOAD
58              
59       0     sub DESTROY {
60              
61             # required to avoid call to AUTOLOAD in some versions of perl
62             }
63              
64             sub new {
65              
66             # Create an 'indentation_item' which describes one level of leading
67             # whitespace when the '-lp' indentation is used.
68 623     623 0 3648 my ( $class, %input_hash ) = @_;
69              
70             # DEFINITIONS:
71             # spaces => # total leading white spaces
72             # level => # the indentation 'level'
73             # ci_level => # the 'continuation level'
74             # available_spaces => # how many left spaces available
75             # # for this level
76             # closed => # index where we saw closing '}'
77             # comma_count => # how many commas at this level?
78             # lp_item_index => # index in output batch list
79             # have_child => # any dependents?
80             # recoverable_spaces => # how many spaces to the right
81             # # we would like to move to get
82             # # alignment (negative if left)
83             # align_seqno => # if we are aligning with an opening structure,
84             # # this is its seqno
85             # marked => # if visited by corrector logic
86             # K_begin_line => # first token index K of this level
87             # arrow_count => # how many =>'s
88              
89 623         904 my $self = [];
90 623         923 bless $self, $class;
91              
92 623         1194 $self->[_spaces_] = $input_hash{spaces};
93 623         1016 $self->[_level_] = $input_hash{level};
94 623         802 $self->[_ci_level_] = $input_hash{ci_level};
95 623         818 $self->[_available_spaces_] = $input_hash{available_spaces};
96 623         918 $self->[_closed_] = -1;
97 623         995 $self->[_comma_count_] = 0;
98 623         899 $self->[_lp_item_index_] = $input_hash{lp_item_index};
99 623         765 $self->[_have_child_] = 0;
100 623         851 $self->[_recoverable_spaces_] = 0;
101 623         909 $self->[_align_seqno_] = $input_hash{align_seqno};
102 623         934 $self->[_marked_] = 0;
103 623         844 $self->[_K_begin_line_] = $input_hash{K_begin_line};
104 623         984 $self->[_arrow_count_] = 0;
105 623         796 $self->[_standard_spaces_] = $input_hash{standard_spaces};
106 623         797 $self->[_K_extra_space_] = $input_hash{K_extra_space};
107              
108 623         1736 return $self;
109             } ## end sub new
110              
111             sub permanently_decrease_available_spaces {
112              
113             # make a permanent reduction in the available indentation spaces
114             # at one indentation item. NOTE: if there are child nodes, their
115             # total SPACES must be reduced by the caller.
116              
117 280     280 0 423 my ( $self, $spaces_needed ) = @_;
118 280         427 my $available_spaces = $self->get_available_spaces();
119 280 100       439 my $deleted_spaces =
120             ( $available_spaces > $spaces_needed )
121             ? $spaces_needed
122             : $available_spaces;
123              
124             # Fixed for c085; a zero value must remain unchanged unless the closed
125             # flag has been set.
126 280         456 my $closed = $self->get_closed();
127 280 100 100     885 $self->decrease_available_spaces($deleted_spaces)
128             if ( $available_spaces != 0 || $closed >= 0 );
129 280         508 $self->decrease_SPACES($deleted_spaces);
130 280         566 $self->set_recoverable_spaces(0);
131              
132 280         590 return $deleted_spaces;
133             } ## end sub permanently_decrease_available_spaces
134              
135             sub tentatively_decrease_available_spaces {
136              
137             # We are asked to tentatively delete $spaces_needed of indentation
138             # for an indentation item. We may want to undo this later. NOTE: if
139             # there are child nodes, their total SPACES must be reduced by the
140             # caller.
141 72     72 0 122 my ( $self, $spaces_needed ) = @_;
142 72         132 my $available_spaces = $self->get_available_spaces();
143 72 100       150 my $deleted_spaces =
144             ( $available_spaces > $spaces_needed )
145             ? $spaces_needed
146             : $available_spaces;
147 72         209 $self->decrease_available_spaces($deleted_spaces);
148 72         176 $self->decrease_SPACES($deleted_spaces);
149 72         190 $self->increase_recoverable_spaces($deleted_spaces);
150 72         108 return $deleted_spaces;
151             } ## end sub tentatively_decrease_available_spaces
152              
153             # time-critical sub
154             sub get_spaces {
155 6829     6829 0 10983 return $_[0]->[_spaces_];
156             }
157              
158             sub get_standard_spaces {
159 57     57 0 85 my $self = shift;
160 57         125 return $self->[_standard_spaces_];
161             }
162              
163             # time-critical sub
164             sub get_marked {
165 3261     3261 0 5356 return $_[0]->[_marked_];
166             }
167              
168             sub set_marked {
169 623     623 0 785 my ( $self, $value ) = @_;
170 623 50       924 if ( defined($value) ) {
171 623         737 $self->[_marked_] = $value;
172             }
173 623         824 return $self->[_marked_];
174             } ## end sub set_marked
175              
176             sub get_available_spaces {
177 1030     1030 0 1223 my $self = shift;
178 1030         1492 return $self->[_available_spaces_];
179             }
180              
181             sub decrease_SPACES {
182 884     884 0 990 my ( $self, $value ) = @_;
183 884 50       1128 if ( defined($value) ) {
184 884         963 $self->[_spaces_] -= $value;
185             }
186 884         1088 return $self->[_spaces_];
187             } ## end sub decrease_SPACES
188              
189             sub decrease_available_spaces {
190 352     352 0 468 my ( $self, $value ) = @_;
191              
192 352 50       551 if ( defined($value) ) {
193 352         443 $self->[_available_spaces_] -= $value;
194             }
195 352         428 return $self->[_available_spaces_];
196             } ## end sub decrease_available_spaces
197              
198             sub get_align_seqno {
199 623     623 0 717 my $self = shift;
200 623         1094 return $self->[_align_seqno_];
201             }
202              
203             sub get_recoverable_spaces {
204 303     303 0 412 my $self = shift;
205 303         906 return $self->[_recoverable_spaces_];
206             }
207              
208             sub set_recoverable_spaces {
209 445     445 0 699 my ( $self, $value ) = @_;
210 445 50       733 if ( defined($value) ) {
211 445         574 $self->[_recoverable_spaces_] = $value;
212             }
213 445         679 return $self->[_recoverable_spaces_];
214             } ## end sub set_recoverable_spaces
215              
216             sub increase_recoverable_spaces {
217 72     72 0 128 my ( $self, $value ) = @_;
218 72 50       136 if ( defined($value) ) {
219 72         106 $self->[_recoverable_spaces_] += $value;
220             }
221 72         92 return $self->[_recoverable_spaces_];
222             } ## end sub increase_recoverable_spaces
223              
224             sub get_ci_level {
225 0     0 0 0 my $self = shift;
226 0         0 return $self->[_ci_level_];
227             }
228              
229             sub get_level {
230 0     0 0 0 my $self = shift;
231 0         0 return $self->[_level_];
232             }
233              
234             sub get_spaces_level_ci {
235 1333     1333 0 1559 my $self = shift;
236 1333         3849 return [ $self->[_spaces_], $self->[_level_], $self->[_ci_level_] ];
237             }
238              
239             sub get_lp_item_index {
240 57     57 0 84 my $self = shift;
241 57         108 return $self->[_lp_item_index_];
242             }
243              
244             sub get_K_begin_line {
245 736     736 0 896 my $self = shift;
246 736         1183 return $self->[_K_begin_line_];
247             }
248              
249             sub get_K_extra_space {
250 30     30 0 65 my $self = shift;
251 30         64 return $self->[_K_extra_space_];
252             }
253              
254             sub set_have_child {
255 506     506 0 648 my ( $self, $value ) = @_;
256 506 50       813 if ( defined($value) ) {
257 506         644 $self->[_have_child_] = $value;
258             }
259 506         677 return $self->[_have_child_];
260             } ## end sub set_have_child
261              
262             sub get_have_child {
263 70     70 0 104 my $self = shift;
264 70         119 return $self->[_have_child_];
265             }
266              
267             sub set_arrow_count {
268 623     623 0 838 my ( $self, $value ) = @_;
269 623 50       977 if ( defined($value) ) {
270 623         722 $self->[_arrow_count_] = $value;
271             }
272 623         821 return $self->[_arrow_count_];
273             } ## end sub set_arrow_count
274              
275             sub get_arrow_count {
276 69     69 0 110 my $self = shift;
277 69         100 return $self->[_arrow_count_];
278             }
279              
280             sub set_comma_count {
281 623     623 0 892 my ( $self, $value ) = @_;
282 623 50       1067 if ( defined($value) ) {
283 623         825 $self->[_comma_count_] = $value;
284             }
285 623         835 return $self->[_comma_count_];
286             } ## end sub set_comma_count
287              
288             sub get_comma_count {
289 69     69 0 102 my $self = shift;
290 69         117 return $self->[_comma_count_];
291             }
292              
293             sub set_closed {
294 623     623 0 858 my ( $self, $value ) = @_;
295 623 50       1046 if ( defined($value) ) {
296 623         798 $self->[_closed_] = $value;
297             }
298 623         896 return $self->[_closed_];
299             } ## end sub set_closed
300              
301             sub get_closed {
302 1154     1154 0 1349 my $self = shift;
303 1154         2234 return $self->[_closed_];
304             }
305             1;