]> git.donarmstrong.com Git - perltidy.git/commitdiff
Fix rare problem with formatting nested ternary statements
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 23 Jul 2021 03:36:54 +0000 (20:36 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 23 Jul 2021 03:36:54 +0000 (20:36 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index 9132083dc1980b6f495f2bbcb1d6343f1a7ef977..cbe41ed2969eee94ac950c8067af351d64bcaba3 100644 (file)
@@ -494,10 +494,6 @@ BEGIN {
     # Maximum number of little messages; probably need not be changed.
     use constant MAX_NAG_MESSAGES => 6;
 
-    # increment between sequence numbers for each type
-    # For example, ?: pairs might have numbers 7,11,15,...
-    use constant TYPE_SEQUENCE_INCREMENT => 4;
-
     # Initialize constant hashes ...
     my @q;
 
@@ -15696,16 +15692,16 @@ sub set_continuation_breaks {
     # places to break long lists.
 
     my (
-        $block_type,               $current_depth,
-        $depth,                    $i,
-        $i_last_nonblank_token,    $last_colon_sequence_number,
-        $last_nonblank_token,      $last_nonblank_type,
-        $last_nonblank_block_type, $last_old_breakpoint_count,
-        $minimum_depth,            $next_nonblank_block_type,
-        $next_nonblank_token,      $next_nonblank_type,
-        $old_breakpoint_count,     $starting_breakpoint_count,
-        $starting_depth,           $token,
-        $type,                     $type_sequence,
+        $block_type,                $current_depth,
+        $depth,                     $i,
+        $i_last_nonblank_token,     $last_nonblank_token,
+        $last_nonblank_type,        $last_nonblank_block_type,
+        $last_old_breakpoint_count, $minimum_depth,
+        $next_nonblank_block_type,  $next_nonblank_token,
+        $next_nonblank_type,        $old_breakpoint_count,
+        $starting_breakpoint_count, $starting_depth,
+        $token,                     $type,
+        $type_sequence,
     );
 
     my (
@@ -16071,14 +16067,13 @@ sub set_continuation_breaks {
 
         $starting_depth = $nesting_depth_to_go[0];
 
-        $block_type                 = ' ';
-        $current_depth              = $starting_depth;
-        $i                          = -1;
-        $last_colon_sequence_number = -1;
-        $last_nonblank_token        = ';';
-        $last_nonblank_type         = ';';
-        $last_nonblank_block_type   = ' ';
-        $last_old_breakpoint_count  = 0;
+        $block_type                = ' ';
+        $current_depth             = $starting_depth;
+        $i                         = -1;
+        $last_nonblank_token       = ';';
+        $last_nonblank_type        = ';';
+        $last_nonblank_block_type  = ' ';
+        $last_old_breakpoint_count = 0;
         $minimum_depth = $current_depth + 1;    # forces update in check below
         $old_breakpoint_count      = 0;
         $starting_breakpoint_count = get_forced_breakpoint_count();
@@ -16097,6 +16092,7 @@ sub set_continuation_breaks {
         my $saw_good_breakpoint;
         my $i_line_end   = -1;
         my $i_line_start = -1;
+        my $i_last_colon = -1;
 
         # loop over all tokens in this batch
         while ( ++$i <= $max_index_to_go ) {
@@ -16295,7 +16291,7 @@ sub set_continuation_breaks {
                 # handle any postponed closing breakpoints
                 if ( $is_closing_sequence_token{$token} ) {
                     if ( $type eq ':' ) {
-                        $last_colon_sequence_number = $type_sequence;
+                        $i_last_colon = $i;
 
                         # retain break at a ':' line break
                         if ( ( $i == $i_line_start || $i == $i_line_end )
@@ -16331,19 +16327,22 @@ sub set_continuation_breaks {
                       )
                     {
 
+                        # don't break if # this has a side comment, and
                         # don't break at a '?' if preceded by ':' on
                         # this line of previous ?/: pair on this line.
                         # This is an attempt to preserve a chain of ?/:
-                        # expressions (elsif2.t).  And don't break if
-                        # this has a side comment.
-                        $self->set_forced_breakpoint($i)
-                          unless (
-                            $type_sequence == (
-                                $last_colon_sequence_number +
-                                  TYPE_SEQUENCE_INCREMENT
+                        # expressions (elsif2.t).
+                        if (
+                            (
+                                   $i_last_colon < 0
+                                || $parent_seqno_to_go[$i_last_colon] !=
+                                $parent_seqno_to_go[$i]
                             )
-                            || $tokens_to_go[$max_index_to_go] eq '#'
-                          );
+                            && $tokens_to_go[$max_index_to_go] ne '#'
+                          )
+                        {
+                            $self->set_forced_breakpoint($i);
+                        }
                         $self->set_closing_breakpoint($i);
                     } ## end if ( $i_colon <= 0  ||...)
                 } ## end elsif ( $token eq '?' )
index dee3c30c3a936e56b8a1165f7349539ba61ebe0f..f1cdc77bf8ac0b8254cfc0bc8f91aad6d8249f58 100644 (file)
@@ -2,6 +2,60 @@
 
 =over 4
 
+=item B<Fix rare problem with formatting nested ternary statements>
+
+This update fixes an extremely rare problem in formatting nested ternary
+statements, illustrated in the following snippet:
+
+  # OLD: There should be a break before the '?' in line 11 here:
+  WriteMakefile(
+      (
+          $PERL_CORE ? ()
+          : (
+              (
+                  eval { ExtUtils::MakeMaker->VERSION(6.48) }
+                  ? ( MIN_PERL_VERSION => '5.006' )
+                  : ()
+              ),
+              (
+                  eval { ExtUtils::MakeMaker->VERSION(6.46) } ? (
+                      META_MERGE => {
+                          #
+                      }
+                    )
+                  : ()
+              ),
+          )
+      ),
+  );
+
+  # NEW: Line 12 correctly begins with a '?'
+  WriteMakefile(
+      (
+          $PERL_CORE ? ()
+          : (
+              (
+                  eval { ExtUtils::MakeMaker->VERSION(6.48) }
+                  ? ( MIN_PERL_VERSION => '5.006' )
+                  : ()
+              ),
+              (
+                  eval { ExtUtils::MakeMaker->VERSION(6.46) }
+                  ? (
+                      META_MERGE => {
+                          #
+                      }
+                    )
+                  : ()
+              ),
+          )
+      ),
+  );
+
+This fixes issue c050.
+
+22 Jul 2021.
+
 =item B<Fix conflict of -bom and -scp parameters>
 
 Automated testing with random parameters produced a case of instability caused