From e230cb35e61dc6df0d81a7edb1c903dddaef43b9 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 2 Sep 2020 08:10:39 -0700 Subject: [PATCH] modify -nib to be exact match, not just leading match --- bin/perltidy | 6 +++--- lib/Perl/Tidy/Formatter.pm | 42 +++++++++++++++++++++++--------------- lib/Perl/Tidy/Tokenizer.pm | 2 +- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index 5303d5bd..bdaf4187 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -874,9 +874,9 @@ The B<-nibp=string> parameter may be used to change the marker for non-indenting braces. The default is equivalent to -nibp='#<<<'. The string that you enter must begin with a # and should be in quotes as necessary to get past the command shell of your system. This string is the leading text of a -regex pattern that is constructed by appending pre-pending a '^', so you must -also include backslashes for characters to be taken literally rather than as -patterns. +regex pattern that is constructed by appending pre-pending a '^' and appending +a'\s', so you must also include backslashes for characters to be taken +literally rather than as patterns. For example, to match the side comment '#++', the parameter would be diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 91012a26..48ec1c01 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -459,20 +459,14 @@ sub trim { } sub max { - my @vals = @_; - my $max = shift @vals; - foreach my $val (@vals) { - $max = ( $max < $val ) ? $val : $max; - } + my $max = shift; + for (@_) { $max = $_ > $max ? $_ : $max }; return $max; } sub min { - my @vals = @_; - my $min = shift @vals; - foreach my $val (@vals) { - $min = ( $min > $val ) ? $val : $min; - } + my $min = shift; + for (@_) { $min = $_ < $min ? $_ : $min }; return $min; } @@ -4623,17 +4617,30 @@ sub non_indenting_braces { my $is_non_indenting_brace = sub { my ($KK) = @_; + + # looking for an opening block brace my $token = $rLL->[$KK]->[_TOKEN_]; my $block_type = $rLL->[$KK]->[_BLOCK_TYPE_]; return unless ( $token eq '{' && $block_type ); - my $line_index = $rLL->[$KK]->[_LINE_INDEX_]; + + # followed by a comment my $K_sc = $self->K_next_nonblank($KK); return unless defined($K_sc); - my $line_index_sc = $rLL->[$K_sc]->[_LINE_INDEX_]; - return unless ( $line_index_sc == $line_index ); my $type_sc = $rLL->[$K_sc]->[_TYPE_]; return unless ( $type_sc eq '#' ); + + # on the same line + my $line_index = $rLL->[$KK]->[_LINE_INDEX_]; + my $line_index_sc = $rLL->[$K_sc]->[_LINE_INDEX_]; + return unless ( $line_index_sc == $line_index ); + + # get the side comment text my $token_sc = $rLL->[$K_sc]->[_TOKEN_]; + + # The pattern ends in \s but we have removed the newline, so + # we added it back for the match. That way we require an exact + # match to the special string and also allow additional text. + $token_sc .= "\n"; return ( $token_sc =~ /$non_indenting_brace_pattern/ ); }; @@ -6556,8 +6563,11 @@ sub make_format_skipping_pattern { sub make_non_indenting_brace_pattern { - # create the pattern used to identify static side comments - $non_indenting_brace_pattern = '^#<<<'; + # Create the pattern used to identify static side comments. + # Note that we are ending the pattern in a \s. This will allow + # the pattern to be followed by a space and some text, or a newline. + # The pattern is used in sub 'non_indenting_braces' + $non_indenting_brace_pattern = '^#<<<\s'; # allow the user to change it if ( $rOpts->{'non-indenting-brace-prefix'} ) { @@ -6566,7 +6576,7 @@ sub make_non_indenting_brace_pattern { if ( $prefix !~ /^#/ ) { Die("ERROR: the -nibp parameter '$prefix' must begin with '#'\n"); } - my $pattern = '^' . $prefix; + my $pattern = '^' . $prefix . '\s'; if ( bad_pattern($pattern) ) { Die( "ERROR: the -nibp prefix '$prefix' causes the invalid regex '$pattern'\n" diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 16e5af4c..898dc741 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -136,7 +136,7 @@ use constant MAX_NAG_MESSAGES => 6; BEGIN { - # Array index names for token variables + # Array index names for $self my $i = 0; use constant { _rhere_target_list_ => $i++, -- 2.39.5