From d5f790a37201ffcc98f7c420210a04d9bf406575 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Thu, 2 May 2024 16:41:34 -0700 Subject: [PATCH] minor code cleanups and update docs --- CHANGES.md | 105 +++++++++++++++++++------------------ bin/perltidy | 27 +++++++--- lib/Perl/Tidy/Formatter.pm | 45 +++++++--------- 3 files changed, 93 insertions(+), 84 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ee947a9a..ce6d0200 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,56 @@ ## 2024 02 02.07 + - The option --valign-signed-numbers, or -vsn is now the default. It + was introduced in the previous release has been found to significantly + improve the overall appearance of columns of signed and unsigned + numbers. It will change formatting slightly in scripts with columns + of vertically aligned numbers, and can be deactivated with -nvsn. + + - Previously, a line break was made before a short concatenated terminal + quoted string, such as "\n", if the previous line had a greater + starting indentation. The break is now placed after the short quote. + This keeps code a little more compact. For example: + + # old rule: break before "\n" here because '$name' has more indentation: + my $html = $this->SUPER::genObject( $query, $bindNode, $field . ":$var", + $name, "remove", "UNCHECKED" ) + . "\n"; + + # new rule: break after a short terminal quote like "\n" for compactness; + my $html = $this->SUPER::genObject( $query, $bindNode, $field . ":$var", + $name, "remove", "UNCHECKED" ) . "\n"; + + - The option --delete-repeated-commas is now the default. + + It makes the following checks and changes: + - Repeated commas like ',,' are removed with a warning + - Repeated fat commas like '=> =>' are removed with a warning + - The combination '=>,' produces a warning but is not changed + These warnings are only output if --warning-output, or -w, is set. + + Use --nodelete-repeated-commas, or -ndrc, to retain repeated commas. + + - The operator ``**=`` now has spaces on both sides by default. Previously, + there was no space on the left. This change makes its spacing the same + as all other assignment operators. The previous behavior can be obtained + with the parameter setting -nwls='**='. + + - The option --file-size-order, or -fso is now the default. When + perltidy is given a list of multiple filenames to process, they + are sorted by size and processed in order of increasing size. + This can significantly reduce memory usage by Perl. This + option has always been used in testing, where typically several + jobs each operating on thousands of filenames are running at the + same time and competing for system resources. If this option + is not wanted for some reason, it can be deactivated with -nfso. + + - In the option --dump-block-summary, the number of sub arguments indicated + for each sub now includes any leading object variable passed with + an arrow-operator call. Previously the count would have been decreased + by one in this case. This change is needed for compatibility with future + updates. + - Fix issue git #138 involving -xlp (--extended-line-up-parentheses). When multiple-line quotes and regexes have long secondary lines, these line lengths could influencing some spacing and indentation, but they @@ -47,12 +97,13 @@ perltidy -wma somefile.pl - The -warn version may be customized with three additional parameters if - necessary to avoid needless warnings: + The -warn version may be customized with the following additional parameters + if necessary to avoid needless warnings: --warn-mismatched-arg-types=s (or -wmat=s), --warn-mismatched-arg-exclusion-list=s (or -wmaxl=s), and --warn-mismatched-arg-undercount-cutoff=n (or -wmauc=n). + --warn-mismatched-arg-overcount-cutoff=n (or -wmaoc=n). These are explained in the manual. @@ -82,56 +133,6 @@ This option currently is off by default to avoid changing existing formatting. - - Previously, a line break was made before a short concatenated terminal - quoted string, such as "\n", if the previous line had a greater - starting indentation. The break is now placed after the short quote. - This keeps code a little more compact. For example: - - # old rule: break before "\n" here because '$name' has more indentation: - my $html = $this->SUPER::genObject( $query, $bindNode, $field . ":$var", - $name, "remove", "UNCHECKED" ) - . "\n"; - - # new rule: break after a short terminal quote like "\n" for compactness; - my $html = $this->SUPER::genObject( $query, $bindNode, $field . ":$var", - $name, "remove", "UNCHECKED" ) . "\n"; - - - In the option --dump-block-summary, the number of sub arguments indicated - for each sub now includes any leading object variable passed with - an arrow-operator call. Previously the count would have been decreased - by one in this case. This change is needed for compatibility with future - updates. - - - The operator ``**=`` now has spaces on both sides by default. Previously, - there was no space on the left. This change makes its spacing the same - as all other assignment operators. The previous behavior can be obtained - with the parameter setting -nwls='**='. - - - The option --file-size-order, or -fso is now the default. When - perltidy is given a list of multiple filenames to process, they - are sorted by size and processed in order of increasing size. - This can significantly reduce memory usage by Perl. This - option has always been used in testing, where typically several - jobs each operating on thousands of filenames are running at the - same time and competing for system resources. If this option - is not wanted for some reason, it can be deactivated with -nfso. - - - The option --valign-signed-numbers, or -vsn is now the default. It - was introduced in the previous release has been found to significantly - improve the overall appearance of columns of signed and unsigned - numbers. It will change formatting slightly in scripts with columns - of vertically aligned numbers, and can be deactivated with -nvsn. - - - The option --delete-repeated-commas is now the default. - - It makes the following checks and changes: - - Repeated commas like ',,' are removed with a warning - - Repeated fat commas like '=> =>' are removed with a warning - - The combination '=>,' produces a warning but is not changed - These warnings are only output if --warning-output, or -w, is set. - - Use --nodelete-repeated-commas, or -ndrc, to retain repeated commas. - - Added control --delete-interbracket-arrows, or -dia, to delete optional hash ref and array ref arrows between brackets as in the following expression (see git #131) diff --git a/bin/perltidy b/bin/perltidy index aa190991..7615362e 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -5748,7 +5748,24 @@ perltidy development. Perltidy reports any obvious issues that are found during formatting, such as unbalanced braces. But several parameters are available for making certain -additional checks for issues which might be of interest to a programmer. +additional checks for issues which might be of interest to a programmer. These +parameters fall into two categories as indicated by their prefix, B<--dump-> or +B<--warn->: + +=over 4 + +=item * +The B<--dump-> parameters read a file, write information to the standard output, +and then exit without doing any formatting. + +=item * +The B<--warn-> parameters, on the other hand, cause perltidy to function +normally but issue warnings to the error output when certain conditions are +encountered. + +=back + +Some of these have associated control parameters. =over 4 @@ -6078,7 +6095,7 @@ C. The parameter B<--dump-mismatched-args>, or B<-dma>, causes perltidy to examine the definitions of subroutines in a file, and calls to those subs, -and report any apparent differences. Like all B<--dump> commands, it +and report certain differences. Like all B<--dump> commands, it writes its report to standard output and exits immediately. For example perltidy -dma somefile.pl >results.txt @@ -6143,10 +6160,6 @@ Anonymous subs and lexical subs (introduced with C) are not checked. Only calls which appear to be to subs defined within the file being processed are checked. But note that a file may contain multiple packages. -=item * -When prototypes or signatures are given, they are used to determine the -expected number of sub arguments. Otherwise, the sub text is scanned. - =back =item B required to avoid reporting them. B<--warn-mismatched-arg-overcount-cutoff=n>, or B<-wmaoc=n>, can be used to avoid B warnings when the expected number of args is less than B. The default value is B. This avoids warning messages for subroutines -which are dummy placeholders for future development. +which are dummy placeholders for possible expansion. =back diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index f36779f6..841c2a0c 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -1144,7 +1144,7 @@ sub new { if (DEVEL_MODE) { my @non_existant; foreach ( 0 .. _LAST_SELF_INDEX_ ) { - if ( !exists( $self->[$_] ) ) { + if ( !exists $self->[$_] ) { push @non_existant, $_; } } @@ -1443,7 +1443,7 @@ sub split_words { return unless $str; $str =~ s/\s+$//; $str =~ s/^\s+//; - return split( /\s+/, $str ); + return split /\s+/, $str; } ## end sub split_words ########################################### @@ -1986,7 +1986,7 @@ sub initialize_space_after_keyword { if ( my @q = split_words( $rOpts->{'nospace-after-keyword'} ) ) { # -nsak='*' selects all the above keywords - if ( @q == 1 && $q[0] eq '*' ) { @q = keys(%space_after_keyword) } + if ( @q == 1 && $q[0] eq '*' ) { @q = keys %space_after_keyword } @space_after_keyword{@q} = (0) x scalar(@q); } @@ -9741,6 +9741,7 @@ sub dump_mixed_call_parens { my $output_string = <{$type_sequence} = { - token_mm => $last_last_nonblank_code_token, - type_mm => $last_last_nonblank_code_type, - token_m => $last_nonblank_code_token, - type_m => $last_nonblank_code_type, + type_mm => $last_last_nonblank_code_type, + token_m => $last_nonblank_code_token, }; } } @@ -13657,7 +13656,7 @@ sub count_prototype_args { $count_min = undef if ( !$saw_semicolon ); return; }; - while ( my $ch = shift(@chars) ) { + while ( my $ch = shift @chars ) { if ( !defined($ch) ) { $saw_array->(); last } elsif ( $ch eq '(' ) { last if ($count_min) } elsif ( $ch eq ')' ) { last } @@ -13889,7 +13888,6 @@ EOM elsif ( $token eq '$_' ) { # Found $_: currently the search ends at '$_[' - # TODO: eventually this can be handled my $Kn = $self->K_next_code($KK); if ( $Kn && $rLL->[$Kn]->[_TOKEN_] eq '[' ) { return; @@ -14296,9 +14294,6 @@ sub update_sub_call_paren_info { my $item = $rsub_call_paren_info_by_seqno->{$seqno}; my $name = $item->{token_m}; my $type_mm = $item->{type_mm}; - ## These values are available but currently unused: [TODO: maybe remove] - ## my $type_m = $item->{type_m}; - ## my $token_mm = $item->{token_mm}; # find function and package my $is_ampersand_call; @@ -14309,6 +14304,18 @@ sub update_sub_call_paren_info { $name = substr( $name, 1 ); } + my $call_type = $is_ampersand_call ? '&' : EMPTY_STRING; + my $caller_name = EMPTY_STRING; + if ( $type_mm eq '->' ) { + $call_type = '->'; + my $K_m = $self->K_previous_code($Ko); + my $K_mm = $self->K_previous_code($K_m); + my $K_mmm = $self->K_previous_code($K_mm); + if ( defined($K_mmm) && $rLL->[$K_mmm]->[_TYPE_] eq 'i' ) { + $caller_name = $rLL->[$K_mmm]->[_TOKEN_]; + } + } + # look for explicit package on name my $package = $current_package; if ( ( index( $name, ':' ) >= 0 || index( $name, "'" ) >= 0 ) @@ -14363,18 +14370,6 @@ sub update_sub_call_paren_info { $arg_count = $item->{shift_count_min}; } - my $call_type = $is_ampersand_call ? '&' : EMPTY_STRING; - my $caller_name = EMPTY_STRING; - if ( $type_mm eq '->' ) { - $call_type = '->'; - my $K_m = $self->K_previous_code($Ko); - my $K_mm = $self->K_previous_code($K_m); - my $K_mmm = $self->K_previous_code($K_mm); - if ( defined($K_mmm) && $rLL->[$K_mmm]->[_TYPE_] eq 'i' ) { - $caller_name = $rLL->[$K_mmm]->[_TOKEN_]; - } - } - # update the hash of info for this item my $line_number = $rLL->[$Ko]->[_LINE_INDEX_] + 1; $item->{arg_count} = $arg_count; @@ -26024,7 +26019,7 @@ sub correct_lp_indentation_pass_1 { return unless (@ilist); my $max_line = @{$ri_first} - 1; - my $inext = shift(@ilist); + my $inext = shift @ilist; # loop over lines, checking length of each with a one-line block my ( $ibeg, $iend ); -- 2.39.5