]> git.donarmstrong.com Git - perltidy.git/commitdiff
added new parameters -vc -vsc -vbc
authorSteve Hancock <perltidy@users.sourceforge.net>
Thu, 16 Sep 2021 23:17:27 +0000 (16:17 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Thu, 16 Sep 2021 23:17:27 +0000 (16:17 -0700)
16 files changed:
bin/perltidy
lib/Perl/Tidy.pm
lib/Perl/Tidy/Formatter.pm
lib/Perl/Tidy/VerticalAligner.pm
t/snippets/expect/novalign.def
t/snippets/expect/novalign.novalign [deleted file]
t/snippets/expect/novalign.novalign1 [new file with mode: 0644]
t/snippets/expect/novalign.novalign2 [new file with mode: 0644]
t/snippets/expect/novalign.novalign3 [new file with mode: 0644]
t/snippets/novalign.in
t/snippets/novalign.par [deleted file]
t/snippets/novalign1.par [new file with mode: 0644]
t/snippets/novalign2.par [new file with mode: 0644]
t/snippets/novalign3.par [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets25.t

index 49c7411a940f80d38ed0a6054ca5023c2f674281..b847c2526640d8019c4837d8fc7c5e902de88fb6 100755 (executable)
@@ -4058,6 +4058,32 @@ For example,
 The default is to use vertical alignment, but bertical alignment can be
 completely turned of with the B<-novalign> flag.
 
+A lower level of control of vertical alignment is possible with three parameters
+B<-vc>, B<-vsc>, and B<-vbc>. These independently control alignment
+of code, side comments and block comments.  They are described in the
+next section.
+
+The parameter B<-valign> is in fact an alias for B<-vc -vsc -vbc>, and its
+negative B<-novalign> is an alias for B<-nvc -nvsc -nvbc>.
+
+=item B<Controlling code alignment with --valign-code or -vc>
+
+The B<-vc> flag enables alignment of code symbols such as B<=>.  The default is B<-vc>.
+
+=item B<Controlling side comment alignment with --valign-side-comments or -vsc>
+
+The B<-vsc> flag enables alignment of side comments and is enabled by default.  If side
+comment aligment is disabled with B<-nvsc> they will appear at a fixed space from the
+preceding code token.  The default is B<-vsc>
+
+=item B<Controlling block comment alignment with --valign-block-comments or -vbc>
+
+When B<-vbc> is enabled, block comments can become aligned for example if one
+comment of a consecutive sequence of comments becomes outdented due a length in
+excess of the maximum line length.  If this occurs, the entire group of
+comments will remain aligned and be outdented by the same amount.  This coordinated
+alignment will not occur if B<-nvbc> is set.  The default is B<-vbc>.
+
 =back
 
 =head2 Other Controls
index b1fadb17cbac9fdcaa78bb7aae7bc9cc5a5cb2bb..ffbfc4c483c158f8b65c632cac2cc933631199e1 100644 (file)
@@ -1,3 +1,4 @@
+#!/usr/bin/perl
 #
 ###########################################################
 #
@@ -2125,7 +2126,6 @@ sub generate_options {
     #                                       which is mainly for debugging
     # scl --> short-concatenation-item-length   # helps break at '.'
     # recombine                           # for debugging line breaks
-    # valign                              # for debugging vertical alignment
     # I   --> DIAGNOSTICS                 # for debugging [**DEACTIVATED**]
     ######################################################################
 
@@ -2181,7 +2181,6 @@ sub generate_options {
       no-profile
       npro
       recombine!
-      valign!
       notidy
     );
 
@@ -2323,6 +2322,9 @@ sub generate_options {
     $add_option->( 'want-left-space',                           'wls',   '=s' );
     $add_option->( 'want-right-space',                          'wrs',   '=s' );
     $add_option->( 'space-prototype-paren',                     'spp',   '=i' );
+    $add_option->( 'valign-code',                               'vc',    '!' );
+    $add_option->( 'valign-block-comments',                     'vbc',   '!' );
+    $add_option->( 'valign-side-comments',                      'vsc',   '!' );
 
     ########################################
     $category = 4;    # Comment controls
@@ -2660,7 +2662,9 @@ sub generate_options {
       noweld-nested-containers
       recombine
       nouse-unicode-gcstring
-      valign
+      valign-code
+      valign-block-comments
+      valign-side-comments
       short-concatenation-item-length=8
       space-for-semicolon
       space-backslash-quote=1
@@ -2817,6 +2821,9 @@ sub generate_options {
         'conv'       => [qw(it=4)],
         'nconv'      => [qw(it=1)],
 
+        'valign'   => [qw(vc vsc vbc)],
+        'novalign' => [qw(nvc nvsc nvbc)],
+
         # NOTE: This is a possible future shortcut.  But it will remain
         # deactivated until the -lpxl flag is no longer experimental.
         # 'line-up-function-parentheses' => [ qw(lp), q#lpxl=[ { F(2# ],
index b95b0a54433536a05b87e4754a279ea8932fcc68..133cc507a835567e45ff76f71a6fa9d2b83d6f6d 100644 (file)
@@ -183,6 +183,8 @@ my (
     $rOpts_tee_side_comments,
     $rOpts_variable_maximum_line_length,
     $rOpts_valign,
+    $rOpts_valign_code,
+    $rOpts_valign_side_comments,
     $rOpts_whitespace_cycle,
 
     # Static hashes initialized in a BEGIN block
@@ -1670,6 +1672,8 @@ EOM
     $rOpts_tee_pod                   = $rOpts->{'tee-pod'};
     $rOpts_tee_side_comments         = $rOpts->{'tee-side-comments'};
     $rOpts_valign                    = $rOpts->{'valign'};
+    $rOpts_valign_code               = $rOpts->{'valign-code'};
+    $rOpts_valign_side_comments      = $rOpts->{'valign-side-comments'};
     $rOpts_variable_maximum_line_length =
       $rOpts->{'variable-maximum-line-length'};
 
@@ -20127,6 +20131,7 @@ EOM
 
         my ( $self, $ri_first, $ri_last ) = @_;
         my $rspecial_side_comment_type = $self->[_rspecial_side_comment_type_];
+        my $rLL                        = $self->[_rLL_];
 
         my $ralignment_type_to_go;
         my $alignment_count = 0;
@@ -20161,28 +20166,52 @@ EOM
             my $token = $tokens_to_go[$max_i];
             my $KK    = $K_to_go[$max_i];
 
-            unless (
+            # Do not align various special side comments
+            my $do_not_align = (
 
                 # it is any specially marked side comment
                 ( defined($KK) && $rspecial_side_comment_type->{$KK} )
 
                 # or it is a static side comment
-                || (   $rOpts->{'static-side-comments'}
+                  || ( $rOpts->{'static-side-comments'}
                     && $token =~ /$static_side_comment_pattern/ )
 
-                # or a closing side comment
-                || (   $types_to_go[$i_terminal] eq '}'
+                  # or a closing side comment
+                  || ( $types_to_go[$i_terminal] eq '}'
                     && $tokens_to_go[$i_terminal] eq '}'
                     && $token =~ /$closing_side_comment_prefix_pattern/ )
-              )
+            );
+
+            # - For the particular combination -vc -nvsc, we put all side comments
+            #   at fixed locations. Note that we will lose hanging side comment
+            #   alignments. Otherwise, hsc's can move to strange locations.
+            # - For -nvc -nvsc we will make all side comments vertical alignments
+            #   because the vertical aligner will check for -nvsc and be able
+            #   to reduce the final padding to the side comments for long lines.
+            #   and keep hanging side comments aligned.
+            if (   !$do_not_align
+                && !$rOpts_valign_side_comments
+                && $rOpts_valign_code )
             {
+
+                $do_not_align = 1;
+                my $ipad = $max_i - 1;
+                if ( $types_to_go[$ipad] eq 'b' ) {
+                    my $pad_spaces =
+                      $rOpts->{'minimum-space-to-comment'} -
+                      $token_lengths_to_go[$ipad];
+                    $self->pad_token( $ipad, $pad_spaces );
+                }
+            }
+
+            if ( !$do_not_align ) {
                 $ralignment_type_to_go->[$max_i] = '#';
                 $alignment_count++;
             }
         }
 
-        # Nothing more to do if -novalign is set
-        if ( !$rOpts_valign ) {
+        # Nothing more to do on this line if -nvc is set
+        if ( !$rOpts_valign_code ) {
             return ( $ralignment_type_to_go, $alignment_count );
         }
 
@@ -20197,7 +20226,7 @@ EOM
             my $ibeg = $ri_first->[$line];
             my $iend = $ri_last->[$line];
 
-            # back up before did any side comment
+            # back up before any side comment
             if ( $iend > $i_terminal ) { $iend = $i_terminal }
 
             my $level_beg = $levels_to_go[$ibeg];
index ade5229f645ec4472fcf373b956e00d228507f5f..24ba98ab5398db0ae185367aca8c0a7064a9c830 100644 (file)
@@ -100,7 +100,9 @@ BEGIN {
         _rOpts_minimum_space_to_comment_     => $i++,
         _rOpts_maximum_line_length_          => $i++,
         _rOpts_variable_maximum_line_length_ => $i++,
-        _rOpts_valign_                       => $i++,
+        _rOpts_valign_code_                  => $i++,
+        _rOpts_valign_block_comments_        => $i++,
+        _rOpts_valign_side_comments_         => $i++,
 
         _last_level_written_            => $i++,
         _last_side_comment_column_      => $i++,
@@ -181,7 +183,9 @@ sub new {
     $self->[_rOpts_maximum_line_length_] = $rOpts->{'maximum-line-length'};
     $self->[_rOpts_variable_maximum_line_length_] =
       $rOpts->{'variable-maximum-line-length'};
-    $self->[_rOpts_valign_] = $rOpts->{'valign'};
+    $self->[_rOpts_valign_code_]           = $rOpts->{'valign-code'};
+    $self->[_rOpts_valign_block_comments_] = $rOpts->{'valign-block-comments'};
+    $self->[_rOpts_valign_side_comments_]  = $rOpts->{'valign-side-comments'};
 
     # Batch of lines being collected
     $self->[_rgroup_lines_]                = [];
@@ -514,8 +518,16 @@ sub valign_input {
     if ( $level < 0 ) { $level = 0 }
 
     # do not align code across indentation level changes
-    # or if vertical alignment is turned off for debugging
-    if ( $level != $group_level || $is_outdented || !$self->[_rOpts_valign_] ) {
+    # or if vertical alignment is turned off
+    if (
+           $level != $group_level
+        || $is_outdented
+        || ( $is_block_comment && !$self->[_rOpts_valign_block_comments_] )
+        || (   !$is_block_comment
+            && !$self->[_rOpts_valign_side_comments_]
+            && !$self->[_rOpts_valign_code_] )
+      )
+    {
 
         $self->_flush_group_lines( $level - $group_level );
 
@@ -4280,7 +4292,8 @@ sub is_good_side_comment_column {
     my $short_diff = SC_LONG_LINE_DIFF / ( 1 + $alev_diff * $num5 );
 
     goto FORGET
-      if ( $line_diff > $short_diff ) || !$self->[_rOpts_valign_];
+      if ( $line_diff > $short_diff
+        || !$self->[_rOpts_valign_side_comments_] );
 
     # RULE3: Forget a side comment if this line is at lower level and
     # ends a block
index 61f326db12ccf18399ded69625967202ce7738a8..d0f43a220f262e709218b7f9f8034a644cfaf47c 100644 (file)
@@ -1,9 +1,13 @@
+{
 # simple vertical alignment of '=' and '#'
-my $lines     = 0;    # checksum: #lines
-my $bytes     = 0;    # checksum: #bytes
-my $sum       = 0;    # checksum: system V sum
-my $patchdata = 0;    # saw patch data
-my $pos       = 0;    # start of patch data
-my $endkit    = 0;    # saw end of kit
-my $fail      = 0;    # failed
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines     = 0;    # checksum: #lines
+    my $bytes     = 0;    # checksum: #bytes
+    my $sum       = 0;    # checksum: system V sum
+    my $patchdata = 0;    # saw patch data
+    my $pos       = 0;    # start of patch data
+                          # a hanging side comment
+    my $endkit    = 0;    # saw end of kit
+    my $fail      = 0;    # failed
+}
 
diff --git a/t/snippets/expect/novalign.novalign b/t/snippets/expect/novalign.novalign
deleted file mode 100644 (file)
index ee672bc..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-# simple vertical alignment of '=' and '#'
-my $lines = 0;    # checksum: #lines
-my $bytes = 0;    # checksum: #bytes
-my $sum = 0;    # checksum: system V sum
-my $patchdata = 0;    # saw patch data
-my $pos = 0;    # start of patch data
-my $endkit = 0;    # saw end of kit
-my $fail = 0;    # failed
-
diff --git a/t/snippets/expect/novalign.novalign1 b/t/snippets/expect/novalign.novalign1
new file mode 100644 (file)
index 0000000..087a94e
--- /dev/null
@@ -0,0 +1,13 @@
+{
+    # simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines = 0;    # checksum: #lines
+    my $bytes = 0;    # checksum: #bytes
+    my $sum = 0;    # checksum: system V sum
+    my $patchdata = 0;    # saw patch data
+    my $pos = 0;    # start of patch data
+                    # a hanging side comment
+    my $endkit = 0;    # saw end of kit
+    my $fail = 0;    # failed
+}
+
diff --git a/t/snippets/expect/novalign.novalign2 b/t/snippets/expect/novalign.novalign2
new file mode 100644 (file)
index 0000000..763bfdd
--- /dev/null
@@ -0,0 +1,13 @@
+{
+    # simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines     = 0;  # checksum: #lines
+    my $bytes     = 0;  # checksum: #bytes
+    my $sum       = 0;  # checksum: system V sum
+    my $patchdata = 0;  # saw patch data
+    my $pos       = 0;  # start of patch data
+      # a hanging side comment
+    my $endkit = 0;  # saw end of kit
+    my $fail = 0;  # failed
+}
+
diff --git a/t/snippets/expect/novalign.novalign3 b/t/snippets/expect/novalign.novalign3
new file mode 100644 (file)
index 0000000..fbcbe34
--- /dev/null
@@ -0,0 +1,13 @@
+{
+# simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines = 0;        # checksum: #lines
+    my $bytes = 0;        # checksum: #bytes
+    my $sum = 0;          # checksum: system V sum
+    my $patchdata = 0;    # saw patch data
+    my $pos = 0;          # start of patch data
+                          # a hanging side comment
+    my $endkit = 0;       # saw end of kit
+    my $fail = 0;         # failed
+}
+
index ee672bc4148e2a6f52c8344e8475e827b6803ef8..1c618b99093030e5b36195ee3e4116653c8f29ef 100644 (file)
@@ -1,9 +1,13 @@
+{
 # simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
 my $lines = 0;    # checksum: #lines
 my $bytes = 0;    # checksum: #bytes
 my $sum = 0;    # checksum: system V sum
 my $patchdata = 0;    # saw patch data
 my $pos = 0;    # start of patch data
+                                         # a hanging side comment
 my $endkit = 0;    # saw end of kit
 my $fail = 0;    # failed
+}
 
diff --git a/t/snippets/novalign.par b/t/snippets/novalign.par
deleted file mode 100644 (file)
index 230ed03..0000000
+++ /dev/null
@@ -1 +0,0 @@
--novalign
diff --git a/t/snippets/novalign1.par b/t/snippets/novalign1.par
new file mode 100644 (file)
index 0000000..230ed03
--- /dev/null
@@ -0,0 +1 @@
+-novalign
diff --git a/t/snippets/novalign2.par b/t/snippets/novalign2.par
new file mode 100644 (file)
index 0000000..59609df
--- /dev/null
@@ -0,0 +1 @@
+-nvsc -nvbc -msc=2
diff --git a/t/snippets/novalign3.par b/t/snippets/novalign3.par
new file mode 100644 (file)
index 0000000..b769159
--- /dev/null
@@ -0,0 +1 @@
+-nvc
index bffe841de199b1fdf461c40fedb4cfc2d4e08dd6..5df46196937e43da75a15bc621b2d4b60a433942 100644 (file)
 ../snippets24.t        git51.def
 ../snippets24.t        git51.git51
 ../snippets24.t        pretok.def
+../snippets25.t        novalign.def
+../snippets25.t        novalign.novalign
 ../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
-../snippets25.t        novalign.def
-../snippets25.t        novalign.novalign
+../snippets25.t        novalign.novalign1
+../snippets25.t        novalign.novalign2
+../snippets25.t        novalign.novalign3
index cd91e97575a16fdaa4896af498c7f72a8e996cf8..3bf1cb7d1c44be9cf9002b3b21207636a02b508b 100644 (file)
@@ -2,7 +2,9 @@
 
 # Contents:
 #1 novalign.def
-#2 novalign.novalign
+#2 novalign.novalign1
+#3 novalign.novalign2
+#4 novalign.novalign3
 
 # To locate test #13 you can search for its name or the string '#13'
 
@@ -20,8 +22,10 @@ BEGIN {
     # BEGIN SECTION 1: Parameter combinations #
     ###########################################
     $rparams = {
-        'def'      => "",
-        'novalign' => "-novalign",
+        'def'       => "",
+        'novalign1' => "-novalign",
+        'novalign2' => "-nvsc -nvbc -msc=2",
+        'novalign3' => "-nvc",
     };
 
     ############################
@@ -30,14 +34,18 @@ BEGIN {
     $rsources = {
 
         'novalign' => <<'----------',
+{
 # simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
 my $lines = 0;    # checksum: #lines
 my $bytes = 0;    # checksum: #bytes
 my $sum = 0;    # checksum: system V sum
 my $patchdata = 0;    # saw patch data
 my $pos = 0;    # start of patch data
+                                         # a hanging side comment
 my $endkit = 0;    # saw end of kit
 my $fail = 0;    # failed
+}
 
 ----------
     };
@@ -51,33 +59,81 @@ my $fail = 0;    # failed
             source => "novalign",
             params => "def",
             expect => <<'#1...........',
+{
 # simple vertical alignment of '=' and '#'
-my $lines     = 0;    # checksum: #lines
-my $bytes     = 0;    # checksum: #bytes
-my $sum       = 0;    # checksum: system V sum
-my $patchdata = 0;    # saw patch data
-my $pos       = 0;    # start of patch data
-my $endkit    = 0;    # saw end of kit
-my $fail      = 0;    # failed
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines     = 0;    # checksum: #lines
+    my $bytes     = 0;    # checksum: #bytes
+    my $sum       = 0;    # checksum: system V sum
+    my $patchdata = 0;    # saw patch data
+    my $pos       = 0;    # start of patch data
+                          # a hanging side comment
+    my $endkit    = 0;    # saw end of kit
+    my $fail      = 0;    # failed
+}
 
 #1...........
         },
 
-        'novalign.novalign' => {
+        'novalign.novalign1' => {
             source => "novalign",
-            params => "novalign",
+            params => "novalign1",
             expect => <<'#2...........',
-# simple vertical alignment of '=' and '#'
-my $lines = 0;    # checksum: #lines
-my $bytes = 0;    # checksum: #bytes
-my $sum = 0;    # checksum: system V sum
-my $patchdata = 0;    # saw patch data
-my $pos = 0;    # start of patch data
-my $endkit = 0;    # saw end of kit
-my $fail = 0;    # failed
+{
+    # simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines = 0;    # checksum: #lines
+    my $bytes = 0;    # checksum: #bytes
+    my $sum = 0;    # checksum: system V sum
+    my $patchdata = 0;    # saw patch data
+    my $pos = 0;    # start of patch data
+                    # a hanging side comment
+    my $endkit = 0;    # saw end of kit
+    my $fail = 0;    # failed
+}
 
 #2...........
         },
+
+        'novalign.novalign2' => {
+            source => "novalign",
+            params => "novalign2",
+            expect => <<'#3...........',
+{
+    # simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines     = 0;  # checksum: #lines
+    my $bytes     = 0;  # checksum: #bytes
+    my $sum       = 0;  # checksum: system V sum
+    my $patchdata = 0;  # saw patch data
+    my $pos       = 0;  # start of patch data
+      # a hanging side comment
+    my $endkit = 0;  # saw end of kit
+    my $fail = 0;  # failed
+}
+
+#3...........
+        },
+
+        'novalign.novalign3' => {
+            source => "novalign",
+            params => "novalign3",
+            expect => <<'#4...........',
+{
+# simple vertical alignment of '=' and '#'
+# A long line to test -nvbc ... normally this will cause the previous line to move left
+    my $lines = 0;        # checksum: #lines
+    my $bytes = 0;        # checksum: #bytes
+    my $sum = 0;          # checksum: system V sum
+    my $patchdata = 0;    # saw patch data
+    my $pos = 0;          # start of patch data
+                          # a hanging side comment
+    my $endkit = 0;       # saw end of kit
+    my $fail = 0;         # failed
+}
+
+#4...........
+        },
     };
 
     my $ntests = 0 + keys %{$rtests};