]> git.donarmstrong.com Git - perltidy.git/commitdiff
simplify and improve efficiency of sub set_ci
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 6 Jun 2023 01:20:31 +0000 (18:20 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 6 Jun 2023 01:20:31 +0000 (18:20 -0700)
lib/Perl/Tidy/Formatter.pm

index ec1fa502b791e316a5519b4a06941f16e49e695a..57967cb3e5eb709a2efcd3647d5a194f14a4d717 100644 (file)
@@ -6691,20 +6691,18 @@ sub set_ci {
     # programs but is useful for testing.
     use constant SET_CI_OPTION_0 => 1;
 
-    # NOTE: This is slightly different from the hash in in break_lists
-    # with the same name
+    # This is slightly different from the hash in in break_lists
+    # with a similar name (removed '?' and ':' to fix t007 and others)
     my %is_logical_container_for_ci;
-    ## Removed ? : to fix t007 and others
-    ##my @q = qw# if elsif unless while and or err not && | || ? : ! #;
     my @q = qw# if elsif unless while and or err not && | || ! #;
     @is_logical_container_for_ci{@q} = (1) x scalar(@q);
 
-    # NOTE: using differnt hash than in tokenizer here, but same name:
+    # This is slightly differnt from a tokenizer hash with a similar name:
     my %is_container_label_type_for_ci;
     @q = qw# k && | || ? : ! #;
     @is_container_label_type_for_ci{@q} = (1) x scalar(@q);
 
-    # The following hash is set to match old ci values
+    # Undo ci of closing list paren followed by these binary operatiors:
     # - initially defined for issue t027, then
     # - added '=' for t015
     # - added '=~' for 'locale.in'
@@ -6727,21 +6725,18 @@ sub set_ci {
 
     # Set a flag which tells if sub respace_tokens has been called yet. This
     # allows this sub to work either before or after respace_tokens is called.
+    # NOTE: this can eventually be removed since it will always be true
     my $respace_tokens_called = defined( $rLL->[0]->[_KNEXT_SEQ_ITEM_] );
 
-    my $token   = ';';
-    my $type    = ';';
-    my $ci_next = 0;
-
-    my $last_token = $token;
-    my $last_type  = $type;
-    my $ci_last    = 0;
-
+    my $token        = ';';
+    my $type         = ';';
+    my $ci_next      = 0;
+    my $last_token   = $token;
+    my $last_type    = $type;
+    my $ci_last      = 0;
     my $ci_next_next = 1;
+    my $rstack       = ();
 
-    my $rstack = ();
-
-    # - note that ci_default = 0 only for 'List'
     my $seq_root = SEQ_ROOT;
     my $rparent  = {
         _seqno          => $seq_root,
@@ -6799,6 +6794,8 @@ sub set_ci {
     };
 
     my $redo_preceding_comment_ci = sub {
+
+        # We need to reset the ci of the previous comment(s)
         my ( $K, $ci ) = @_;
         my $Km = $self->K_previous_code($K);
         return if ( !defined($Km) );
@@ -6810,67 +6807,155 @@ sub set_ci {
         return;
     };
 
-    #----------
-    # Main loop
-    #----------
-    foreach my $KK ( 0 .. $Klimit ) {
-        my $rtoken_K = $rLL->[$KK];
+    # Definitions of the sequence of ci_values being maintained:
+    # $ci_last      = the ci value of the previous non-blank, non-comment token
+    # $ci_this      = the ci value to be stored for this token at index $KK
+    # $ci_next      = the normal ci for the next token, set by the previous tok
+    # $ci_next_next = the normal next value of $ci_next in this container
 
-        $type  = $rtoken_K->[_TYPE_];
-        $token = $rtoken_K->[_TOKEN_];
+    #--------------------------
+    # Main loop over all tokens
+    #--------------------------
+    my $KK = -1;
+    foreach my $rtoken_K ( @{$rLL} ) {
 
-        # Definitions:
-        # $ci_this      = the ci for this token
-        # $ci_next      = the ci for the next token
-        # $ci_next_next = the normal next value of $ci_next in this container
+        $KK++;
+        $type = $rtoken_K->[_TYPE_];
 
-        # Normally we use the ci value value set by previous token.
-        my $ci_this = $ci_next;
+        #------------------
+        # Section 1. Blanks
+        #------------------
+        if ( $type eq 'b' ) {
 
-        # First guess at next value uses the stored default
-        # which is 0 for logical containers, 1 for other containers:
-        $ci_next = $ci_next_next;
+            $rtoken_K->[_CI_LEVEL_] = $ci_next;
 
-        # We will change these ci values necessary for special cases...
+            # 'next' to avoid saving last_ values for blanks and commas
+            next;
+        }
 
-        #----------
-        # 1. Blanks
-        #----------
-        if ( $type eq 'b' ) {
+        #--------------------
+        # Section 2. Comments
+        #--------------------
+        if ( $type eq '#' ) {
 
-            $ci_next = $ci_this;
+            my $ci_this = $ci_next;
+
+            # If at '#' in ternary before a ? or :, use that level to make
+            # the comment line up with the next ? or : line.  (see c202/t052)
+            # i.e. if a nested ? follows, we increase the '#' level by 1, and
+            # if a nested : follows, we decrease the '#' level by 1.
+            # This is the only place where this sub changes a _LEVEL_ value.
+            my $Kn;
+            my $parent_container_type = $rparent->{_container_type};
+            if ( $parent_container_type eq 'Ternary' ) {
+                $Kn = $self->K_next_code($KK);
+                if ($Kn) {
+                    my $type_kn = $rLL->[$Kn]->[_TYPE_];
+                    if ( $is_ternary{$type_kn} ) {
+                        my $level_KK = $rLL->[$KK]->[_LEVEL_];
+                        my $level_Kn = $rLL->[$Kn]->[_LEVEL_];
+                        $rLL->[$KK]->[_LEVEL_] = $rLL->[$Kn]->[_LEVEL_];
+
+                        # and use the ci of a terminating ':'
+                        if ( $Kn == $rparent->{_Kc} ) {
+                            $ci_this = $rparent->{_ci_close};
+                        }
+                    }
+                }
+            }
+
+            # Undo ci for a block comment followed by a closing token or , or ;
+            # provided that the parent container:
+            # - ends without ci, or
+            # - starts ci=0 and is a comma list or this follows a closing type
+            # - has a level jump
+            if (
+                $ci_this
+                && (
+                    !$rparent->{_ci_close}
+                    || (
+                        !$rparent->{_ci_open_next}
+                        && ( ( $rparent->{_comma_count} || $last_type eq ',' )
+                            || $is_closing_type{$last_type} )
+                    )
+                )
+              )
+            {
+                # Be sure this is a block comment
+                my $lx       = $rtoken_K->[_LINE_INDEX_];
+                my $rK_range = $rlines->[$lx]->{_rK_range};
+                my $Kfirst;
+                if ($rK_range) { $Kfirst = $rK_range->[0] }
+                if ( defined($Kfirst) && $Kfirst == $KK ) {
+
+                    # Look for trailing closing token
+                    #    [ and possibly ',' or ';' ]
+                    $Kn = $self->K_next_code($KK) if ( !$Kn );
+                    my $Kc = $rparent->{_Kc};
+                    if (
+                           $Kn
+                        && $Kc
+                        && (
+                            $Kn == $Kc
 
-            # We should never be using the ci of a blank token, but for
-            # reference, here is the rule and code to match the old ci coding:
+                            # only look for comma if -wbb=',' is set
+                            # to minimize changes to existing formatting
+                            || (   $rLL->[$Kn]->[_TYPE_] eq ','
+                                && $want_break_before_comma
+                                && $parent_container_type eq 'List' )
 
-            #  A blank after closing token has same ci as previous token,
-            #  Otherwise a blank has same ci as next token;
-            #  if ( $is_closing_type{$last_type} ) {
-            #      $ci_this = $ci_last;
-            #  }
+                            # do not look ahead for a bare ';' because
+                            # it changes old formatting with little benefit.
+##                          || (   $rLL->[$Kn]->[_TYPE_] eq ';'
+##                                && $parent_container_type eq 'Block' )
+                        )
+                      )
+                    {
 
+                        # Be sure container has a level jump
+                        my $level_KK = $rLL->[$KK]->[_LEVEL_];
+                        my $level_Kc = $rLL->[$Kc]->[_LEVEL_];
+                        if ( $level_Kc < $level_KK ) {
+                            $ci_this = 0;
+                        }
+                    }
+                }
+            }
+
+            $ci_next = $ci_this;
             $rtoken_K->[_CI_LEVEL_] = $ci_this;
 
             # 'next' to avoid saving last_ values for blanks and commas
             next;
         }
 
-        #--------------------
-        # 2. Container tokens
-        #--------------------
-        elsif ( $rtoken_K->[_TYPE_SEQUENCE_] ) {
+        #------------------------------------------------------------
+        # Section 3. Continuing with non-blank and non-comment tokens
+        #------------------------------------------------------------
+
+        $token = $rtoken_K->[_TOKEN_];
+
+        # Set ci values appropriate for most tokens:
+        my $ci_this = $ci_next;
+        $ci_next = $ci_next_next;
+
+        # Now change these ci values as necessary for special cases...
+
+        #----------------------------
+        # Section 4. Container tokens
+        #----------------------------
+        if ( $rtoken_K->[_TYPE_SEQUENCE_] ) {
 
             my $seqno       = $rtoken_K->[_TYPE_SEQUENCE_];
             my $rtype_count = $rtype_count_by_seqno->{$seqno};
             my $comma_count = $rtype_count ? $rtype_count->{','} : 0;
 
-            #-----------------------------
-            # 2.1 Opening container tokens
-            #-----------------------------
+            #-------------------------------------
+            # Section 4.1 Opening container tokens
+            #-------------------------------------
             if ( $is_opening_sequence_token{$token} ) {
 
-                my $level          = $rtoken_K->[_LEVEL_];
-                my $container_type = EMPTY_STRING;
+                my $level = $rtoken_K->[_LEVEL_];
 
                 # Default ci values for the closing token, to be modified
                 # as necessary:
@@ -6906,101 +6991,32 @@ sub set_ci {
                 my $opening_level_jump =
                   $Kn ? $rLL->[$Kn]->[_LEVEL_] - $level : 0;
 
-                #-----------------------------------
-                # 2.1.1 Determine the container type
-                #-----------------------------------
-                my $is_logical = $is_container_label_type_for_ci{$last_type}
-                  && $is_logical_container_for_ci{$last_token};
-
-                # Part 1 of optional patch to get agreement with previous ci
-                # This makes almost no difference in a typical program because
-                # we will seldom break within an array index.
-                $is_logical ||= $type eq '[' && SET_CI_OPTION_0;
-
-                if ( $token eq '(' ) {
-
-                    # 'foreach' and 'for' paren contents are treated as logical
-                    #  except for C-style 'for'
-                    if ( $last_type eq 'k' ) {
-                        $is_logical ||= $last_token eq 'foreach';
-
-                        if ( $last_token eq 'for' ) {
-                            if (   $rtype_count
-                                && $rtype_count->{'f'} )
-                            {
-                                # C-style 'for' container will be type 'List'
-                                $is_logical = 0;
-                            }
-                            else {
-                                $is_logical = 1;
-                            }
-                        }
-                    }
-
-                    # Check for 'for' and 'foreach' loops with iterators
-                    elsif ( $last_type eq 'i' && defined($Kcn) ) {
-                        my $seqno_kcn = $rLL->[$Kcn]->[_TYPE_SEQUENCE_];
-                        my $type_kcn  = $rLL->[$Kcn]->[_TOKEN_];
-                        if ( $seqno_kcn && $type_kcn eq '{' ) {
-                            my $block_type_kcn =
-                              $rblock_type_of_seqno->{$seqno_kcn};
-                            $is_logical ||= $block_type_kcn
-                              && ( $block_type_kcn eq 'for'
-                                || $block_type_kcn eq 'foreach' );
-                        }
-
-                        # Search backwards for 'for'/'foreach' with iterator in
-                        # case user is running from an editor and did not
-                        # include the block (fixes case 'xci.in').
-                        my $Km = $self->K_previous_code($KK);
-                        foreach ( 0 .. 2 ) {
-                            $Km = $self->K_previous_code($Km);
-                            last unless defined($Km);
-                            last unless $rLL->[$Km]->[_TYPE_] eq 'k';
-                            my $tok = $rLL->[$Km]->[_TOKEN_];
-                            next if $tok eq 'my';
-                            $is_logical ||=
-                              ( $tok eq 'for' || $tok eq 'foreach' );
-                            last;
-                        }
-                    }
-
-                    elsif ( $last_token eq '(' ) {
-                        $is_logical ||=
-                          $rparent->{_container_type} eq 'Logical';
-                    }
-
-                    # Pass ci though an '!'
-                    elsif ( $last_type eq '!' ) { $ci_this = $ci_last }
-                }
-
                 # initialize ci_next_next to its standard value
                 $ci_next_next = 1;
 
-                my $block_type = $rblock_type_of_seqno->{$seqno};
-                $block_type = EMPTY_STRING unless ($block_type);
-
                 # Default: ci of first item of list with level jump is same as
                 # ci of first item of container
                 if ( $opening_level_jump > 0 ) {
                     $ci_next = $rparent->{_ci_open_next};
                 }
-                my $no_semicolon;
 
-                #-----------------
-                # 2.1.2 Code Block
-                #-----------------
+                my $container_type;
+
+                #-------------------------
+                # Section 4.1.1 Code Block
+                #-------------------------
+                my $block_type = $rblock_type_of_seqno->{$seqno};
                 if ($block_type) {
                     $container_type = 'Block';
 
-                    $no_semicolon =
+                    # set default depending on block type
+                    $ci_close = 0;
+
+                    my $no_semicolon =
                          $is_block_without_semicolon{$block_type}
                       || $ris_sub_block->{$seqno}
                       || $last_type eq 'J';
 
-                    # set default depending on block type
-                    $ci_close = 0;
-
                     if ( !$no_semicolon ) {
 
                         # Optional fix for block types sort/map/etc which use
@@ -7058,9 +7074,9 @@ sub set_ci {
                     $ci_close_next = $ci_close;
                 }
 
-                #--------------
-                # 2.1.3 Ternary
-                #--------------
+                #----------------------
+                # Section 4.1.2 Ternary
+                #----------------------
                 elsif ( $type eq '?' ) {
                     $container_type = 'Ternary';
                     if ( $rparent->{_container_type} eq 'List'
@@ -7077,84 +7093,148 @@ sub set_ci {
                     }
                 }
 
-                #--------------
-                # 2.1.4 Logical
-                #--------------
-                elsif ($is_logical) {
-                    $container_type = 'Logical';
+                #-------------------------------
+                # Section 4.1.3 Logical or List?
+                #-------------------------------
+                else {
+                    my $is_logical = $is_container_label_type_for_ci{$last_type}
+                      && $is_logical_container_for_ci{$last_token}
 
-                    $ci_next_next  = 0;
-                    $ci_close_next = $ci_this;
+                      # Part 1 of optional patch to get agreement with previous
+                      # ci This makes almost no difference in a typical program
+                      # because we will seldom break within an array index.
+                      || $type eq '[' && SET_CI_OPTION_0;
 
-                    # Part 2 of optional patch to get agreement with previous ci
-                    if ( $type eq '[' && SET_CI_OPTION_0 ) {
+                    if ( !$is_logical && $token eq '(' ) {
 
-                        $ci_next_next = $ci_this;
+                        # 'foreach' and 'for' paren contents are treated as
+                        # logical except for C-style 'for'
+                        if ( $last_type eq 'k' ) {
+                            $is_logical ||= $last_token eq 'foreach';
 
-                        # Undo ci at a chain of indexes or hash keys
-                        if ( $last_type eq '}' ) {
-                            $ci_this = $ci_last;
+                            # C-style 'for' container will be type 'List'
+                            if ( $last_token eq 'for' ) {
+                                $is_logical =
+                                  !( $rtype_count && $rtype_count->{'f'} );
+                            }
                         }
-                    }
 
-                    if ($opening_level_jump) {
-                        $ci_next = 0;
+                        # Check for 'for' and 'foreach' loops with iterators
+                        elsif ( $last_type eq 'i' && defined($Kcn) ) {
+                            my $seqno_kcn = $rLL->[$Kcn]->[_TYPE_SEQUENCE_];
+                            my $type_kcn  = $rLL->[$Kcn]->[_TOKEN_];
+                            if ( $seqno_kcn && $type_kcn eq '{' ) {
+                                my $block_type_kcn =
+                                  $rblock_type_of_seqno->{$seqno_kcn};
+                                $is_logical ||= $block_type_kcn
+                                  && ( $block_type_kcn eq 'for'
+                                    || $block_type_kcn eq 'foreach' );
+                            }
+
+                            # Search backwards for 'for'/'foreach' with
+                            # iterator in case user is running from an editor
+                            # and did not include the block (fixes case
+                            # 'xci.in').
+                            my $Km = $self->K_previous_code($KK);
+                            foreach ( 0 .. 2 ) {
+                                $Km = $self->K_previous_code($Km);
+                                last unless defined($Km);
+                                last unless $rLL->[$Km]->[_TYPE_] eq 'k';
+                                my $tok = $rLL->[$Km]->[_TOKEN_];
+                                next if $tok eq 'my';
+                                $is_logical ||=
+                                  ( $tok eq 'for' || $tok eq 'foreach' );
+                                last;
+                            }
+                        }
+                        elsif ( $last_token eq '(' ) {
+                            $is_logical ||=
+                              $rparent->{_container_type} eq 'Logical';
+                        }
                     }
-                }
 
-                #-----------
-                # 2.1.5 List
-                #-----------
-                else {
+                    #------------------------
+                    # Section 4.1.3.1 Logical
+                    #------------------------
+                    if ($is_logical) {
+                        $container_type = 'Logical';
 
-                    # Here 'List' is a catchall for none of the above types
-                    $container_type = 'List';
+                        # Pass ci though an '!'
+                        if ( $last_type eq '!' ) { $ci_this = $ci_last }
 
-                    # lists in blocks ...
-                    if ( $rparent->{_container_type} eq 'Block' ) {
+                        $ci_next_next  = 0;
+                        $ci_close_next = $ci_this;
 
-                        # undo ci if another closing token follows
-                        if ( defined($Kcn) ) {
-                            my $closing_level_jump =
-                              $rLL->[$Kcn]->[_LEVEL_] - $level;
-                            if ( $closing_level_jump < 0 ) {
-                                $ci_close = $ci_this;
+                        # Part 2 of optional patch to get agreement with
+                        # previous ci
+                        if ( $type eq '[' && SET_CI_OPTION_0 ) {
+
+                            $ci_next_next = $ci_this;
+
+                            # Undo ci at a chain of indexes or hash keys
+                            if ( $last_type eq '}' ) {
+                                $ci_this = $ci_last;
                             }
                         }
+
+                        if ($opening_level_jump) {
+                            $ci_next = 0;
+                        }
                     }
 
-                    # lists not in blocks ...
+                    #---------------------
+                    # Section 4.1.3.2 List
+                    #---------------------
                     else {
 
-                        if ( !$rparent->{_comma_count} ) {
+                        # Here 'List' is a catchall for none of the above types
+                        $container_type = 'List';
 
-                            $ci_close = $ci_this;
+                        # lists in blocks ...
+                        if ( $rparent->{_container_type} eq 'Block' ) {
 
-                            # undo ci at binary op after right paren if no
-                            # commas in container; fixes t027, t028
-                            if ( $ci_close_next != $ci_close && defined($Kcn) )
-                            {
-                                my $type_kcn = $rLL->[$Kcn]->[_TYPE_];
-                                if ( $bin_op_type{$type_kcn} ) {
-                                    $ci_close_next = $ci_close;
+                            # undo ci if another closing token follows
+                            if ( defined($Kcn) ) {
+                                my $closing_level_jump =
+                                  $rLL->[$Kcn]->[_LEVEL_] - $level;
+                                if ( $closing_level_jump < 0 ) {
+                                    $ci_close = $ci_this;
                                 }
                             }
                         }
 
-                        if ( $rparent->{_container_type} eq 'Ternary' ) {
-                            $ci_next = 0;
+                        # lists not in blocks ...
+                        else {
+
+                            if ( !$rparent->{_comma_count} ) {
+
+                                $ci_close = $ci_this;
+
+                                # undo ci at binary op after right paren if no
+                                # commas in container; fixes t027, t028
+                                if (   $ci_close_next != $ci_close
+                                    && defined($Kcn)
+                                    && $bin_op_type{ $rLL->[$Kcn]->[_TYPE_] } )
+                                {
+                                    $ci_close_next = $ci_close;
+                                }
+                            }
+
+                            if ( $rparent->{_container_type} eq 'Ternary' ) {
+                                $ci_next = 0;
+                            }
                         }
-                    }
 
-                    # Undo ci at a chain of indexes or hash keys
-                    if ( $token ne '(' && $last_type eq '}' ) {
-                        $ci_this = $ci_close = $ci_last;
+                        # Undo ci at a chain of indexes or hash keys
+                        if ( $token ne '(' && $last_type eq '}' ) {
+                            $ci_this = $ci_close = $ci_last;
+                        }
                     }
                 }
 
-                #--------------------------------
-                # 2.1.6 Closing token common code
-                #--------------------------------
+                #---------------------------------------
+                # Section 4.1.4 Store opening token info
+                #---------------------------------------
 
                 # Most closing tokens should align with their opening tokens.
                 if (
@@ -7186,10 +7266,11 @@ sub set_ci {
                 };
             }
 
-            #-----------------------------
-            # 2.2 Closing container tokens
-            #-----------------------------
+            #-------------------------------------
+            # Section 4.2 Closing container tokens
+            #-------------------------------------
             else {
+
                 my $seqno_test = $rparent->{_seqno};
                 if ( $seqno_test ne $seqno ) {
 
@@ -7199,11 +7280,12 @@ sub set_ci {
                       && Fault("stack error: $seqno_test != $seqno\n");
                 }
 
-                # use the values set by the opening token
+                # Use ci_this, ci_next values set by the matching opening token:
                 $ci_this = $rparent->{_ci_close};
                 $ci_next = $rparent->{_ci_close_next};
-
                 my $ci_open_old = $rparent->{_ci_open};
+
+                # Then pop the stack and use the parent ci_next_next value:
                 if ( @{$rstack} ) {
                     $rparent      = pop @{$rstack};
                     $ci_next_next = $rparent->{_ci_next_next};
@@ -7214,8 +7296,8 @@ sub set_ci {
                     DEVEL_MODE && Fault("empty stack - shouldn't happen\n");
                 }
 
-                # Undo ci at a closing token followed by a closing token. Goal
-                # is to keep formatting independent of the existance of a
+                # Fix: undo ci at a closing token followed by a closing token.
+                # Goal is to keep formatting independent of the existance of a
                 # trailing comma or semicolon.
                 if ( $ci_this > 0 && !$ci_open_old && !$rparent->{_ci_close} ) {
                     my $Kc = $rparent->{_Kc};
@@ -7227,103 +7309,9 @@ sub set_ci {
             }
         }
 
-        #------------
-        # 3. Comments
-        #------------
-        elsif ( $type eq '#' ) {
-
-            # If at '#' in ternary before a ? or :, use that level to make
-            # the comment line up with the next ? or : line.  (see c202/t052)
-            # i.e. if a nested ? follows, we increase the '#' level by 1, and
-            # if a nested : follows, we decrease the '#' level by 1.
-            # This is the only place where this sub changes a _LEVEL_ value.
-            my $Kn;
-            my $parent_container_type = $rparent->{_container_type};
-            if ( $parent_container_type eq 'Ternary' ) {
-                $Kn = $self->K_next_code($KK);
-                if ($Kn) {
-                    my $type_kn = $rLL->[$Kn]->[_TYPE_];
-                    if ( $is_ternary{$type_kn} ) {
-                        my $level_KK = $rLL->[$KK]->[_LEVEL_];
-                        my $level_Kn = $rLL->[$Kn]->[_LEVEL_];
-                        $rLL->[$KK]->[_LEVEL_] = $rLL->[$Kn]->[_LEVEL_];
-
-                        # and use the ci of a terminating ':'
-                        if ( $Kn == $rparent->{_Kc} ) {
-                            $ci_this = $rparent->{_ci_close};
-                        }
-                    }
-                }
-            }
-
-            # Undo ci for a block comment followed by a closing token or , or ;
-            # provided that the parent container:
-            # - ends without ci, or
-            # - starts ci=0 and is a comma list or this follows a closing type
-            # - has a level jump
-            if (
-                $ci_this
-                && (
-                    !$rparent->{_ci_close}
-                    || (
-                        !$rparent->{_ci_open_next}
-                        && ( ( $rparent->{_comma_count} || $last_type eq ',' )
-                            || $is_closing_type{$last_type} )
-                    )
-                )
-              )
-            {
-                # Be sure this is a block comment
-                my $lx       = $rtoken_K->[_LINE_INDEX_];
-                my $rK_range = $rlines->[$lx]->{_rK_range};
-                my $Kfirst;
-                if ($rK_range) { $Kfirst = $rK_range->[0] }
-                if ( defined($Kfirst) && $Kfirst == $KK ) {
-
-                    # Look for trailing closing token
-                    #    [ and possibly ',' or ';' ]
-                    $Kn = $self->K_next_code($KK) if ( !$Kn );
-                    my $Kc = $rparent->{_Kc};
-                    if (
-                           $Kn
-                        && $Kc
-                        && (
-                            $Kn == $Kc
-
-                            # only look for comma if -wbb=',' is set
-                            # to minimize changes to existing formatting
-                            || (   $rLL->[$Kn]->[_TYPE_] eq ','
-                                && $want_break_before_comma
-                                && $parent_container_type eq 'List' )
-
-                            # do not look ahead for a bare ';' because
-                            # it changes old formatting with little benefit.
-##                          || (   $rLL->[$Kn]->[_TYPE_] eq ';'
-##                                && $parent_container_type eq 'Block' )
-                        )
-                      )
-                    {
-
-                        # Be sure container has a level jump
-                        my $level_KK = $rLL->[$KK]->[_LEVEL_];
-                        my $level_Kc = $rLL->[$Kc]->[_LEVEL_];
-                        if ( $level_Kc < $level_KK ) {
-                            $ci_this = 0;
-                        }
-                    }
-                }
-            }
-
-            $ci_next = $ci_this;
-            $rtoken_K->[_CI_LEVEL_] = $ci_this;
-
-            # 'next' to avoid saving last_ values for blanks and commas
-            next;
-        }
-
-        #-------------------------
-        # 4. Semicolons and Labels
-        #-------------------------
+        #---------------------------------
+        # Section 5. Semicolons and Labels
+        #---------------------------------
         # The next token after a ';' and label (type 'J') starts a new stmt
         # The ci after a C-style for ';' (type 'f') is handled similarly.
         # TODO: There is type 'f' redundant coding in sub respace which can
@@ -7333,9 +7321,9 @@ sub set_ci {
             if ( $is_closing_type{$last_type} ) { $ci_this = $ci_last }
         }
 
-        #------------
-        # 5. Keywords
-        #------------
+        #--------------------
+        # Section 6. Keywords
+        #--------------------
         # Undo ci after a format statement
         elsif ( $type eq 'k' ) {
             if ( substr( $token, 0, 6 ) eq 'format' ) {
@@ -7343,9 +7331,9 @@ sub set_ci {
             }
         }
 
-        #----------
-        # 6. Commas
-        #----------
+        #------------------
+        # Section 7. Commas
+        #------------------
         # A comma and the subsequent item normally have ci undone
         # unless ci has been set at a lower level
         elsif ( $type eq ',' ) {
@@ -7355,9 +7343,9 @@ sub set_ci {
             }
         }
 
-        #-------------------------
-        # 7. Hanging side comments
-        #-------------------------
+        #---------------------------------
+        # Section 8. Hanging side comments
+        #---------------------------------
         # Treat hanging side comments like blanks
         elsif ( $type eq 'q' && $token eq EMPTY_STRING ) {
             $ci_next = $ci_this;
@@ -7368,10 +7356,7 @@ sub set_ci {
             next;
         }
 
-        #-----------------------------------------------------------------
-        # Development test: ci_this should match the ci from the tokenizer
-        # except where the new value makes an improvement.
-        #-----------------------------------------------------------------
+        # Save debug info if requested
         DEBUG_SET_CI && do {
 
             my $seqno = $rtoken_K->[_TYPE_SEQUENCE_];
@@ -7391,7 +7376,6 @@ sub set_ci {
             my $block_type;
             $block_type = $rblock_type_of_seqno->{$seqno} if ($seqno);
             $block_type = EMPTY_STRING unless ($block_type);
-            if ( !defined($block_type) ) { $block_type = EMPTY_STRING }
             my $ptype = $rparent->{_container_type};
             my $pname = $ptype;
 
@@ -7405,19 +7389,24 @@ $lno\t$ci\t$ci_this\t$ci_next\t$last_type\t$last_tok\t$type\t$tok\t$seqno\t$leve
 EOM
         };
 
+        #----------------------------------
+        # Store the ci value for this token
+        #----------------------------------
         $rtoken_K->[_CI_LEVEL_] = $ci_this
 
           # do not store in hybrid testing mode
           if ( !$ci_comments_only );
 
-        # Remember last nonblank, non-comment token info
+        # Remember last nonblank, non-comment token info for the next pass
         $ci_last    = $ci_this;
         $last_token = $token;
         $last_type  = $type;
 
-    }
+    }    ## End main loop over tokens
 
-    # End main loop
+    #----------------------
+    # Post-loop operations:
+    #----------------------
 
     # if the logfile is saved, we need to save the leading ci of
     # each old line of code.