From: Steve Hancock Date: Wed, 24 Jul 2024 21:50:05 +0000 (-0700) Subject: delay -atc and -dtc 1 iteration if -conv, see git #156 X-Git-Tag: 20240511.07~2 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=7048bb24579d3c3dbd46a42421a19b075e683493;p=perltidy.git delay -atc and -dtc 1 iteration if -conv, see git #156 --- diff --git a/bin/perltidy b/bin/perltidy index 98c9e91d..a55e8838 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -6377,7 +6377,7 @@ cannot be determined for a call then it cannot be checked. Since only return statements are scanned for return values, this analysis will not be useful for programming which relies on the default return mechanism, as in -C above. +the first sub above. Note that the B policy B can be used to check for code in this situation. Reported issues are diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 45798f6d..0d0dea19 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -504,6 +504,10 @@ my $md5_hex = sub { return $digest; }; +sub get_iteration_count { + return $rstatus->{iteration_count}; +} + BEGIN { # Array index names for $self. @@ -3521,6 +3525,7 @@ sub generate_options { $add_option->( 'delete-weld-interfering-commas', 'dwic', '!' ); $add_option->( 'delete-semicolons', 'dsm', '!' ); $add_option->( 'function-paren-vertical-alignment', 'fpva', '!' ); + $add_option->( 'iterate-trailing-commas', 'itc', '!' ); $add_option->( 'keyword-paren-inner-tightness', 'kpit', '=i' ); $add_option->( 'keyword-paren-inner-tightness-list', 'kpitl', '=s' ); $add_option->( 'logical-padding', 'lop', '!' ); @@ -3865,6 +3870,7 @@ sub generate_options { indent-columns=4 integer-range-check=2 interbracket-arrow-complexity=1 + iterate-trailing-commas iterations=1 keep-old-blank-lines=1 keyword-paren-inner-tightness=1 diff --git a/lib/Perl/Tidy/FileWriter.pm b/lib/Perl/Tidy/FileWriter.pm index ca4e3e59..33c34632 100644 --- a/lib/Perl/Tidy/FileWriter.pm +++ b/lib/Perl/Tidy/FileWriter.pm @@ -224,6 +224,15 @@ sub setup_convergence_test { return; } ## end sub setup_convergence_test +sub not_converged { + my $self = shift; + + # Block convergence of this iteration. This is currently needed + # when adding/deleting commas is delayed by 1 iteration (see git #156) + $self->[_K_arrival_order_matches_] = 0; + return; +} ## end sub not_converged + sub get_convergence_check { my ($self) = @_; my $rlist = $self->[_rK_checklist_]; diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 8cc60c24..f1b385b2 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -12265,6 +12265,33 @@ sub add_phantom_semicolon { return; } ## end sub add_phantom_semicolon +sub delay_trailing_comma_op { + my $self = shift; + + # Returns: + # true if a trailing comma operation should be skipped + # false otherwise + + # This can prevent unwanted path-dependent formatting when both + # line breaks are changing and we are only adding or deleting + # commas, but not both. See git #156 + + # permission must be given + return if ( !$rOpts->{'iterate-trailing-commas'} ); + + # we must be at the first of multiple iterations + my $it = Perl::Tidy::get_iteration_count(); + my $max_iterations = $rOpts->{'iterations'}; + if ( $it == 1 && $max_iterations > 1 ) { + + # if so, force another iteration + my $file_writer_object = $self->[_file_writer_object_]; + $file_writer_object->not_converged(); + return 1; + } + return; +} ## end sub delay_trailing_comma_op + sub add_trailing_comma { # Implement the --add-trailing-commas flag to the line end before index $KK: @@ -12317,8 +12344,8 @@ sub add_trailing_comma { } } - # if so, add a comma - if ($match) { + # If so, and not delayed, add a comma + if ( $match && !$self->delay_trailing_comma_op() ) { # any blank after the comma will be added before the closing paren, # below @@ -12407,9 +12434,10 @@ sub delete_trailing_comma { } } - # If no match, delete it - if ( !$match ) { + # If no match and not delayed + if ( !$match && !$self->delay_trailing_comma_op() ) { + # delete it return $self->unstore_last_nonblank_token(','); } return; @@ -15951,9 +15979,9 @@ sub cross_check_sub_calls { } - #----------------------------------------------------------------------- - # Loop over all sub calls to compare counts of args passed with expected - #----------------------------------------------------------------------- + #-------------------------------------------------------------- + # Loop over all sub calls to compare call and return arg counts + #-------------------------------------------------------------- foreach my $seqno ( keys %{$rsub_call_paren_info_by_seqno} ) { my $rcall_item = $rsub_call_paren_info_by_seqno->{$seqno};