]> git.donarmstrong.com Git - perltidy.git/commitdiff
allow Switch::Plain constructs within ternary stmts
authorSteve Hancock <perltidy@users.sourceforge.net>
Thu, 11 Jun 2020 13:44:45 +0000 (06:44 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Thu, 11 Jun 2020 13:44:45 +0000 (06:44 -0700)
lib/Perl/Tidy/Tokenizer.pm
t/snippets/expect/switch_plain.def
t/snippets/expect/switch_plain.switch_plain
t/snippets/packing_list.txt
t/snippets/switch_plain.in
t/snippets21.t

index cf7aa7406515535836685a4d9b8c780573617ba1..db8c81e56f2f5cfcceec1aa9117076f99df315b7 100644 (file)
@@ -1,4 +1,4 @@
-########################################################################
+#######################################################################
 #
 # the Perl::Tidy::Tokenizer package is essentially a filter which
 # reads lines of perl source code from a source object and provides
@@ -1550,16 +1550,13 @@ sub prepare_for_a_new_file {
         return unless ( $tokenizer_self->{'_extended_syntax'} );
 
         if ( $type eq ':' ) {
-            my ( $next_nonblank_token, $i_next ) =
-              find_next_nonblank_token( $i + 1, $rtokens, $max_token_index );
 
             # Look for Switch::Plain syntax; some examples
             #  case 1: {
             #  default: {
             #  default:
-            # This should be enough to identify this syntax.  If necessary, we
-            # could also look for and require 'use Switch::Plain', but then
-            # perltidy would fail if run on small snippets in an editor.
+            # Note that the line 'default:' will be parsed as a label elsewhere.
+
             if ( $statement_type eq 'case' || $statement_type eq 'default' ) {
 
                 # The type will be the same as a label
@@ -2187,7 +2184,7 @@ sub prepare_for_a_new_file {
             }
 
             # if an error would otherwise occur, check for extended syntax
-            elsif ( !$current_depth[QUESTION_COLON]
+            elsif ( !is_balanced_closing_container(QUESTION_COLON)
                 && $extended_syntax_type->() )
             {
 
@@ -4980,6 +4977,27 @@ sub increase_nesting_depth {
     return ( $seqno, $indent );
 }
 
+sub is_balanced_closing_container {
+
+    # Return true if a closing container can go here without error
+    # Return false if not
+    my ($aa) = @_;
+
+    # cannot close if there was no opening
+    return unless ( $current_depth[$aa] > 0 );
+
+    # check that any other brace types $bb contained within would be balanced
+    for my $bb ( 0 .. @closing_brace_names - 1 ) {
+        next if ( $bb == $aa );
+        return
+          unless ( $depth_array[$aa][$bb][ $current_depth[$aa] ] ==
+            $current_depth[$bb] );
+    }
+
+    # OK, everything will be balanced
+    return 1;
+}
+
 sub decrease_nesting_depth {
 
     my ( $aa, $pos ) = @_;
index 54bedb3f862a648cfbe7843fb76684da32d9d02e..65278a370794992664ad9a98640600022d056bb7 100644 (file)
@@ -1,4 +1,4 @@
-# must run with -nola to keep default from outdenting
+# run with -nola to keep default from outdenting
 use Switch::Plain;
 my $r = 'fail';
 my $x = int rand 100_000;
@@ -8,13 +8,17 @@ nswitch( 1 + $x * 2 ) {
         $r = 'ok';
     }
 }
-my @words = qw(speed me towards death);
-$r = do {
+
+my @words = qw(cinnamon ginger nutmeg cloves);
+my $test  = 1;
+$r = $test
+  ? do {
     sswitch( $words[ rand @words ] ) {
         case $words[0]:
         case $words[1]:
         case $words[2]:
         case $words[3]: { 'ok' }
-      default: { 'wtf' }
+      default: { 'default case' }
     }
-};
+  }
+  : 'not ok';
index 66d9cbf7ff6aba1dcfffb683eaf75254e606a580..3e9a0cd29f8726615efb1f3da3964257c5931605 100644 (file)
@@ -1,4 +1,4 @@
-# must run with -nola to keep default from outdenting
+# run with -nola to keep default from outdenting
 use Switch::Plain;
 my $r = 'fail';
 my $x = int rand 100_000;
@@ -8,13 +8,17 @@ nswitch( 1 + $x * 2 ) {
         $r = 'ok';
     }
 }
-my @words = qw(speed me towards death);
-$r = do {
+
+my @words = qw(cinnamon ginger nutmeg cloves);
+my $test  = 1;
+$r = $test
+  ? do {
     sswitch( $words[ rand @words ] ) {
         case $words[0]:
         case $words[1]:
         case $words[2]:
         case $words[3]: { 'ok' }
-        default: { 'wtf' }
+        default: { 'default case' }
     }
-};
+  }
+  : 'not ok';
index d0d040e93f062f95712e7bb0243e594f13571377..d21f2552d3b315817fb00ad8a0fb53d70bdb35d7 100644 (file)
 ../snippets20.t        hanging_side_comments3.def
 ../snippets20.t        lop.def
 ../snippets21.t        lop.lop
+../snippets21.t        switch_plain.def
+../snippets21.t        switch_plain.switch_plain
 ../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
-../snippets21.t        switch_plain.def
-../snippets21.t        switch_plain.switch_plain
index 58cae32a94f0e2e024253890f8bdbcfe13532bfc..ee14f834884ea1d5e0f47543162b21159f4c79b1 100644 (file)
@@ -1,4 +1,4 @@
-# must run with -nola to keep default from outdenting
+# run with -nola to keep default from outdenting
 use Switch::Plain;
 my $r = 'fail';
 my $x = int rand 100_000;
@@ -8,13 +8,17 @@ nswitch (1 + $x * 2) {
         $r = 'ok';
     }
 }
-my @words = qw(speed me towards death);
-$r = do {
-    sswitch ($words[rand @words]) {
+
+my @words = qw(cinnamon ginger nutmeg cloves);
+my $test = 1;
+$r = $test
+  ? do {
+    sswitch( $words[ rand @words ] ) {
         case $words[0]:
         case $words[1]:
         case $words[2]:
         case $words[3]: { 'ok' }
-        default: { 'wtf' }
+      default: { 'default case' }
     }
-};
+  }
+  : 'not ok';
index 42061dd40219fae82246c1c3cf4421fa0d52dd67..2eff5aa90aaa16fe793d4b5cbfd528f8763cf962 100644 (file)
@@ -52,7 +52,7 @@ lc( $self->mime_attr('content-type')
 ----------
 
         'switch_plain' => <<'----------',
-# must run with -nola to keep default from outdenting
+# run with -nola to keep default from outdenting
 use Switch::Plain;
 my $r = 'fail';
 my $x = int rand 100_000;
@@ -62,16 +62,20 @@ nswitch (1 + $x * 2) {
         $r = 'ok';
     }
 }
-my @words = qw(speed me towards death);
-$r = do {
-    sswitch ($words[rand @words]) {
+
+my @words = qw(cinnamon ginger nutmeg cloves);
+my $test = 1;
+$r = $test
+  ? do {
+    sswitch( $words[ rand @words ] ) {
         case $words[0]:
         case $words[1]:
         case $words[2]:
         case $words[3]: { 'ok' }
-        default: { 'wtf' }
+      default: { 'default case' }
     }
-};
+  }
+  : 'not ok';
 ----------
     };
 
@@ -108,7 +112,7 @@ lc( $self->mime_attr('content-type')
             source => "switch_plain",
             params => "def",
             expect => <<'#2...........',
-# must run with -nola to keep default from outdenting
+# run with -nola to keep default from outdenting
 use Switch::Plain;
 my $r = 'fail';
 my $x = int rand 100_000;
@@ -118,16 +122,20 @@ nswitch( 1 + $x * 2 ) {
         $r = 'ok';
     }
 }
-my @words = qw(speed me towards death);
-$r = do {
+
+my @words = qw(cinnamon ginger nutmeg cloves);
+my $test  = 1;
+$r = $test
+  ? do {
     sswitch( $words[ rand @words ] ) {
         case $words[0]:
         case $words[1]:
         case $words[2]:
         case $words[3]: { 'ok' }
-      default: { 'wtf' }
+      default: { 'default case' }
     }
-};
+  }
+  : 'not ok';
 #2...........
         },
 
@@ -135,7 +143,7 @@ $r = do {
             source => "switch_plain",
             params => "switch_plain",
             expect => <<'#3...........',
-# must run with -nola to keep default from outdenting
+# run with -nola to keep default from outdenting
 use Switch::Plain;
 my $r = 'fail';
 my $x = int rand 100_000;
@@ -145,16 +153,20 @@ nswitch( 1 + $x * 2 ) {
         $r = 'ok';
     }
 }
-my @words = qw(speed me towards death);
-$r = do {
+
+my @words = qw(cinnamon ginger nutmeg cloves);
+my $test  = 1;
+$r = $test
+  ? do {
     sswitch( $words[ rand @words ] ) {
         case $words[0]:
         case $words[1]:
         case $words[2]:
         case $words[3]: { 'ok' }
-        default: { 'wtf' }
+        default: { 'default case' }
     }
-};
+  }
+  : 'not ok';
 #3...........
         },
     };