From 52e5e298e008993cb27dafabf98df8fbc446408e Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Thu, 6 Aug 2020 07:27:40 -0700 Subject: [PATCH] replace pattern match in sub decide_if_list with call to decode --- lib/Perl/Tidy/VerticalAligner.pm | 56 ++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/lib/Perl/Tidy/VerticalAligner.pm b/lib/Perl/Tidy/VerticalAligner.pm index 7d6654e5..45fe9596 100644 --- a/lib/Perl/Tidy/VerticalAligner.pm +++ b/lib/Perl/Tidy/VerticalAligner.pm @@ -426,7 +426,7 @@ sub valign_input { set_cached_line_valid(0); } - # patch until new aligner is finished + # caller might request no alignment in special cases if ($do_not_pad) { $self->my_flush() } # shouldn't happen: @@ -653,7 +653,8 @@ sub valign_input { sub join_hanging_comment { # Add dummy fields to a hanging side comment to make it look - # like the first line in its potential group. + # like the first line in its potential group. This simplifies + # the coding. my ( $new_line, $old_line ) = @_; my $jmax = $new_line->get_jmax(); @@ -739,32 +740,45 @@ sub make_side_comment { return; } -sub decide_if_list { +{ # closure for decide_if_list - my $line = shift; + my %is_comma_token; - # A list will be taken to be a line with a forced break in which all - # of the field separators are commas or comma-arrows (except for the - # trailing #) + BEGIN { + + my @q = qw( => ); + push @q, ','; + @is_comma_token{@q} = (1) x scalar(@q); + } - # List separator tokens are things like ',3' or '=>2', - # where the trailing digit is the nesting depth. Allow braces - # to allow nested list items. - my $rtokens = $line->get_rtokens(); - my $test_token = $rtokens->[0]; - if ( $test_token =~ /^(\,|=>)/ ) { - my $list_type = $test_token; - my $jmax = $line->get_jmax(); + sub decide_if_list { - foreach ( 1 .. $jmax - 2 ) { - if ( $rtokens->[$_] !~ /^(\,|=>)/ ) { - $list_type = ""; - last; + my $line = shift; + + # A list will be taken to be a line with a forced break in which all + # of the field separators are commas or comma-arrows (except for the + # trailing #) + + my $rtokens = $line->get_rtokens(); + my $test_token = $rtokens->[0]; + my ( $raw_tok, $lev, $tag, $tok_count ) = + decode_alignment_token($test_token); + if ( $is_comma_token{$raw_tok} ) { + my $list_type = $test_token; + my $jmax = $line->get_jmax(); + + foreach ( 1 .. $jmax - 2 ) { + ( $raw_tok, $lev, $tag, $tok_count ) = + decode_alignment_token( $rtokens->[$_] ); + if ( !$is_comma_token{$raw_tok} ) { + $list_type = ""; + last; + } } + $line->set_list_type($list_type); } - $line->set_list_type($list_type); + return; } - return; } sub fix_terminal_ternary { -- 2.39.5