]> git.donarmstrong.com Git - perltidy.git/commitdiff
added option --logical-padding or -lop, see git#29
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 5 Jun 2020 02:06:41 +0000 (19:06 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 5 Jun 2020 02:06:41 +0000 (19:06 -0700)
14 files changed:
CHANGES.md
MANIFEST
bin/perltidy
docs/ChangeLog.html
docs/perltidy.html
lib/Perl/Tidy.pm
lib/Perl/Tidy/Formatter.pm
t/snippets/expect/lop.def [new file with mode: 0644]
t/snippets/expect/lop.lop [new file with mode: 0644]
t/snippets/lop.in [new file with mode: 0644]
t/snippets/lop.par [new file with mode: 0644]
t/snippets/packing_list.txt
t/snippets20.t
t/snippets21.t [new file with mode: 0644]

index ea121a66cdfef146e3607d396c2c1774f95c087c..c9381b720c3e4abb253e9f71cc390b0a357d3d26 100644 (file)
@@ -2,6 +2,10 @@
 
 ## 2020 01 10.01
 
+    - Added a parameter --logical-padding or -lop to allow logical padding
+      to be turned off.  Requested by git #29. This flag is on by default.
+      The man pages have examples.
+
     - Added a parameter -kpit=n to control spaces inside of parens following
       certain keywords, requested in git#26. This flag is off by default.
 
       separately.
 
     - Added --character-encoding=guess or -guess to have perltidy guess
-      if a file is encoded as -utf8 or some older single-byte encoding. This
-      is useful when processing a mixture of file types, such as utf8 and 
-      latin-1.  Also, specific encodings of input files other than utf8 may
-      now be given, for example --character-encoding=euc-jp.  For a
-      description of the guessing method see the man pages.
+      if a file (or other input stream) is encoded as -utf8 or some 
+      older single-byte encoding. This is useful when processing a mixture 
+      of file types, such as utf8 and latin-1.  
 
       Please Note: The default encoding has been set to be 'guess'
       instead of 'none'. I do not like to change defaults, but this seems like
-      the right choice, since it should make perltidy work properly with both
-      older latin-1 and newer utf8 files.  I have done extensive testing and
-      so far haven't found any problems.
+      the best choice, since it should make perltidy work properly with both
+      utf8 files and older latin-1 files.  The guess mode uses Encode::Guess,
+      which is included in standard perl distributions, and only tries to 
+      guess if a file is utf8 or not, never any other encoding.  If the guess is 
+      utf8, and if the file successfully decodes as utf8, then it the encoding 
+      is assumed to be utf8.  Otherwise, no encoding is assumed.
+      I have done extensive testing and have not detected any problems with
+      this guess method. If you do not want to use this new default guess mode,
+      or have a problem with it, you can set --character-encoding=none 
+      (the previous default) or --character-encoding=utf8 (if you deal
+      with utf8 files).
+
+    - Specific encodings of input files other than utf8 may now be given, for
+      example --character-encoding=euc-jp.  
 
     - Fix for git#22, Preserve function signature on a single line. An
       unwanted line break was being introduced when a closing signature paren
index cc8c9c51a138be8aa11eb95d34f5054234ca1f5d..015ced2d7eca83c32ebeb2da1f75bbe7f048f43b 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -1,4 +1,5 @@
 .pre-commit-hooks.yaml
+bbs.t
 bin/perltidy
 BUGS.md
 CHANGES.md
@@ -22,6 +23,7 @@ examples/lextest
 examples/perlcomment.pl
 examples/perllinetype.pl
 examples/perlmask.pl
+examples/perltidy.DEBUG
 examples/perltidy_okw.pl
 examples/perltidyrc_dump.pl
 examples/perlxmltok.pl
@@ -53,7 +55,8 @@ Makefile.PL
 MANIFEST                       This list of files
 pm2pl
 README.md
-t/filter_example.t.SKIP
+t/atee.t
+t/filter_example.t
 t/snippets1.t
 t/snippets10.t
 t/snippets11.t
@@ -63,7 +66,11 @@ t/snippets14.t
 t/snippets15.t
 t/snippets16.t
 t/snippets17.t
+t/snippets18.t
+t/snippets19.t
 t/snippets2.t
+t/snippets20.t
+t/snippets21.t
 t/snippets3.t
 t/snippets4.t
 t/snippets5.t
@@ -77,3 +84,4 @@ t/testsa.t
 t/testss.t
 t/testwide.pl.src
 t/testwide.t
+t/testwide.t.tdy
index 2a9577c96fbb900a68337acb6c7aa849c962f40d..e3160f5a53b0ceec714a2c34149dd7c82a762874 100755 (executable)
@@ -1210,6 +1210,56 @@ For example, the commands C<-kpitl="if else while" -kpit=2> will cause the just
 the spaces inside parens following  'if', 'else', and 'while' keywords to
 follow the tightness value indicated by the B<-kpit=2> flag.
 
+=item B<-lop>  or B<--logical-padding>
+
+In the following example some extra space has been inserted on the second
+line between the two open parens. This extra space is called "logical padding"
+and is intended to help align similar things vertically in some logical
+or ternary expressions.  
+
+    # perltidy [default formatting] 
+    $same =
+      (      ( $aP eq $bP )
+          && ( $aS eq $bS )
+          && ( $aT eq $bT )
+          && ( $a->{'title'} eq $b->{'title'} )
+          && ( $a->{'href'} eq $b->{'href'} ) );
+
+Note that this is considered to be a different operation from "vertical
+alignment" because space at just one line is being adjusted, whereas in
+"vertical alignment" the spaces at all lines are being adjusted. So it sort of
+a local version of vertical alignment.
+
+Here is an example involving a ternary operator:
+
+    # perltidy [default formatting] 
+    $bits =
+        $top > 0xffff ? 32
+      : $top > 0xff   ? 16
+      : $top > 1      ? 8
+      :                 1;
+
+This behavior is controlled with the flag B<--logical-padding>, which is set
+'on' by default.  If it is not desired it can be turned off using
+B<--nological-padding> or B<-nlop>.  The above two examples become, with
+B<-nlop>:
+
+    # perltidy -nlop
+    $same =
+      ( ( $aP eq $bP )
+          && ( $aS eq $bS )
+          && ( $aT eq $bT )
+          && ( $a->{'title'} eq $b->{'title'} )
+          && ( $a->{'href'} eq $b->{'href'} ) );
+
+    # perltidy -nlop
+    $bits =
+      $top > 0xffff ? 32
+      : $top > 0xff ? 16
+      : $top > 1    ? 8
+      :               1;
+
+
 =item Trimming whitespace around C<qw> quotes
 
 B<-tqw> or B<--trim-qw> provide the default behavior of trimming
@@ -1220,16 +1270,16 @@ multi-line C<qw> quotes to be left unchanged.  This option will not
 normally be necessary, but was added for testing purposes, because in
 some versions of perl, trimming C<qw> quotes changes the syntax tree.
 
-=item B<-sbq=n>  or B<--space-backslash-quote=n>
+=item b<-sbq=n>  or b<--space-backslash-quote=n>
 
-Lines like
+lines like
 
        $str1=\"string1";
        $str2=\'string2';
 
 can confuse syntax highlighters unless a space is included between the backslash and the single or double quotation mark.
 
-This can be controlled with the value of B<n> as follows:
+this can be controlled with the value of b<n> as follows:
 
     -sbq=0 means no space between the backslash and quote
     -sbq=1 means follow the example of the source code
index 094d007167bde0a162bf44799965e96b381c4382..a789e486900a7fc3746401cb6d90b698b1bf0422 100644 (file)
@@ -2,7 +2,14 @@
 
 <h2>2020 01 10.01</h2>
 
-<pre><code>- Added fix for git#25, improve vertical alignment for long lists with
+<pre><code>- Added a parameter --logical-padding or -lop to allow logical padding
+  to be turned off.  Requested by git #29. This flag is on by default.
+  The man pages have examples.
+
+- Added a parameter -kpit=n to control spaces inside of parens following
+  certain keywords, requested in git#26. This flag is off by default.
+
+- Added fix for git#25, improve vertical alignment for long lists with
   varying numbers of items per line.
 
 - calls to the module Perl::Tidy can now capture any output produced
   separately.
 
 - Added --character-encoding=guess or -guess to have perltidy guess
-  if a file is encoded as -utf8 or some older single-byte encoding. This
-  is useful when processing a mixture of file types, such as utf8 and 
-  latin-1.  Also, specific encodings of input files other than utf8 may
-  now be given, for example --character-encoding=euc-jp.  For a
-  description of the guessing method see the man pages.
+  if a file (or other input stream) is encoded as -utf8 or some 
+  older single-byte encoding. This is useful when processing a mixture 
+  of file types, such as utf8 and latin-1.  
 
   Please Note: The default encoding has been set to be 'guess'
   instead of 'none'. I do not like to change defaults, but this seems like
-  the right choice, since it should make perltidy work properly with both
-  older latin-1 and newer utf8 files.  I have done extensive testing and
-  so far haven't found any problems.
+  the best choice, since it should make perltidy work properly with both
+  utf8 files and older latin-1 files.  The guess mode uses Encode::Guess,
+  which is included in standard perl distributions, and only tries to 
+  guess if a file is utf8 or not, never any other encoding.  If the guess is 
+  utf8, and if the file successfully decodes as utf8, then it the encoding 
+  is assumed to be utf8.  Otherwise, no encoding is assumed.
+  I have done extensive testing and have not detected any problems with
+  this guess method. If you do not want to use this new default guess mode,
+  or have a problem with it, you can set --character-encoding=none 
+  (the previous default) or --character-encoding=utf8 (if you deal
+  with utf8 files).
+
+- Specific encodings of input files other than utf8 may now be given, for
+  example --character-encoding=euc-jp.  
 
 - Fix for git#22, Preserve function signature on a single line. An
   unwanted line break was being introduced when a closing signature paren
index bfb16504fe61c3875f27a315a491b15e29f8a09d..7cb5d930ae4116a725c3f262571c32836a182bb5 100644 (file)
 
 <p>For example, the commands <code>-kpitl=&quot;if else while&quot; -kpit=2</code> will cause the just the spaces inside parens following &#39;if&#39;, &#39;else&#39;, and &#39;while&#39; keywords to follow the tightness value indicated by the <b>-kpit=2</b> flag.</p>
 
+</dd>
+<dt id="lop-or---logical-padding"><b>-lop</b> or <b>--logical-padding</b></dt>
+<dd>
+
+<p>In the following example some extra space has been inserted on the second line between the two open parens. This extra space is called &quot;logical padding&quot; and is intended to help align similar things vertically in some logical or ternary expressions.</p>
+
+<pre><code>    # perltidy [default formatting] 
+    $same =
+      (      ( $aP eq $bP )
+          &amp;&amp; ( $aS eq $bS )
+          &amp;&amp; ( $aT eq $bT )
+          &amp;&amp; ( $a-&gt;{&#39;title&#39;} eq $b-&gt;{&#39;title&#39;} )
+          &amp;&amp; ( $a-&gt;{&#39;href&#39;} eq $b-&gt;{&#39;href&#39;} ) );</code></pre>
+
+<p>Note that this is considered to be a different operation from &quot;vertical alignment&quot; because space at just one line is being adjusted, whereas in &quot;vertical alignment&quot; the spaces at all lines are being adjusted. So it sort of a local version of vertical alignment.</p>
+
+<p>Here is an example involving a ternary operator:</p>
+
+<pre><code>    # perltidy [default formatting] 
+    $bits =
+        $top &gt; 0xffff ? 32
+      : $top &gt; 0xff   ? 16
+      : $top &gt; 1      ? 8
+      :                 1;</code></pre>
+
+<p>This behavior is controlled with the flag <b>--logical-padding</b>, which is set &#39;on&#39; by default. If it is not desired it can be turned off using <b>--nological-padding</b> or <b>-nlop</b>. The above two examples become, with <b>-nlop</b>:</p>
+
+<pre><code>    # perltidy -nlop
+    $same =
+      ( ( $aP eq $bP )
+          &amp;&amp; ( $aS eq $bS )
+          &amp;&amp; ( $aT eq $bT )
+          &amp;&amp; ( $a-&gt;{&#39;title&#39;} eq $b-&gt;{&#39;title&#39;} )
+          &amp;&amp; ( $a-&gt;{&#39;href&#39;} eq $b-&gt;{&#39;href&#39;} ) );
+
+    # perltidy -nlop
+    $bits =
+      $top &gt; 0xffff ? 32
+      : $top &gt; 0xff ? 16
+      : $top &gt; 1    ? 8
+      :               1;</code></pre>
+
 </dd>
 <dt id="Trimming-whitespace-around-qw-quotes">Trimming whitespace around <code>qw</code> quotes</dt>
 <dd>
 <p><b>-ntqw</b> or <b>--notrim-qw</b> cause leading and trailing whitespace around multi-line <code>qw</code> quotes to be left unchanged. This option will not normally be necessary, but was added for testing purposes, because in some versions of perl, trimming <code>qw</code> quotes changes the syntax tree.</p>
 
 </dd>
-<dt id="sbq-n-or---space-backslash-quote-n"><b>-sbq=n</b> or <b>--space-backslash-quote=n</b></dt>
+<dt id="b-sbq-n-or-b--space-backslash-quote-n">b&lt;-sbq=n&gt; or b&lt;--space-backslash-quote=n&gt;</dt>
 <dd>
 
-<p>Lines like</p>
+<p>lines like</p>
 
 <pre><code>       $str1=\&quot;string1&quot;;
        $str2=\&#39;string2&#39;;</code></pre>
 
 <p>can confuse syntax highlighters unless a space is included between the backslash and the single or double quotation mark.</p>
 
-<p>This can be controlled with the value of <b>n</b> as follows:</p>
+<p>this can be controlled with the value of b&lt;n&gt; as follows:</p>
 
 <pre><code>    -sbq=0 means no space between the backslash and quote
     -sbq=1 means follow the example of the source code
index 8f55ea033ad0436c145d7ea25ebd9fca3f2833a0..32929006b727f05714c76e537cb43897720ee462 100644 (file)
@@ -1988,6 +1988,7 @@ sub generate_options {
     $add_option->( 'delete-semicolons',                         'dsm',   '!' );
     $add_option->( 'keyword-paren-inner-tightness',             'kpit',  '=i' );
     $add_option->( 'keyword-paren-inner-tightness-list',        'kpitl', '=s' );
+    $add_option->( 'logical-padding',                           'lop',   '!' );
     $add_option->( 'nospace-after-keyword',                     'nsak',  '=s' );
     $add_option->( 'nowant-left-space',                         'nwls',  '=s' );
     $add_option->( 'nowant-right-space',                        'nwrs',  '=s' );
@@ -2283,6 +2284,7 @@ sub generate_options {
       iterations=1
       keep-old-blank-lines=1
       keyword-paren-inner-tightness=1
+      logical-padding
       long-block-line-count=8
       look-for-autoloader
       look-for-selfloader
index eb5d1359f55cae2d252ab0b986821827a5254074..e723bc93269bcd87fad57dfd6e04cd912b1471df 100644 (file)
@@ -10362,7 +10362,8 @@ sub send_lines_to_vertical_aligner {
 
     $self->undo_ci( $ri_first, $ri_last );
 
-    $self->set_logical_padding( $ri_first, $ri_last );
+    $self->set_logical_padding( $ri_first, $ri_last )
+      if ( $rOpts->{'logical-padding'} );
 
     # loop to prepare each line for shipment
     my $in_comma_list;
diff --git a/t/snippets/expect/lop.def b/t/snippets/expect/lop.def
new file mode 100644 (file)
index 0000000..e877748
--- /dev/null
@@ -0,0 +1,17 @@
+# logical padding examples
+$same =
+  (      ( $aP eq $bP )
+      && ( $aS eq $bS )
+      && ( $aT eq $bT )
+      && ( $a->{'title'} eq $b->{'title'} )
+      && ( $a->{'href'} eq $b->{'href'} ) );
+
+$bits =
+    $top > 0xffff ? 32
+  : $top > 0xff   ? 16
+  : $top > 1      ? 8
+  :                 1;
+
+lc(      $self->mime_attr('content-type')
+      || $self->{MIH_DefaultType}
+      || 'text/plain' );
diff --git a/t/snippets/expect/lop.lop b/t/snippets/expect/lop.lop
new file mode 100644 (file)
index 0000000..e9ce0a5
--- /dev/null
@@ -0,0 +1,17 @@
+# logical padding examples
+$same =
+  ( ( $aP eq $bP )
+      && ( $aS eq $bS )
+      && ( $aT eq $bT )
+      && ( $a->{'title'} eq $b->{'title'} )
+      && ( $a->{'href'} eq $b->{'href'} ) );
+
+$bits =
+  $top > 0xffff ? 32
+  : $top > 0xff ? 16
+  : $top > 1    ? 8
+  :               1;
+
+lc( $self->mime_attr('content-type')
+      || $self->{MIH_DefaultType}
+      || 'text/plain' );
diff --git a/t/snippets/lop.in b/t/snippets/lop.in
new file mode 100644 (file)
index 0000000..283b1b9
--- /dev/null
@@ -0,0 +1,17 @@
+# logical padding examples
+$same =
+  (      ( $aP eq $bP )
+      && ( $aS eq $bS )
+      && ( $aT eq $bT )
+      && ( $a->{'title'} eq $b->{'title'} )
+      && ( $a->{'href'} eq $b->{'href'} ) );
+
+$bits =
+    $top > 0xffff ? 32
+  : $top > 0xff   ? 16
+  : $top > 1      ? 8
+  :                 1;
+
+lc( $self->mime_attr('content-type')
+        || $self->{MIH_DefaultType}
+        || 'text/plain' );
diff --git a/t/snippets/lop.par b/t/snippets/lop.par
new file mode 100644 (file)
index 0000000..42e74ad
--- /dev/null
@@ -0,0 +1 @@
+-nlop
index f68982d6f2495dcd455a7ade619e5a2d2fdfbf92..41c88a8d7b0606c6daec20f431052efaf9e7cb4a 100644 (file)
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
+../snippets20.t        lop.def
+../snippets21.t        lop.lop
index 7d55f2a8b9761ac6041653aa127f9b9fd908e7fb..d911e714120df47740ff139220f78dac317a9637 100644 (file)
@@ -19,6 +19,7 @@
 #16 kpitl.def
 #17 kpitl.kpitl
 #18 hanging_side_comments3.def
+#19 lop.def
 
 # To locate test #13 you can search for its name or the string '#13'
 
@@ -154,6 +155,26 @@ return ( $r**$n ) * ( pi**( $n / 2 ) ) / ( sqrt(pi) * factorial( 2 * ( int( $n
 );
 ----------
 
+        'lop' => <<'----------',
+# logical padding examples
+$same =
+  (      ( $aP eq $bP )
+      && ( $aS eq $bS )
+      && ( $aT eq $bT )
+      && ( $a->{'title'} eq $b->{'title'} )
+      && ( $a->{'href'} eq $b->{'href'} ) );
+
+$bits =
+    $top > 0xffff ? 32
+  : $top > 0xff   ? 16
+  : $top > 1      ? 8
+  :                 1;
+
+lc( $self->mime_attr('content-type')
+        || $self->{MIH_DefaultType}
+        || 'text/plain' );
+----------
+
         'outdent' => <<'----------',
         my $i;
       LOOP: while ( $i = <FOTOS> ) {
@@ -600,6 +621,30 @@ return ( $r**$n ) *
     }
 #18...........
         },
+
+        'lop.def' => {
+            source => "lop",
+            params => "def",
+            expect => <<'#19...........',
+# logical padding examples
+$same =
+  (      ( $aP eq $bP )
+      && ( $aS eq $bS )
+      && ( $aT eq $bT )
+      && ( $a->{'title'} eq $b->{'title'} )
+      && ( $a->{'href'} eq $b->{'href'} ) );
+
+$bits =
+    $top > 0xffff ? 32
+  : $top > 0xff   ? 16
+  : $top > 1      ? 8
+  :                 1;
+
+lc(      $self->mime_attr('content-type')
+      || $self->{MIH_DefaultType}
+      || 'text/plain' );
+#19...........
+        },
     };
 
     my $ntests = 0 + keys %{$rtests};
diff --git a/t/snippets21.t b/t/snippets21.t
new file mode 100644 (file)
index 0000000..6edfb8d
--- /dev/null
@@ -0,0 +1,137 @@
+# Created with: ./make_t.pl
+
+# Contents:
+#1 lop.lop
+
+# To locate test #13 you can search for its name or the string '#13'
+
+use strict;
+use Test::More;
+use Carp;
+use Perl::Tidy;
+my $rparams;
+my $rsources;
+my $rtests;
+
+BEGIN {
+
+    ###########################################
+    # BEGIN SECTION 1: Parameter combinations #
+    ###########################################
+    $rparams = { 'lop' => "-nlop", };
+
+    ############################
+    # BEGIN SECTION 2: Sources #
+    ############################
+    $rsources = {
+
+        'lop' => <<'----------',
+# logical padding examples
+$same =
+  (      ( $aP eq $bP )
+      && ( $aS eq $bS )
+      && ( $aT eq $bT )
+      && ( $a->{'title'} eq $b->{'title'} )
+      && ( $a->{'href'} eq $b->{'href'} ) );
+
+$bits =
+    $top > 0xffff ? 32
+  : $top > 0xff   ? 16
+  : $top > 1      ? 8
+  :                 1;
+
+lc( $self->mime_attr('content-type')
+        || $self->{MIH_DefaultType}
+        || 'text/plain' );
+----------
+    };
+
+    ####################################
+    # BEGIN SECTION 3: Expected output #
+    ####################################
+    $rtests = {
+
+        'lop.lop' => {
+            source => "lop",
+            params => "lop",
+            expect => <<'#1...........',
+# logical padding examples
+$same =
+  ( ( $aP eq $bP )
+      && ( $aS eq $bS )
+      && ( $aT eq $bT )
+      && ( $a->{'title'} eq $b->{'title'} )
+      && ( $a->{'href'} eq $b->{'href'} ) );
+
+$bits =
+  $top > 0xffff ? 32
+  : $top > 0xff ? 16
+  : $top > 1    ? 8
+  :               1;
+
+lc( $self->mime_attr('content-type')
+      || $self->{MIH_DefaultType}
+      || 'text/plain' );
+#1...........
+        },
+    };
+
+    my $ntests = 0 + keys %{$rtests};
+    plan tests => $ntests;
+}
+
+###############
+# EXECUTE TESTS
+###############
+
+foreach my $key ( sort keys %{$rtests} ) {
+    my $output;
+    my $sname  = $rtests->{$key}->{source};
+    my $expect = $rtests->{$key}->{expect};
+    my $pname  = $rtests->{$key}->{params};
+    my $source = $rsources->{$sname};
+    my $params = defined($pname) ? $rparams->{$pname} : "";
+    my $stderr_string;
+    my $errorfile_string;
+    my $err = Perl::Tidy::perltidy(
+        source      => \$source,
+        destination => \$output,
+        perltidyrc  => \$params,
+        argv        => '',             # for safety; hide any ARGV from perltidy
+        stderr      => \$stderr_string,
+        errorfile => \$errorfile_string,    # not used when -se flag is set
+    );
+    if ( $err || $stderr_string || $errorfile_string ) {
+        print STDERR "Error output received for test '$key'\n";
+        if ($err) {
+            print STDERR "An error flag '$err' was returned\n";
+            ok( !$err );
+        }
+        if ($stderr_string) {
+            print STDERR "---------------------\n";
+            print STDERR "<<STDERR>>\n$stderr_string\n";
+            print STDERR "---------------------\n";
+            ok( !$stderr_string );
+        }
+        if ($errorfile_string) {
+            print STDERR "---------------------\n";
+            print STDERR "<<.ERR file>>\n$errorfile_string\n";
+            print STDERR "---------------------\n";
+            ok( !$errorfile_string );
+        }
+    }
+    else {
+        if ( !is( $output, $expect, $key ) ) {
+            my $leno = length($output);
+            my $lene = length($expect);
+            if ( $leno == $lene ) {
+                print STDERR
+"#> Test '$key' gave unexpected output.  Strings differ but both have length $leno\n";
+            }
+            else {
+                print STDERR
+"#> Test '$key' gave unexpected output.  String lengths differ: output=$leno, expected=$lene\n";
+            }
+        }
+    }
+}