From 24a11d3caf30b0af288cda5131ab1ccb29ef7fe0 Mon Sep 17 00:00:00 2001
From: Steve Hancock <perltidy@users.sourceforge.net>
Date: Thu, 13 May 2021 08:07:23 -0700
Subject: [PATCH] Adjust tolerances for some line length tests

---
 lib/Perl/Tidy/Formatter.pm | 40 ++++++++++++++++++++++++++++++--------
 local-docs/BugLog.pod      | 10 ++++++++++
 2 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm
index 1704e69d..5fecb7b3 100644
--- a/lib/Perl/Tidy/Formatter.pm
+++ b/lib/Perl/Tidy/Formatter.pm
@@ -15347,17 +15347,34 @@ sub set_continuation_breaks {
     my ( @has_broken_sublist, @dont_align, @want_comma_break );
 
     my $length_tol;
+    my $length_tol_multiline_increase;
 
     sub initialize_scan_list {
         @dont_align         = ();
         @has_broken_sublist = ();
         @want_comma_break   = ();
 
-        # Use an increased line length tolerance when -ci > -i
-        # to avoid blinking states (case b923 and others).
+        # Define a tolerance to use a tolerance when checking if closed
+        # containers will fit on one line.  This is necessary to avoid
+        # formatting instability. The basic tolerance is based on the
+        # following:
+
+        # - Always allow for at least one extra space after a closing token so
+        # that we do not strand a comma or semicolon. (oneline.t).
+
+        # - Use an increased line length tolerance when -ci > -i to avoid
+        # blinking states (case b923 and others).
         $length_tol =
           1 + max( 0, $rOpts_continuation_indentation - $rOpts_indent_columns );
 
+        # In addition, use a few characters of extra tolerance for broken lines
+        # when -lp is used to help prevent instability. This is currently only
+        # necessary for -lp which has a more variable indentation.  At least 3
+        # characters have been found to be required.
+        # Fixes cases b1059 b1063 b1117.
+        $length_tol_multiline_increase = 0;
+        if ($rOpts_line_up_parentheses) { $length_tol_multiline_increase = 3 }
+
         return;
     }
 
@@ -16127,15 +16144,22 @@ sub set_continuation_breaks {
                 # mark term as long if the length between opening and closing
                 # parens exceeds allowed line length
                 if ( !$is_long_term && $saw_opening_structure ) {
+
                     my $i_opening_minus =
                       $self->find_token_starting_list($i_opening);
 
-                    # Note: we have to allow for at least one extra space after
-                    # a closing token so that we do not strand a comma or
-                    # semicolon. (oneline.t).
-                    $is_long_term =
-                      $self->excess_line_length( $i_opening_minus, $i ) >
-                      -$length_tol;
+                    my $excess =
+                      $self->excess_line_length( $i_opening_minus, $i );
+
+                    my $tol = $length_tol;
+                    if (   $length_tol_multiline_increase
+                        && $self->[_ris_broken_container_]->{$type_sequence} )
+                    {
+                        $tol += $length_tol_multiline_increase;
+                    }
+
+                    $is_long_term = $excess + $tol > 0;
+
                 } ## end if ( !$is_long_term &&...)
 
                 # We've set breaks after all comma-arrows.  Now we have to
diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod
index 0a6fcc1a..c044d1d9 100644
--- a/local-docs/BugLog.pod
+++ b/local-docs/BugLog.pod
@@ -2,6 +2,16 @@
 
 =over 4
 
+=item B<Adjust tolerances for some line length tests>
+
+Random testing produced some edge cases of unstable formatting involving the -lp
+format.  These were fixed by using a slightly larger tolerance in the length test
+for containers which were broken in the input file.
+
+This fixes cases b1059 b1063 b1117.
+
+13 May 2021.
+
 =item B<Do not apply -lp formatting to containers with here-doc text>
 
 If a container contains text of a here-doc then the indentation is fixed by
-- 
2.39.5