1 #####################################################################
3 # the Perl::Tidy::IndentationItem class supplies items which contain
4 # how much whitespace should be used at the start of a line
6 #####################################################################
8 package Perl::Tidy::IndentationItem;
11 our $VERSION = '20220217';
16 # Do not combine with other BEGIN blocks (c101).
22 _available_spaces_ => $i++,
24 _comma_count_ => $i++,
25 _lp_item_index_ => $i++,
27 _recoverable_spaces_ => $i++,
28 _align_seqno_ => $i++,
30 _stack_depth_ => $i++,
31 _K_begin_line_ => $i++,
32 _arrow_count_ => $i++,
38 # Catch any undefined sub calls so that we are sure to get
39 # some diagnostic information. This sub should never be called
40 # except for a programming error.
42 return if ( $AUTOLOAD =~ /\bDESTROY$/ );
43 my ( $pkg, $fname, $lno ) = caller();
44 my $my_package = __PACKAGE__;
46 ======================================================================
47 Error detected in package '$my_package', version $VERSION
48 Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
49 Called from package: '$pkg'
50 Called from File '$fname' at line '$lno'
51 This error is probably due to a recent programming change
52 ======================================================================
59 # required to avoid call to AUTOLOAD in some versions of perl
64 # Create an 'indentation_item' which describes one level of leading
65 # whitespace when the '-lp' indentation is used.
66 my ( $class, %input_hash ) = @_;
69 # spaces => # total leading white spaces
70 # level => # the indentation 'level'
71 # ci_level => # the 'continuation level'
72 # available_spaces => # how many left spaces available
74 # closed => # index where we saw closing '}'
75 # comma_count => # how many commas at this level?
76 # lp_item_index => # index in output batch list
77 # have_child => # any dependents?
78 # recoverable_spaces => # how many spaces to the right
79 # # we would like to move to get
80 # # alignment (negative if left)
81 # align_seqno => # if we are aligning with an opening structure,
83 # marked => # if visited by corrector logic
84 # stack_depth => # indentation nesting depth
85 # K_begin_line => # first token index K of this level
86 # arrow_count => # how many =>'s
89 $self->[_spaces_] = $input_hash{spaces};
90 $self->[_level_] = $input_hash{level};
91 $self->[_ci_level_] = $input_hash{ci_level};
92 $self->[_available_spaces_] = $input_hash{available_spaces};
93 $self->[_closed_] = -1;
94 $self->[_comma_count_] = 0;
95 $self->[_lp_item_index_] = $input_hash{lp_item_index};
96 $self->[_have_child_] = 0;
97 $self->[_recoverable_spaces_] = 0;
98 $self->[_align_seqno_] = $input_hash{align_seqno};
99 $self->[_marked_] = 0;
100 $self->[_stack_depth_] = $input_hash{stack_depth};
101 $self->[_K_begin_line_] = $input_hash{K_begin_line};
102 $self->[_arrow_count_] = 0;
108 sub permanently_decrease_available_spaces {
110 # make a permanent reduction in the available indentation spaces
111 # at one indentation item. NOTE: if there are child nodes, their
112 # total SPACES must be reduced by the caller.
114 my ( $item, $spaces_needed ) = @_;
115 my $available_spaces = $item->get_available_spaces();
117 ( $available_spaces > $spaces_needed )
121 # Fixed for c085; a zero value must remain unchanged unless the closed
123 my $closed = $item->get_closed();
124 $item->decrease_available_spaces($deleted_spaces)
125 unless ( $available_spaces == 0 && $closed < 0 );
126 $item->decrease_SPACES($deleted_spaces);
127 $item->set_recoverable_spaces(0);
129 return $deleted_spaces;
132 sub tentatively_decrease_available_spaces {
134 # We are asked to tentatively delete $spaces_needed of indentation
135 # for an indentation item. We may want to undo this later. NOTE: if
136 # there are child nodes, their total SPACES must be reduced by the
138 my ( $item, $spaces_needed ) = @_;
139 my $available_spaces = $item->get_available_spaces();
141 ( $available_spaces > $spaces_needed )
144 $item->decrease_available_spaces($deleted_spaces);
145 $item->decrease_SPACES($deleted_spaces);
146 $item->increase_recoverable_spaces($deleted_spaces);
147 return $deleted_spaces;
150 sub get_stack_depth {
151 return $_[0]->[_stack_depth_];
155 return $_[0]->[_spaces_];
159 return $_[0]->[_marked_];
163 my ( $self, $value ) = @_;
164 if ( defined($value) ) {
165 $self->[_marked_] = $value;
167 return $self->[_marked_];
170 sub get_available_spaces {
171 return $_[0]->[_available_spaces_];
174 sub decrease_SPACES {
175 my ( $self, $value ) = @_;
176 if ( defined($value) ) {
177 $self->[_spaces_] -= $value;
179 return $self->[_spaces_];
182 sub decrease_available_spaces {
183 my ( $self, $value ) = @_;
184 if ( defined($value) ) {
185 $self->[_available_spaces_] -= $value;
187 return $self->[_available_spaces_];
190 sub get_align_seqno {
191 return $_[0]->[_align_seqno_];
194 sub get_recoverable_spaces {
195 return $_[0]->[_recoverable_spaces_];
198 sub set_recoverable_spaces {
199 my ( $self, $value ) = @_;
200 if ( defined($value) ) {
201 $self->[_recoverable_spaces_] = $value;
203 return $self->[_recoverable_spaces_];
206 sub increase_recoverable_spaces {
207 my ( $self, $value ) = @_;
208 if ( defined($value) ) {
209 $self->[_recoverable_spaces_] += $value;
211 return $self->[_recoverable_spaces_];
215 return $_[0]->[_ci_level_];
219 return $_[0]->[_level_];
222 sub get_spaces_level_ci {
224 return [ $self->[_spaces_], $self->[_level_], $self->[_ci_level_] ];
227 sub get_lp_item_index {
228 return $_[0]->[_lp_item_index_];
231 sub get_K_begin_line {
232 return $_[0]->[_K_begin_line_];
236 my ( $self, $value ) = @_;
237 if ( defined($value) ) {
238 $self->[_have_child_] = $value;
240 return $self->[_have_child_];
244 return $_[0]->[_have_child_];
247 sub set_arrow_count {
248 my ( $self, $value ) = @_;
249 if ( defined($value) ) {
250 $self->[_arrow_count_] = $value;
252 return $self->[_arrow_count_];
255 sub get_arrow_count {
256 return $_[0]->[_arrow_count_];
259 sub set_comma_count {
260 my ( $self, $value ) = @_;
261 if ( defined($value) ) {
262 $self->[_comma_count_] = $value;
264 return $self->[_comma_count_];
267 sub get_comma_count {
268 return $_[0]->[_comma_count_];
272 my ( $self, $value ) = @_;
273 if ( defined($value) ) {
274 $self->[_closed_] = $value;
276 return $self->[_closed_];
280 return $_[0]->[_closed_];