]> git.donarmstrong.com Git - perltidy.git/commitdiff
add --cuddeled-paren-brace, suggested in git #110
authorSteve Hancock <perltidy@users.sourceforge.net>
Sun, 18 Dec 2022 05:31:01 +0000 (21:31 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sun, 18 Dec 2022 05:31:01 +0000 (21:31 -0800)
lib/Perl/Tidy.pm
lib/Perl/Tidy/Formatter.pm
t/snippets/cpb.in [new file with mode: 0644]
t/snippets/cpb.par [new file with mode: 0644]

index 65eb4b743aaeccdc37c6728b7d2570be67fa80cf..9f64bd55015dbf6691a32dd42daaa3d9d9afbbf8 100644 (file)
@@ -3262,6 +3262,7 @@ sub generate_options {
     $add_option->( 'cuddled-block-list',                      'cbl',   '=s' );
     $add_option->( 'cuddled-block-list-exclusive',            'cblx',  '!' );
     $add_option->( 'cuddled-break-option',                    'cbo',   '=i' );
+    $add_option->( 'cuddled-paren-brace',                     'cpb',   '!' );
     $add_option->( 'delete-old-newlines',                     'dnl',   '!' );
     $add_option->( 'opening-brace-always-on-right',           'bar',   '!' );
     $add_option->( 'opening-brace-on-new-line',               'bl',    '!' );
index f55c91f61e1d1cc629ca3554baba45521010bdbc..e34ebbfe8330c98336745ac60edd9c2c57e4a788 100644 (file)
@@ -189,6 +189,7 @@ my (
     $rOpts_closing_side_comment_maximum_text,
     $rOpts_comma_arrow_breakpoints,
     $rOpts_continuation_indentation,
+    $rOpts_cuddled_paren_brace,
     $rOpts_delete_closing_side_comments,
     $rOpts_delete_old_whitespace,
     $rOpts_delete_side_comments,
@@ -1872,6 +1873,8 @@ EOM
       $rOpts->{'closing-side-comment-maximum-text'};
     $rOpts_comma_arrow_breakpoints  = $rOpts->{'comma-arrow-breakpoints'};
     $rOpts_continuation_indentation = $rOpts->{'continuation-indentation'};
+    $rOpts_cuddled_paren_brace =
+      $rOpts->{'cuddled-paren-brace'};
     $rOpts_delete_closing_side_comments =
       $rOpts->{'delete-closing-side-comments'};
     $rOpts_delete_old_whitespace = $rOpts->{'delete-old-whitespace'};
@@ -17941,16 +17944,33 @@ EOM
 
                     DEBUG_RECOMBINE > 1 && do {
                         print STDERR
-"RECOMBINE: n=$n imid=$iend_1 if=$ibeg_1 type=$type_ibeg_1 =$tokens_to_go[$ibeg_1] next_type=$type_ibeg_2 next_tok=$tokens_to_go[$ibeg_2]\n";
+"RECOMBINE: n=$n nmax=$nmax imid=$iend_1 if=$ibeg_1 type=$type_ibeg_1 =$tokens_to_go[$ibeg_1] next_type=$type_ibeg_2 next_tok=$tokens_to_go[$ibeg_2]\n";
                     };
 
                     # If line $n is the last line, we set some flags and
                     # do any special checks for it
                     if ( $n == $nmax ) {
 
-                        # a terminal '{' should stay where it is
-                        # unless preceded by a fat comma
-                        next if ( $type_ibeg_2 eq '{' && $type_iend_1 ne '=>' );
+                        if ( $type_ibeg_2 eq '{' ) {
+
+                            # join isolated ')' and '{' if requested (git #110)
+                            if (   $rOpts_cuddled_paren_brace
+                                && $type_iend_1 eq '}'
+                                && $iend_1 == $ibeg_1
+                                && $ibeg_2 == $iend_2 )
+                            {
+                                if (   $tokens_to_go[$iend_1] eq ')'
+                                    && $tokens_to_go[$ibeg_2] eq '{' )
+                                {
+                                    $n_best = $n;
+                                    last;
+                                }
+                            }
+
+                            # otherwise, a terminal '{' should stay where it is
+                            # unless preceded by a fat comma
+                            next if ( $type_iend_1 ne '=>' );
+                        }
 
                         if (   $type_iend_2 eq '#'
                             && $iend_2 - $ibeg_2 >= 2
diff --git a/t/snippets/cpb.in b/t/snippets/cpb.in
new file mode 100644 (file)
index 0000000..ef9a855
--- /dev/null
@@ -0,0 +1,9 @@
+foreach my $dir (
+    '05_lexer', '07_token', '08_regression', '11_util',
+    '13_data',  '15_transform'
+  )
+{
+    my @perl = find_files( catdir( 't', 'data', $dir ) );
+    push @files, @perl;
+}
+
diff --git a/t/snippets/cpb.par b/t/snippets/cpb.par
new file mode 100644 (file)
index 0000000..5aa8bd7
--- /dev/null
@@ -0,0 +1 @@
+-cpb