]> git.donarmstrong.com Git - perltidy.git/commitdiff
improve side comment locations
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 29 Dec 2020 14:48:24 +0000 (06:48 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 29 Dec 2020 14:48:24 +0000 (06:48 -0800)
lib/Perl/Tidy/VerticalAligner.pm
local-docs/BugLog.pod
t/snippets/expect/comments.comments2
t/snippets/expect/comments.comments3
t/snippets/expect/comments.comments4
t/snippets/expect/comments.def
t/snippets/expect/mangle4.def
t/snippets/packing_list.txt
t/snippets17.t
t/snippets22.t

index 6a87ddc4d2b4ce0be368792352d6c2ceb8bf4599..32fb0ea98acc8e998a74fa789a8d9c7be1072a34 100644 (file)
@@ -677,7 +677,7 @@ EOM
     # if none exists.
     # --------------------------------------------------------------------
     $self->make_side_comment( $rtokens, $rfields, $rpatterns, $rfield_lengths,
-        $level_end );
+        $level_end, $level );
     $jmax = @{$rfields} - 1;
 
     # --------------------------------------------------------------------
@@ -789,12 +789,17 @@ sub join_hanging_comment {
     return 1;
 }
 
+# Switch for comparing side comment forgetting methods. This can eventually
+# be removed.
+use constant TEST_OLD_SIDE_COMMENT_METHOD => 0;
+
 sub make_side_comment {
 
     # create an empty side comment if none exists
 
-    my ( $self, $rtokens, $rfields, $rpatterns, $rfield_lengths, $level_end ) =
-      @_;
+    my ( $self, $rtokens, $rfields, $rpatterns, $rfield_lengths, $level_end,
+        $level )
+      = @_;
 
     my $jmax = @{$rfields} - 1;
 
@@ -810,15 +815,19 @@ sub make_side_comment {
     # line has a side comment..
     else {
 
-        # don't remember old side comment location for very long
-        my $line_number = $self->get_output_line_number();
-        if (
-            $line_number - $self->[_last_side_comment_line_number_] > 12
+        # don't remember old side comment location for very long...
+        my $line_number   = $self->get_output_line_number();
+        my $last_sc_level = $self->[_last_side_comment_level_];
 
-            # and don't remember comment location across block level changes
-            || (   $level_end < $self->[_last_side_comment_level_]
-                && $rfields->[0] =~ /^}/ )
-          )
+        # OLD METHOD: forget side comment location across block level changes.
+        # NEW METHOD: forget side comment location unless old level matches
+        # either leading or trailing level.
+        my $is_level_change = TEST_OLD_SIDE_COMMENT_METHOD
+          ? $level_end < $last_sc_level  && $rfields->[0] =~ /^}/
+          : $level_end != $last_sc_level && $level != $last_sc_level;
+
+          if ( $line_number - $self->[_last_side_comment_line_number_] > 12
+            || $is_level_change )
         {
             $self->forget_side_comment();
         }
@@ -1169,6 +1178,7 @@ sub check_match {
             $GoToMsg = "Not all tokens match: $imax_align != $jlimit\n";
             goto NO_MATCH;
         }
+
     }
 
     # The tokens match, but the lines must have identical number of
@@ -1178,6 +1188,24 @@ sub check_match {
         goto NO_MATCH;
     }
 
+    # FOR TESTING ONLY: if we do not mix lines with and without side comments
+    # in the top-down sweep, then lines without side comments cannot influence
+    # side comment locations.  The results of this test are interesting but not
+    # always better.  But it could be that some combination of the two
+    # possibilities can give better overall results, so this test patch is
+    # temporarily retained for further experimentation.
+    if (0) { #<<<
+    my $sc_len_base = $base_line->get_rfield_lengths()->[$maximum_field_index];
+    my $sc_len      = $new_line->get_rfield_lengths()->[$jmax];
+    if ( $sc_len == 0 && $sc_len_base > 0 || $sc_len > 0 && $sc_len_base == 0 )
+    {
+        EXPLAIN_CHECK_MATCH
+          && print
+          "match but sc mismatch, imax_align=$imax_align, jmax=$jmax\n";
+        return ( 1, $jlimit );
+    }
+    }
+
     # The tokens match. Now See if there is space for this line in the
     # current group.
     if ( $self->check_fit( $new_line, $base_line ) && !TEST_SWEEP_ONLY ) {
index 125ce4fc379c67dd6a37aa505850b6666cf4615c..98eb5a069ae2538b82d3badf6b05f5cf6ab00d9c 100644 (file)
 
 =over 4
 
+=item B<Improve rule for forgetting last side comment location>
+
+The code which aligns side comments remembers the most recent side comment and
+in some cases tries to start aligning at that column for later side comments.
+Sometimes the old side comment column was being remembered too long, causing
+occasional poor formatting and causing a noticable and unexpected drift of side
+comment locations to the right.  The rule for forgetting the previous side
+comment column has been modified to reduce this problem.  The new rule is
+essentially to forget the previous side comment location at a new side comment
+with different indentation level or significant number of lines without side
+comments (about 12).  The previous implementation forgetting changes in
+indentation level across code blocks only.  Below is an example where the old
+method gets into trouble and the new method is ok:
+
+        # OLD:
+        foreach my $r (@$array) {
+            $Dat{Data}{ uc $r->[0] } = join( ";", @$r );    # store all info
+            my $name = $Dat{GivenName}{ uc $r->[0] } || $r->[1];
+
+            # pass array as ad-hoc string, mark missing values
+            $Dat{Data}{ uc $r->[0] } = join(
+                ";",
+                (
+                    uc $r->[0], uc $name,                   # symbol, name
+                    $r->[2],    $r->[3], $r->[4],           # price, date, time
+                    $r->[5],    $r->[6],                    # change, %change
+                    $r->[7],    "-", "-", "-",    # vol, avg vol, bid,ask
+                    $r->[8],               $r->[9],     # previous, open
+                    "$r->[10] - $r->[11]", $r->[12],    # day range,year range,
+                    "-",                   "-", "-", "-", "-"
+                )
+            );                                          # eps,p/e,div,yld,cap
+        }
+
+The second side comment is at a deeper indentation level but was not being forgotten,
+causing line length limits to interfere with later alignment. The new rule gives
+a better result:
+
+        # NEW:
+        foreach my $r (@$array) {
+            $Dat{Data}{ uc $r->[0] } = join( ";", @$r );    # store all info
+            my $name = $Dat{GivenName}{ uc $r->[0] } || $r->[1];
+
+            # pass array as ad-hoc string, mark missing values
+            $Dat{Data}{ uc $r->[0] } = join(
+                ";",
+                (
+                    uc $r->[0], uc $name,               # symbol, name
+                    $r->[2],    $r->[3], $r->[4],       # price, date, time
+                    $r->[5],    $r->[6],                # change, %change
+                    $r->[7],    "-", "-", "-",          # vol, avg vol, bid,ask
+                    $r->[8],               $r->[9],     # previous, open
+                    "$r->[10] - $r->[11]", $r->[12],    # day range,year range,
+                    "-",                   "-", "-", "-", "-"
+                )
+            );    # eps,p/e,div,yld,cap
+        }
+
+The following exampel shows an unexpected alignment in the cascade of trailing
+comments which are aligned but slowly separating from their closing containers:
+
+    # OLD:
+    {
+        $a = [
+            Cascade    => $menu_cb,
+            -menuitems => [
+                [ Checkbutton => 'Oil checked', -variable => \$OIL ],
+                [
+                    Button   => 'See current values',
+                    -command => [
+                        \&see_vars, $TOP,
+
+                    ],    # end see_vars
+                ],        # end button
+            ],            # end checkbutton menuitems
+        ];                # end checkbuttons cascade
+    }
+
+This was caused by forgetting side comments only across code block changes. The new
+result is more reasonable: 
+
+    # NEW:
+    {
+        $a = [
+            Cascade    => $menu_cb,
+            -menuitems => [
+                [ Checkbutton => 'Oil checked', -variable => \$OIL ],
+                [
+                    Button   => 'See current values',
+                    -command => [
+                        \&see_vars, $TOP,
+
+                    ],    # end see_vars
+                ],    # end button
+            ],    # end checkbutton menuitems
+        ];    # end checkbuttons cascade
+    }
+
+This change will cause occasional differences in side comment locations from
+previous versions but overall it gives fewer unexpected results so it is a worthwhile
+change.  29-Dec-2020.
+
 =item B<Fixed very minor inconsistency in redefining lists after prune step>
 
 In rare cases it is necessary to update the type of lists, and this
 influences vertical alignment. This update fixes a minor inconsistency
 in doing this.  In some rare cases with complex list elements vertical
-alignment can be improved.
+alignment can be improved.  27 Dec, 2020, 751faec.
 
             # OLD
             return join( '',
@@ -59,7 +161,7 @@ below:
 
 The rule in sub 'two_line_pad' was updated to allow alignment of any lists
 if the patterns match exactly (all numbers in this case).  Updated
-27-Dec-2020.
+27-Dec-2020, 035d2b7.
 
 =item B<Avoid -lp style formatting of lists containing multiline qw quotes>
 
@@ -90,6 +192,8 @@ quotes.  This update avoids this problem by not formatting such lists with the
         @trig,
     );
 
+27-Dec-2020, 948c9bd.
+
 =item B<improve formatting of multiline qw>
 
 This update adds a sequence numbering system for multiline qw quotes.  In the
@@ -165,6 +269,8 @@ Here is another example
       Class::XYZ::Overload
       ); #<-- outdented same as the line with 'for qw('
 
+26 Dec 2020, cdbf0e4.
+
 =item B<improve list marking method>
 
 In the process of making vertical alignments, lines which are simple lists of
@@ -206,7 +312,7 @@ For example (note padding after first comma)
     my ( $ym, $al, $cov, $bet, $olda, $ochisq, $di, $pivt, $info ) =
       map { null } ( 0 .. 8 );
 
-This update was made 22 Dec 2020.
+This update was made 22 Dec 2020, 36d4c35.
 
 =item B<Fix git #51, closing quote pattern delimiters not following -cti flag settings>
 
@@ -266,7 +372,7 @@ but (';' remains indented):
         @trig
     );
 
-This update was added 18 Dec 2020 and modified 24 Dec.
+This update was added 18 Dec 2020 and modified 24 Dec 2020, 538688f.
 
 =item B<Update manual pages regarding issue git #50>
 
@@ -277,7 +383,7 @@ perltidy does not change whitespace.  This update was added 17 Dec 2020.
 
 Moved inner part of sub check_match into sub match_line_pair in order to
 make info available earlier.  This gave some minor alignment improvements.
-This was done 16 Dec 2020.
+This was done 16 Dec 2020, 7ba4f3b.
 
     # OLD:
     @tests = (
@@ -333,7 +439,7 @@ achieved by the subsequent 'sweep' pass.
     elsif ( $cmd eq "remove" )      { $remove      = 1; last; }
     elsif ( $cmd eq "help" )        { $help        = 1; last; }
 
-This update was made 14 Dec 2020.
+This update was made 14 Dec 2020, 44e0afa.
 
 =item B<Improved some marginal vertical alignments>
 
index 713e9d33570b4a59e972e6ff5c2a7ad76d060f1c..5eb3693a20cc600f6bff926e94b139fb7fcea229 100644 (file)
@@ -32,7 +32,7 @@ sub macro_get_names {          #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }                #end level 4
+            }          #end level 4
         }          # end level 3
     }          # end level 2
 }          # end level 1
index 077d0818adb89b37f76deee40bf4598a83021d87..eecb8204a0ade445a451ef07ab55d9508f58d451 100644 (file)
@@ -48,7 +48,7 @@ sub macro_get_names {    #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }          #end level 4
+            }    #end level 4
         }    # end level 3
     }    # end level 2
 }    # end level 1
index 58638d2b9171ee4126a8fde80638c4a565ed0389..dee97983ef21626ec4d7ec6b45ee131571ce79c9 100644 (file)
@@ -49,7 +49,7 @@ sub macro_get_names {    #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }          #end level 4
+            }    #end level 4
         }    # end level 3
     }    # end level 2
 }    # end level 1
index 58ffcdf78d4818a696238a4823535cbc20a194bc..431e492c1361321f46e822fb70d502a417b5bd72 100644 (file)
@@ -46,7 +46,7 @@ sub macro_get_names {    #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }          #end level 4
+            }    #end level 4
         }    # end level 3
     }    # end level 2
 }    # end level 1
index ed062760be67e15b621307f9d890e78f1fe4ce64..84d1986c09c17d0e4ddfb22997bcab038a67cd8d 100644 (file)
@@ -13,5 +13,5 @@ sub t086 (    #foo)))
       333     #foo)))
     ,         #foo)))
     ,         #foo)))
-  )           #foo)))
+  )    #foo)))
 { $a . $b }
index 78559e5b5dea876a8fc1ed650d164e735713b6a9..158c7fdaa76b84636d5d47b0b744ba37fc4ebb24 100644 (file)
 ../snippets23.t        align34.def
 ../snippets23.t        git47.def
 ../snippets23.t        git47.git47
+../snippets23.t        qw.def
 ../snippets3.t ce_wn1.ce_wn
 ../snippets3.t ce_wn1.def
 ../snippets3.t colin.colin
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
-../snippets23.t        qw.def
index 9bfe63686fe81a8490e74dc0859e4232f8c78c18..ab2d0ba19400829d25cbdcbf68dbb8be3b3bb235 100644 (file)
@@ -464,7 +464,7 @@ sub macro_get_names {          #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }                #end level 4
+            }          #end level 4
         }          # end level 3
     }          # end level 2
 }          # end level 1
@@ -545,7 +545,7 @@ sub macro_get_names {    #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }          #end level 4
+            }    #end level 4
         }    # end level 3
     }    # end level 2
 }    # end level 1
@@ -639,7 +639,7 @@ sub macro_get_names {    #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }          #end level 4
+            }    #end level 4
         }    # end level 3
     }    # end level 2
 }    # end level 1
@@ -738,7 +738,7 @@ sub macro_get_names {    #
         {
             {
                 { ${msg} = "Hello World!"; print "My message: ${msg}\n"; }
-            }          #end level 4
+            }    #end level 4
         }    # end level 3
     }    # end level 2
 }    # end level 1
index 92563c258b62f8068a3886aed94ba6591889750b..9930115996d15c16f5e204e34ae28c68715df6c4 100644 (file)
@@ -549,7 +549,7 @@ sub t086 (    #foo)))
       333     #foo)))
     ,         #foo)))
     ,         #foo)))
-  )           #foo)))
+  )    #foo)))
 { $a . $b }
 #11...........
         },