1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
# An update to the basic Perl::Tidy continuation indentation model
Perl::Tidy version 20230701 has several changes in the basic method for
computing "continuation indentation". This has been on the TODO list
for a long time. The changes mainly apply to some unusual situations,
and most programs will remain unchanged. This note explains what the changes
are and why they are needed.
To briefly review, the indentation of a line is the sum of two parts:
(1) **structural indentation**, and (2) **continuation indentation**.
These are occasionally called primary and secondary indentation.
**Structural indentation** is introduced by opening container tokens
**{**, **(**, or **[**. Default structural indentation is 4 characters by
default but can be changed with the **-i=n** parameter. The total structural
indentation is easily determined by keeping a stack of the opening tokens which
contain a given line.
**Continuation indentation** is introduced to help show structure in multi-line
statements, list items, and logical expressions. The first line of such long
lines usually starts with the basic structural indentation. Subsequent
lines are given the additional continuation indentation to emphasize that
they are a continuation of the statement.
The default continuation indentation is 2 characters but this can be changed
with the **-ci=n** parameter.
Previously, computation of continuation indentation was done in the
initial pass through a file, and this placed some limits on what it could do.
This computation has been moved downstream in the processing pipeline, where the
entire file is accessible with full data structures, and this allows several
improvements to be made. These mainly involve (1) the continuation
indentation assigned to comments in unusual circumstances, or (2) the
indentation of complex ternary expressions, or (3) the indentation of
chains of ``sort/map/grep`` blocks. Some examples are as follows.
## Block comment indentation changes before closing braces, brackets and parens
The indentation of one-line comments, also called block comments, which
appear near the end of a containing structure are now independent of the
existence of any optional trailing comma or semicolon.
To illustrate the issue, consider the following example, in which the last
statement is not terminated with a semicolon. Previously, the subsequent
comments would have continuation indentation, since the statement is not
terminated:
```
BEGIN {
$my_hash{'word1'} = 1;
$my_hash{'word2'} = 1
# comment
# ...
}
```
In the updated version, since the final semicolon is optional, the
comments do not have the continuation indentation:
```
BEGIN {
$my_hash{'word1'} = 1;
$my_hash{'word2'} = 1
# comment
# ...
}
```
This makes the comments have the same indentation as if there were a
terminal semicolon. This update keeps large blocks of comments from shifting
when an optional trailing semicolon or comma is added or removed.
## Closing brace indentation changes
A related issue which has been fixed is illustrated with the following
example which shows the previous formatting:
```
if ( $term->ReadLine eq "Term::ReadLine::Gnu" ) {
my $attribs = $term->Attribs;
$attribs->{attempted_completion_function} = sub {
&CPAN::Complete::gnu_cpl;
}
# comment
# comment
}
```
Here again, an optional terminal semicolon is missing after the closing sub
brace, and there are some comments before the closing ``if`` block brace. The
previous logic had a limited look-ahead ability, and in this case the
continuation indentation of the closing sub brace was not removed.
The updated logic fixes this problem:
```
if ( $term->ReadLine eq "Term::ReadLine::Gnu" ) {
my $attribs = $term->Attribs;
$attribs->{attempted_completion_function} = sub {
&CPAN::Complete::gnu_cpl;
}
# comment
# comment
}
```
## Block comment indentation changes in ternary statements
Another change is that the indentation of block comments within ternary
statements is improved. These can be difficult to format. For example,
here is the old default formatting of a complex ternary with lots of comments:
```
# a) under an interactive shell?
my $rl_avail = ( !$term->isa('CPANPLUS::Shell::_Faked') )
# b) do we have a tty terminal?
? ( -t STDIN )
# c) should we enable the term?
? ( !$self->__is_bad_terminal($term) )
# d) external modules available?
? ( $term->ReadLine ne "Term::ReadLine::Stub" )
# a+b+c+d => "Smart" terminal
? loc("enabled")
# a+b+c => "Stub" terminal
: loc("available (try 'i Term::ReadLine::Perl')")
# a+b => "Bad" terminal
: loc("disabled")
# a => "Dumb" terminal
: loc("suppressed")
# none => "Faked" terminal
: loc("suppressed in batch mode");
```
The comment indentation is very poor here. Here is the new formatting:
```
# a) under an interactive shell?
my $rl_avail = ( !$term->isa('CPANPLUS::Shell::_Faked') )
# b) do we have a tty terminal?
? ( -t STDIN )
# c) should we enable the term?
? ( !$self->__is_bad_terminal($term) )
# d) external modules available?
? ( $term->ReadLine ne "Term::ReadLine::Stub" )
# a+b+c+d => "Smart" terminal
? loc("enabled")
# a+b+c => "Stub" terminal
: loc("available (try 'i Term::ReadLine::Perl')")
# a+b => "Bad" terminal
: loc("disabled")
# a => "Dumb" terminal
: loc("suppressed")
# none => "Faked" terminal
: loc("suppressed in batch mode");
```
## Improved indentation for some nested welds.
An issue has been fixed involving cases where the **--weld-nested**, or **-wn**
parameter was used on comma-separated lists of items at block level (paren-less
lists). For example, here is the old default formatting with the **-wn**
parameter.
```
is_deeply $fixer->fix( {
demo => { nl => 'Tuin der lusten', en => 'The Garden of Earthly Delights' }
} ),
{
demo => { NL => 'TUIN DER LUSTEN', en => 'The Garden of Earthly Delights' },
titles => ['The Garden of Earthly Delights']
},
'specific testing';
```
The closing '} )' is missing some continuation indentation. The new default
formatting is
```
is_deeply $fixer->fix( {
demo => { nl => 'Tuin der lusten', en => 'The Garden of Earthly Delights' }
} ),
{
demo => { NL => 'TUIN DER LUSTEN', en => 'The Garden of Earthly Delights' },
titles => ['The Garden of Earthly Delights']
},
'specific testing';
```
## Problems with excess continuation indentation
A very rare problem has been fixed in which excess indentation could occur.
This is illustrated in the following example which is run with **-ci=4** to
emphasize the problem:
```
( $foo, $dayC[$cnt], $foo ) = split /;/,
$slist[
&UnixDate(
&ParseDate(
$week_name[ $cnt - 1 ]
. " week "
. $uweek . " "
. $this_year
),
"%j"
) - 1
];
```
The problem is that the lines with leading dots have twice the
amount of indentation that they should. The new version fixes this:
```
( $foo, $dayC[$cnt], $foo ) = split /;/,
$slist[
&UnixDate(
&ParseDate(
$week_name[ $cnt - 1 ]
. " week "
. $uweek . " "
. $this_year
),
"%j"
) - 1
];
```
Here is another example, also run with **-ci=4** for emphasis:
```
$a
? $b
? $c
? $d
? $e
: $f
: $g
: $h
: print "hello\n";
```
Note how $e and $f have excess indentation. The updated version is:
```
$a
? $b
? $c
? $d
? $e
: $f
: $g
: $h
: print "hello\n";
```
## Some problems with indentation in ternary expressions
The continuation indentation in some complex ternary statements has been
improved. For example, in the following old formatting the lines beginning
with ``&&`` lack continuation indentation:
```
if (
$file eq '-' ? open(PHONES, '<&STDIN')
: $file =~ /\.Z$/ ? open(PHONES, "zcat '$file' 2>/dev/null |")
: $file =~ /\.pgp$/ ? $usepgp
&& length($ENV{PGPPASS})
&& open(PHONES, "pgp -fd <'$file' |")
: open(PHONES, "< $file\0")
)
{
}
```
The updated version adds indentation to these lines to help indicate that
they are a continuation of the previous line.
```
if (
$file eq '-' ? open(PHONES, '<&STDIN')
: $file =~ /\.Z$/ ? open(PHONES, "zcat '$file' 2>/dev/null |")
: $file =~ /\.pgp$/ ? $usepgp
&& length($ENV{PGPPASS})
&& open(PHONES, "pgp -fd <'$file' |")
: open(PHONES, "< $file\0")
)
{
}
```
## Some improved indentation of filter block chains
The lines of an isolated chain of ``sort/map/grep`` blocks are normally all
given the same indentation. For example
```
@new_in_dir = (
grep { not $seen{$_} }
map { $dir . "/" . $_ }
grep { not ignore_file($_) }
grep { not $skip{$_} } readdir(D)
);
```
Previously, there were a a number of situations where this could not be
achieved. As an example, if the above example had side comments then the
formatting would be
```
@new_in_dir = (
grep { not $seen{$_} } # files not yet processed
map { $dir . "/" . $_ } # map from file to dir/file
grep { not ignore_file($_) } # ignore files in cvsignore
grep { not $skip{$_} } # skip files to be ignored
readdir(D)
);
```
The first line now has a different indentation from the rest, and this is
undesirable because ideally indentation should be independent of the existence
of side comments. The new version handles this correctly:
```
@new_in_dir = (
grep { not $seen{$_} } # files not yet processed
map { $dir . "/" . $_ } # map from file to dir/file
grep { not ignore_file($_) } # ignore files in cvsignore
grep { not $skip{$_} } # skip files to be ignored
readdir(D)
);
```
A related change is that some undesirable alignments across changes in
continuation indentation have been removed. For example, here is an
example of this issue as previously formatted:
```
print $fh map { $_->[0] }
sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] }
map { my $f = lc $_; $f =~ s/[^a-z0-9\s]//g; [ $_, $f ] } @manifest;
```
The alignment of the ``map`` and ``sort`` braces produces an undesirable
gap. The revised formatting avoids this:
```
print $fh map { $_->[0] }
sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] }
map { my $f = lc $_; $f =~ s/[^a-z0-9\s]//g; [ $_, $f ] } @manifest;
```
|