File Coverage

blib/lib/Perl/Tidy/IndentationItem.pm
Criterion Covered Total %
statement 95 105 90.4
branch 15 26 57.6
condition 3 3 100.0
subroutine 30 34 88.2
pod 0 28 0.0
total 143 196 72.9


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 39     39   311 use strict;
  39         100  
  39         1789  
10 39     39   258 use warnings;
  39         153  
  39         4507  
11             our $VERSION = '20230909';
12              
13 0         0 BEGIN {
14              
15             # Array index names
16             # Do not combine with other BEGIN blocks (c101).
17 39     39   58259 my $i = 0;
18             use constant {
19 39         6857 _spaces_ => $i++,
20             _level_ => $i++,
21             _ci_level_ => $i++,
22             _available_spaces_ => $i++,
23             _closed_ => $i++,
24             _comma_count_ => $i++,
25             _lp_item_index_ => $i++,
26             _have_child_ => $i++,
27             _recoverable_spaces_ => $i++,
28             _align_seqno_ => $i++,
29             _marked_ => $i++,
30             _stack_depth_ => $i++,
31             _K_begin_line_ => $i++,
32             _arrow_count_ => $i++,
33             _standard_spaces_ => $i++,
34             _K_extra_space_ => $i++,
35 39     39   309 };
  39         140  
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 608     608 0 4063 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 608         1424 my $self = [];
90 608         1407 $self->[_spaces_] = $input_hash{spaces};
91 608         1096 $self->[_level_] = $input_hash{level};
92 608         961 $self->[_ci_level_] = $input_hash{ci_level};
93 608         1007 $self->[_available_spaces_] = $input_hash{available_spaces};
94 608         1165 $self->[_closed_] = -1;
95 608         1079 $self->[_comma_count_] = 0;
96 608         1128 $self->[_lp_item_index_] = $input_hash{lp_item_index};
97 608         1022 $self->[_have_child_] = 0;
98 608         1054 $self->[_recoverable_spaces_] = 0;
99 608         1079 $self->[_align_seqno_] = $input_hash{align_seqno};
100 608         1235 $self->[_marked_] = 0;
101 608         1230 $self->[_K_begin_line_] = $input_hash{K_begin_line};
102 608         982 $self->[_arrow_count_] = 0;
103 608         1052 $self->[_standard_spaces_] = $input_hash{standard_spaces};
104 608         1355 $self->[_K_extra_space_] = $input_hash{K_extra_space};
105              
106 608         1117 bless $self, $class;
107 608         2066 return $self;
108             } ## end sub new
109              
110             sub permanently_decrease_available_spaces {
111              
112             # make a permanent reduction in the available indentation spaces
113             # at one indentation item. NOTE: if there are child nodes, their
114             # total SPACES must be reduced by the caller.
115              
116 285     285 0 545 my ( $item, $spaces_needed ) = @_;
117 285         537 my $available_spaces = $item->get_available_spaces();
118 285 100       599 my $deleted_spaces =
119             ( $available_spaces > $spaces_needed )
120             ? $spaces_needed
121             : $available_spaces;
122              
123             # Fixed for c085; a zero value must remain unchanged unless the closed
124             # flag has been set.
125 285         685 my $closed = $item->get_closed();
126 285 100 100     1092 $item->decrease_available_spaces($deleted_spaces)
127             if ( $available_spaces != 0 || $closed >= 0 );
128 285         648 $item->decrease_SPACES($deleted_spaces);
129 285         678 $item->set_recoverable_spaces(0);
130              
131 285         728 return $deleted_spaces;
132             } ## end sub permanently_decrease_available_spaces
133              
134             sub tentatively_decrease_available_spaces {
135              
136             # We are asked to tentatively delete $spaces_needed of indentation
137             # for an indentation item. We may want to undo this later. NOTE: if
138             # there are child nodes, their total SPACES must be reduced by the
139             # caller.
140 71     71 0 175 my ( $item, $spaces_needed ) = @_;
141 71         167 my $available_spaces = $item->get_available_spaces();
142 71 100       207 my $deleted_spaces =
143             ( $available_spaces > $spaces_needed )
144             ? $spaces_needed
145             : $available_spaces;
146 71         270 $item->decrease_available_spaces($deleted_spaces);
147 71         235 $item->decrease_SPACES($deleted_spaces);
148 71         217 $item->increase_recoverable_spaces($deleted_spaces);
149 71         172 return $deleted_spaces;
150             } ## end sub tentatively_decrease_available_spaces
151              
152             sub get_spaces {
153 6462     6462 0 13179 return $_[0]->[_spaces_];
154             }
155              
156             sub get_standard_spaces {
157 25     25 0 51 return $_[0]->[_standard_spaces_];
158             }
159              
160             sub get_marked {
161 3065     3065 0 6927 return $_[0]->[_marked_];
162             }
163              
164             sub set_marked {
165 608     608 0 1033 my ( $self, $value ) = @_;
166 608 50       1181 if ( defined($value) ) {
167 608         910 $self->[_marked_] = $value;
168             }
169 608         1026 return $self->[_marked_];
170             } ## end sub set_marked
171              
172             sub get_available_spaces {
173 1018     1018 0 1982 return $_[0]->[_available_spaces_];
174             }
175              
176             sub decrease_SPACES {
177 885     885 0 1388 my ( $self, $value ) = @_;
178 885 50       1571 if ( defined($value) ) {
179 885         1225 $self->[_spaces_] -= $value;
180             }
181 885         1514 return $self->[_spaces_];
182             } ## end sub decrease_SPACES
183              
184             sub decrease_available_spaces {
185 356     356 0 643 my ( $self, $value ) = @_;
186              
187 356 50       704 if ( defined($value) ) {
188 356         566 $self->[_available_spaces_] -= $value;
189             }
190 356         540 return $self->[_available_spaces_];
191             } ## end sub decrease_available_spaces
192              
193             sub get_align_seqno {
194 608     608 0 1355 return $_[0]->[_align_seqno_];
195             }
196              
197             sub get_recoverable_spaces {
198 300     300 0 1060 return $_[0]->[_recoverable_spaces_];
199             }
200              
201             sub set_recoverable_spaces {
202 445     445 0 783 my ( $self, $value ) = @_;
203 445 50       913 if ( defined($value) ) {
204 445         724 $self->[_recoverable_spaces_] = $value;
205             }
206 445         749 return $self->[_recoverable_spaces_];
207             } ## end sub set_recoverable_spaces
208              
209             sub increase_recoverable_spaces {
210 71     71 0 153 my ( $self, $value ) = @_;
211 71 50       175 if ( defined($value) ) {
212 71         157 $self->[_recoverable_spaces_] += $value;
213             }
214 71         123 return $self->[_recoverable_spaces_];
215             } ## end sub increase_recoverable_spaces
216              
217             sub get_ci_level {
218 0     0 0 0 return $_[0]->[_ci_level_];
219             }
220              
221             sub get_level {
222 0     0 0 0 return $_[0]->[_level_];
223             }
224              
225             sub get_spaces_level_ci {
226 1306     1306 0 2158 my $self = shift;
227 1306         4666 return [ $self->[_spaces_], $self->[_level_], $self->[_ci_level_] ];
228             }
229              
230             sub get_lp_item_index {
231 62     62 0 156 return $_[0]->[_lp_item_index_];
232             }
233              
234             sub get_K_begin_line {
235 717     717 0 1523 return $_[0]->[_K_begin_line_];
236             }
237              
238             sub get_K_extra_space {
239 28     28 0 66 return $_[0]->[_K_extra_space_];
240             }
241              
242             sub set_have_child {
243 498     498 0 924 my ( $self, $value ) = @_;
244 498 50       1059 if ( defined($value) ) {
245 498         793 $self->[_have_child_] = $value;
246             }
247 498         909 return $self->[_have_child_];
248             } ## end sub set_have_child
249              
250             sub get_have_child {
251 69     69 0 187 return $_[0]->[_have_child_];
252             }
253              
254             sub set_arrow_count {
255 608     608 0 1072 my ( $self, $value ) = @_;
256 608 50       1245 if ( defined($value) ) {
257 608         974 $self->[_arrow_count_] = $value;
258             }
259 608         1056 return $self->[_arrow_count_];
260             } ## end sub set_arrow_count
261              
262             sub get_arrow_count {
263 68     68 0 143 return $_[0]->[_arrow_count_];
264             }
265              
266             sub set_comma_count {
267 608     608 0 1041 my ( $self, $value ) = @_;
268 608 50       1245 if ( defined($value) ) {
269 608         956 $self->[_comma_count_] = $value;
270             }
271 608         1094 return $self->[_comma_count_];
272             } ## end sub set_comma_count
273              
274             sub get_comma_count {
275 68     68 0 157 return $_[0]->[_comma_count_];
276             }
277              
278             sub set_closed {
279 608     608 0 1108 my ( $self, $value ) = @_;
280 608 50       1275 if ( defined($value) ) {
281 608         995 $self->[_closed_] = $value;
282             }
283 608         1102 return $self->[_closed_];
284             } ## end sub set_closed
285              
286             sub get_closed {
287 1142     1142 0 2518 return $_[0]->[_closed_];
288             }
289             1;