]> git.donarmstrong.com Git - perltidy.git/commitdiff
delay -atc and -dtc 1 iteration if -conv, see git #156
authorSteve Hancock <perltidy@users.sourceforge.net>
Wed, 24 Jul 2024 21:50:05 +0000 (14:50 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Wed, 24 Jul 2024 21:50:05 +0000 (14:50 -0700)
bin/perltidy
lib/Perl/Tidy.pm
lib/Perl/Tidy/FileWriter.pm
lib/Perl/Tidy/Formatter.pm

index 98c9e91da55657aebd6784f699b6f35df19f8b37..a55e8838e5128ccf062bd595468c71da3ea62a1c 100755 (executable)
@@ -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<sub old_school> above.
+the first sub above.
 Note that the B<Perl::Critic> policy B<RequireFinalReturn> can be used to check for code in this situation.
 
 Reported issues are
index 45798f6d92a0dbffc9247e92aaa5c4102351bda4..0d0dea1961c352d2f96add8395e80e1926e7744c 100644 (file)
@@ -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
index ca4e3e5945a3d42faf21010baeb272b048c1ca47..33c34632137beb0bc8d780924898063eab577919 100644 (file)
@@ -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_];
index 8cc60c24acde98a87fedada936dc4fce427d8103..f1b385b2ef33ec17a4ff232f1cee5af3149cc8c6 100644 (file)
@@ -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};