From: Steve Hancock Date: Wed, 23 Jan 2019 01:15:30 +0000 (-0800) Subject: added flag -olbs=n for semicolons in one-line blocks X-Git-Tag: 20190601~16 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=3120ac7fb2d372fced441d44e815d90acf1ffd87;p=perltidy.git added flag -olbs=n for semicolons in one-line blocks --- diff --git a/CHANGES.md b/CHANGES.md index 10e35228..a1b68cb9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,13 @@ ## 2018 11 20.01 + - RT #128280, added flag --one-line-block-semicolons=n (-olbs=n) + to control semicolons in one-line blocks. The values of n are: + n=0 means no semicolons termininating simple one-line blocks + n=1 means stable; do not change from input file [DEFAULT and current] + n=2 means always add semicolons in one-line blocks + The current behavior corresponds to the default n=1. + - RT #128216, very minor update to prevent inserting unwanted blank line at indentation level change. This should not change existing scripts. diff --git a/docs/ChangeLog.html b/docs/ChangeLog.html index 052c34d5..3a7b04ff 100644 --- a/docs/ChangeLog.html +++ b/docs/ChangeLog.html @@ -2,7 +2,14 @@

2018 11 20.01

-
- RT #128216, very minor update to prevent inserting unwanted blank line at
+
- RT #128280, added flag --one-line-block-semicolons=n (-olbs=n) 
+  to control semicolons in one-line blocks.  The values of n are:
+    n=0 means no semicolons termininating simple one-line blocks
+    n=1 means stable; do not change from input file [DEFAULT and current]
+    n=2 means always add semicolons in one-line blocks
+  The current behavior corresponds to the default n=1.
+
+- RT #128216, very minor update to prevent inserting unwanted blank line at
   indentation level change.  This should not change existing scripts.
 
 - RT #81852: Improved indentation when quoted word (qw) lists are 
diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm
index a8a066cd..543754eb 100644
--- a/lib/Perl/Tidy.pm
+++ b/lib/Perl/Tidy.pm
@@ -1767,6 +1767,7 @@ sub generate_options {
     $add_option->( 'break-after-all-operators',               'baao',  '!' );
     $add_option->( 'break-before-all-operators',              'bbao',  '!' );
     $add_option->( 'keep-interior-semicolons',                'kis',   '!' );
+    $add_option->( 'one-line-block-semicolons',               'olbs',  '=i' );
 
     ########################################
     $category = 6;    # Controlling list formatting
@@ -1990,6 +1991,7 @@ sub generate_options {
       notabs
       nowarning-output
       character-encoding=none
+      one-line-block-semicolons=1
       outdent-labels
       outdent-long-quotes
       outdent-long-comments
diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm
index 54a3dc20..0bc780fd 100644
--- a/lib/Perl/Tidy/Formatter.pm
+++ b/lib/Perl/Tidy/Formatter.pm
@@ -277,6 +277,7 @@ use vars qw{
   $rOpts_stack_closing_block_brace
   $rOpts_space_backslash_quote
   $rOpts_whitespace_cycle
+  $rOpts_one_line_block_semicolons
 
   %is_opening_type
   %is_closing_type
@@ -2522,12 +2523,10 @@ sub respace_tokens {
             # on a token which has been stored.
             my $rcopy = copy_token_as_type( $rLL_new->[$Ktop], 'b', ' ' );
 
-            # TESTING: works, but needs flag implemented
             # Convert the existing blank to:
             #   a phantom semicolon for one_line_block option = 0 or 1
             #   a real semicolon    for one_line_block option = 2
-	    my $Opt_semicolons_in_one_line_blocks = 1;
-            my $tok = $Opt_semicolons_in_one_line_blocks == 2 ? ';' : '';
+            my $tok = $rOpts_one_line_block_semicolons == 2 ? ';' : '';
 
             $rLL_new->[$Ktop]->[_TOKEN_] = $tok;   # zero length if phantom
             $rLL_new->[$Ktop]->[_TYPE_]  = ';';
@@ -5691,14 +5690,15 @@ EOM
       $rOpts->{'closing-side-comment-else-flag'};
     $rOpts_closing_side_comment_maximum_text =
       $rOpts->{'closing-side-comment-maximum-text'};
-    $rOpts_continuation_indentation = $rOpts->{'continuation-indentation'};
-    $rOpts_delete_old_whitespace    = $rOpts->{'delete-old-whitespace'};
-    $rOpts_fuzzy_line_length        = $rOpts->{'fuzzy-line-length'};
-    $rOpts_indent_columns           = $rOpts->{'indent-columns'};
-    $rOpts_line_up_parentheses      = $rOpts->{'line-up-parentheses'};
-    $rOpts_maximum_fields_per_table = $rOpts->{'maximum-fields-per-table'};
-    $rOpts_maximum_line_length      = $rOpts->{'maximum-line-length'};
-    $rOpts_whitespace_cycle         = $rOpts->{'whitespace-cycle'};
+    $rOpts_continuation_indentation  = $rOpts->{'continuation-indentation'};
+    $rOpts_delete_old_whitespace     = $rOpts->{'delete-old-whitespace'};
+    $rOpts_fuzzy_line_length         = $rOpts->{'fuzzy-line-length'};
+    $rOpts_indent_columns            = $rOpts->{'indent-columns'};
+    $rOpts_line_up_parentheses       = $rOpts->{'line-up-parentheses'};
+    $rOpts_maximum_fields_per_table  = $rOpts->{'maximum-fields-per-table'};
+    $rOpts_maximum_line_length       = $rOpts->{'maximum-line-length'};
+    $rOpts_whitespace_cycle          = $rOpts->{'whitespace-cycle'};
+    $rOpts_one_line_block_semicolons = $rOpts->{'one-line-block-semicolons'};
 
     $rOpts_variable_maximum_line_length =
       $rOpts->{'variable-maximum-line-length'};
@@ -7822,6 +7822,9 @@ sub output_line_to_go {
             $do_not_pad = correct_lp_indentation( $ri_first, $ri_last );
         }
         $self->unmask_phantom_semicolons( $ri_first, $ri_last );
+        if ( $rOpts_one_line_block_semicolons == 0 ) {
+            $self->delete_one_line_semicolons( $ri_first, $ri_last );
+        }
         $self->send_lines_to_vertical_aligner( $ri_first, $ri_last,
             $do_not_pad );
 
@@ -14832,6 +14835,66 @@ sub undo_forced_breakpoint_stack {
         return;
     }
 
+    sub delete_one_line_semicolons {
+
+        my ( $self, $ri_beg, $ri_end ) = @_;
+        my $rLL                 = $self->{rLL};
+        my $K_opening_container = $self->{K_opening_container};
+
+	# Walk down the lines of this batch and delete any semicolons
+	# terminating one-line blocks;
+        my $nmax = @{$ri_end} - 1;
+
+        foreach my $n ( 0 .. $nmax ) {
+            my $i_beg    = $ri_beg->[$n];
+            my $i_e      = $ri_end->[$n];
+            my $K_beg    = $K_to_go[$i_beg];
+            my $K_e      = $K_to_go[$i_e];
+            my $K_end    = $K_e;
+            my $type_end = $rLL->[$K_end]->[_TYPE_];
+            if ( $type_end eq '#' ) {
+                $K_end = $self->K_previous_nonblank($K_end);
+                if ( defined($K_end) ) { $type_end = $rLL->[$K_end]->[_TYPE_]; }
+            }
+
+	    # we are looking for a line ending in closing brace
+            next
+              unless ( $type_end eq '}' && $rLL->[$K_end]->[_TOKEN_] eq '}' );
+
+	    # ...and preceded by a semicolon on the same line
+            my $K_semicolon = $self->K_previous_nonblank($K_end);
+            my $i_semicolon = $i_beg + ( $K_semicolon - $K_beg );
+            next if ( $i_semicolon <= $i_beg );
+            next unless ( $rLL->[$K_semicolon]->[_TYPE_] eq ';' );
+
+	    # safety check - shouldn't happen
+	    if ($types_to_go[$i_semicolon] ne ';') {
+		print STDERR "unexpected type looking for semicolon, ignoring\n";
+		next;
+	    }
+
+	    # ... with the corresponding opening brace on the same line
+            my $type_sequence = $rLL->[$K_end]->[_TYPE_SEQUENCE_];
+            my $K_opening     = $K_opening_container->{$type_sequence};
+            my $i_opening     = $i_beg + ( $K_opening - $K_beg );
+            next if ( $i_opening < $i_beg );
+
+	    # ... and only one semicolon between these braces
+	    my $semicolon_count=0;
+            foreach my $K ( $K_opening + 1 .. $K_semicolon-1 ) {
+		if ($rLL->[$K]->[_TYPE_] eq ';') {
+			$semicolon_count++;
+			last;
+		}
+            }
+	    next if ($semicolon_count);
+
+	    # ...ok, then make the semicolon invisible
+            $tokens_to_go[$i_semicolon] = "";
+        }
+        return;
+    }
+
     sub unmask_phantom_semicolons {
 
         my ( $self, $ri_beg, $ri_end ) = @_;