]> git.donarmstrong.com Git - perltidy.git/commitdiff
skip needless flush calls
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 7 Nov 2023 13:47:03 +0000 (05:47 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 7 Nov 2023 13:47:03 +0000 (05:47 -0800)
lib/Perl/Tidy/Formatter.pm
lib/Perl/Tidy/VerticalAligner.pm

index 5249d7ca555bd9f1f7f97aeeea897bc79aedbae6..3442cb789ada4fdfe44b8f0d75c49541a1ab89bf 100644 (file)
     my $iline = -1;
     foreach my $line_of_tokens ( @{$rlines} ) {
         $iline++;
-        my $line_type = $line_of_tokens->{_line_type};
-        if ( $line_type eq 'CODE' ) {
-
-            # Get the old number of tokens on this line
-            my $rK_range_old = $line_of_tokens->{_rK_range};
-            my ( $Kfirst_old, $Klast_old ) = @{$rK_range_old};
-            my $Kdiff_old = 0;
-            if ( defined($Kfirst_old) ) {
-                $Kdiff_old = $Klast_old - $Kfirst_old;
-            }
-
-            # Find the range of NEW K indexes for the line:
-            # $Kfirst = index of first token on line
-            # $Klast  = index of last token on line
-            my ( $Kfirst, $Klast );
-
-            my $Knext_beg = $Knext;    # this will be $Kfirst if we find tokens
-
-            # Optimization: Although the actual K indexes may be completely
-            # changed after respacing, the number of tokens on any given line
-            # will often be nearly unchanged.  So we will see if we can start
-            # our search by guessing that the new line has the same number
-            # of tokens as the old line.
-            my $Knext_guess = $Knext + $Kdiff_old;
-            if (   $Knext_guess > $Knext
-                && $Knext_guess < $Kmax
-                && $rLL->[$Knext_guess]->[_LINE_INDEX_] <= $iline )
-            {
+        next if ( $line_of_tokens->{_line_type} ne 'CODE' );
+
+        # Get the old number of tokens on this line
+        my $rK_range_old = $line_of_tokens->{_rK_range};
+        my ( $Kfirst_old, $Klast_old ) = @{$rK_range_old};
+        my $Kdiff_old = 0;
+        if ( defined($Kfirst_old) ) {
+            $Kdiff_old = $Klast_old - $Kfirst_old;
+        }
+
+        # Find the range of NEW K indexes for the line:
+        # $Kfirst = index of first token on line
+        # $Klast  = index of last token on line
+        my ( $Kfirst, $Klast );
+
+        my $Knext_beg = $Knext;    # this will be $Kfirst if we find tokens
+
+        # Optimization: Although the actual K indexes may be completely
+        # changed after respacing, the number of tokens on any given line
+        # will often be nearly unchanged.  So we will see if we can start
+        # our search by guessing that the new line has the same number
+        # of tokens as the old line.
+        my $Knext_guess = $Knext + $Kdiff_old;
+        if (   $Knext_guess > $Knext
+            && $Knext_guess < $Kmax
+            && $rLL->[$Knext_guess]->[_LINE_INDEX_] <= $iline )
+        {
 
-                # the guess is good, so we can start our search here
-                $Knext = $Knext_guess + 1;
-            }
+            # the guess is good, so we can start our search here
+            $Knext = $Knext_guess + 1;
+        }
 
-            while ($Knext <= $Kmax
-                && $rLL->[$Knext]->[_LINE_INDEX_] <= $iline )
-            {
-                $Knext++;
-            }
+        while ($Knext <= $Kmax
+            && $rLL->[$Knext]->[_LINE_INDEX_] <= $iline )
+        {
+            $Knext++;
+        }
 
-            if ( $Knext > $Knext_beg ) {
+        if ( $Knext > $Knext_beg ) {
 
-                $Klast = $Knext - 1;
+            $Klast = $Knext - 1;
 
-                # Delete any terminal blank token
-                if ( $rLL->[$Klast]->[_TYPE_] eq 'b' ) { $Klast -= 1 }
+            # Delete any terminal blank token
+            if ( $rLL->[$Klast]->[_TYPE_] eq 'b' ) { $Klast -= 1 }
 
-                if ( $Klast < $Knext_beg ) {
-                    $Klast = undef;
-                }
-                else {
+            if ( $Klast < $Knext_beg ) {
+                $Klast = undef;
+            }
+            else {
 
-                    $Kfirst = $Knext_beg;
+                $Kfirst = $Knext_beg;
 
-                    # Save ranges of non-comment code. This will be used by
-                    # sub keep_old_line_breaks.
-                    if ( $rLL->[$Kfirst]->[_TYPE_] ne '#' ) {
-                        push @Krange_code_without_comments, [ $Kfirst, $Klast ];
-                    }
+                # Save ranges of non-comment code. This will be used by
+                # sub keep_old_line_breaks.
+                if ( $rLL->[$Kfirst]->[_TYPE_] ne '#' ) {
+                    push @Krange_code_without_comments, [ $Kfirst, $Klast ];
+                }
 
-                    # Only save ending K indexes of code types which are blank
-                    # or 'VER'.  These will be used for a convergence check.
-                    # See related code in sub 'convey_batch_to_vertical_aligner'
-                    my $CODE_type = $line_of_tokens->{_code_type};
-                    if (  !$CODE_type
-                        || $CODE_type eq 'VER' )
-                    {
-                        push @Klast_valign_code, $Klast;
-                    }
+                # Only save ending K indexes of code types which are blank
+                # or 'VER'.  These will be used for a convergence check.
+                # See related code in sub 'convey_batch_to_vertical_aligner'
+                my $CODE_type = $line_of_tokens->{_code_type};
+                if (  !$CODE_type
+                    || $CODE_type eq 'VER' )
+                {
+                    push @Klast_valign_code, $Klast;
                 }
             }
+        }
 
-            # It is only safe to trim the actual line text if the input
-            # line had a terminal blank token. Otherwise, we may be
-            # in a quote.
-            if ( $line_of_tokens->{_ended_in_blank_token} ) {
-                $line_of_tokens->{_line_text} =~ s/\s+$//;
-            }
-            $line_of_tokens->{_rK_range} = [ $Kfirst, $Klast ];
+        # It is only safe to trim the actual line text if the input
+        # line had a terminal blank token. Otherwise, we may be
+        # in a quote.
+        if ( $line_of_tokens->{_ended_in_blank_token} ) {
+            $line_of_tokens->{_line_text} =~ s/\s+$//;
+        }
+        $line_of_tokens->{_rK_range} = [ $Kfirst, $Klast ];
 
-            # Deleting semicolons can create new empty code lines
-            # which should be marked as blank
-            if ( !defined($Kfirst) ) {
-                my $CODE_type = $line_of_tokens->{_code_type};
-                if ( !$CODE_type ) {
-                    $line_of_tokens->{_code_type} = 'BL';
-                }
+        # Deleting semicolons can create new empty code lines
+        # which should be marked as blank
+        if ( !defined($Kfirst) ) {
+            my $CODE_type = $line_of_tokens->{_code_type};
+            if ( !$CODE_type ) {
+                $line_of_tokens->{_code_type} = 'BL';
             }
-            else {
+        }
+        else {
 
-                #---------------------------------------------------
-                # save indexes of all lines with a 'q' at either end
-                # for later use by sub find_multiline_qw
-                #---------------------------------------------------
-                if (   $rLL->[$Kfirst]->[_TYPE_] eq 'q'
-                    || $rLL->[$Klast]->[_TYPE_] eq 'q' )
-                {
-                    push @{$rqw_lines}, $iline;
-                }
+            #---------------------------------------------------
+            # save indexes of all lines with a 'q' at either end
+            # for later use by sub find_multiline_qw
+            #---------------------------------------------------
+            if (   $rLL->[$Kfirst]->[_TYPE_] eq 'q'
+                || $rLL->[$Klast]->[_TYPE_] eq 'q' )
+            {
+                push @{$rqw_lines}, $iline;
             }
         }
     }
@@ -16712,24 +16710,27 @@ EOM
         return;
     } ## end sub flush_vertical_aligner
 
-    # flush is called to output any tokens in the pipeline, so that
-    # an alternate source of lines can be written in the correct order
     sub flush {
         my ( $self, $CODE_type_flush ) = @_;
 
-        # end the current batch with 1 exception
+        # sub flush is called to output any tokens in the pipeline, so that
+        # an alternate source of lines can be written in the correct order
 
         $index_start_one_line_block = undef;
 
-        # Exception: if we are flushing within the code stream only to insert
-        # blank line(s), then we can keep the batch intact at a weld. This
-        # improves formatting of -ce.  See test 'ce1.ce'
-        if ( $CODE_type_flush && $CODE_type_flush eq 'BL' ) {
-            $self->end_batch() if ( $max_index_to_go >= 0 );
-        }
+        # End the current batch, if it holds any tokens, with 1 exception
+        if ( $max_index_to_go >= 0 ) {
 
-        # otherwise, we have to shut things down completely.
-        else { $self->flush_batch_of_CODE() }
+            # Exception: if we are flushing within the code stream only to
+            # insert blank line(s), then we can keep the batch intact at a
+            # weld. This improves formatting of -ce.  See test 'ce1.ce'
+            if ( $CODE_type_flush && $CODE_type_flush eq 'BL' ) {
+                $self->end_batch();
+            }
+
+            # otherwise, we have to shut things down completely.
+            else { $self->flush_batch_of_CODE() }
+        }
 
         $self->flush_vertical_aligner();
         return;
index 744b8ea2dfb366de6bb8bb2e4db9165d85337827..87a9f8a54be321c6df70481c767c87005f7bb168 100644 (file)
@@ -398,7 +398,8 @@ sub flush {
     # push things out the pipeline...
 
     # push out any current group lines
-    $self->_flush_group_lines();
+    $self->_flush_group_lines()
+      if ( @{ $self->[_rgroup_lines_] } );
 
     # then anything left in the cache of step_B
     $self->_flush_step_B_cache();
@@ -751,7 +752,8 @@ sub valign_input {
       )
     {
 
-        $self->_flush_group_lines( $level - $group_level );
+        $self->_flush_group_lines( $level - $group_level )
+          if ( @{ $self->[_rgroup_lines_] } );
 
         $group_level                         = $level;
         $self->[_group_level_]               = $group_level;
@@ -779,7 +781,8 @@ sub valign_input {
             return;
         }
         else {
-            $self->_flush_group_lines();
+            $self->_flush_group_lines()
+              if ( @{ $self->[_rgroup_lines_] } );
         }
     }
 
@@ -845,7 +848,8 @@ sub valign_input {
             if (   $rgroup_lines->[0]->{'jmax'} > 1
                 || $self->[_zero_count_] > 3 )
             {
-                $self->_flush_group_lines();
+                $self->_flush_group_lines()
+                  if ( @{ $self->[_rgroup_lines_] } );
 
                 # Update '$rgroup_lines' - it will become a ref to empty array.
                 # This allows avoiding a call to get_group_line_count below.
@@ -959,14 +963,16 @@ sub valign_input {
 
     # output this group if it ends in a terminal else or ternary line
     if ( defined($j_terminal_match) ) {
-        $self->_flush_group_lines();
+        $self->_flush_group_lines()
+          if ( @{ $self->[_rgroup_lines_] } );
     }
 
     # Force break after jump to lower level
     elsif ($level_end < $level
         || $is_closing_token{ substr( $rfields->[0], 0, 1 ) } )
     {
-        $self->_flush_group_lines(-1);
+        $self->_flush_group_lines(-1)
+          if ( @{ $self->[_rgroup_lines_] } );
     }
 
     else {