From 36d4c3568a830bf36a45d1aecde66c9fc97c91c6 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 22 Dec 2020 06:25:10 -0800 Subject: [PATCH] added code to transition to new method for marking lists --- lib/Perl/Tidy/Formatter.pm | 28 ++++++------------- lib/Perl/Tidy/VerticalAligner.pm | 40 ++++++++++++++++++++------- lib/Perl/Tidy/VerticalAligner/Line.pm | 10 ++----- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 991eacce..a43f4e3b 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -16716,8 +16716,6 @@ sub reduce_lp_indentation { # CODE SECTION 13: Preparing batches for vertical alignment ########################################################### -use constant TEST_NEW_LIST_METHOD => 0; - sub send_lines_to_vertical_aligner { my ($self) = @_; @@ -16994,23 +16992,12 @@ sub send_lines_to_vertical_aligner { $rfield_lengths->[-1] += 2; } - ################################################# - # TESTING different methods for indicating a list - ################################################# - - # TODO: - # - change 'is_forced_break' to 'is_list' - # - compare list names in vertical aligner - # - try using K of lowest level comma instead of Kbeg - # - maybe combine the best of both list methods - - # Old method: Works well; a few problems, esp with side comments - my $list_flag_old = $forced_breakpoint || $in_comma_list; - - # Test method: Working well but still has a few quirks - my $list_flag_new = $self->is_list_by_K($Kbeg); + # List flag for old method: Works well; a few problems, esp with side + # comments. This will eventually be replaced with the new method. + my $is_forced_break = $forced_breakpoint || $in_comma_list; - my $list_flag = TEST_NEW_LIST_METHOD ? $list_flag_new : $list_flag_old; + # List flag for new method: Works well but needs more testing + my $list_seqno = $self->is_list_by_K($Kbeg); # send this new line down the pipe my $rvalign_hash = {}; @@ -17018,7 +17005,8 @@ sub send_lines_to_vertical_aligner { $rvalign_hash->{level_end} = $level_end; $rvalign_hash->{level_adj} = $level_adj; $rvalign_hash->{indentation} = $indentation; - $rvalign_hash->{is_forced_break} = $list_flag; + $rvalign_hash->{is_forced_break} = $is_forced_break; + $rvalign_hash->{list_seqno} = $list_seqno; $rvalign_hash->{outdent_long_lines} = $outdent_long_lines; $rvalign_hash->{is_terminal_ternary} = $is_terminal_ternary; $rvalign_hash->{is_terminal_statement} = $is_semicolon_terminated; @@ -18717,7 +18705,7 @@ sub make_paren_name { # 4. formatting with the -lp option is complicated, and does not # work well with qw quotes and with -wn formatting. # 5. a number of special situations, such as 'cuddled' formatting. - # 6. This routine is mainly concerned with outdenting closing tokens + # 6. This routine is mainly concerned with outdenting closing tokens # but note that there is some overlap with the functions of sub # undo_ci, which was processed earlier, so care has to be taken to # keep them coordinated. diff --git a/lib/Perl/Tidy/VerticalAligner.pm b/lib/Perl/Tidy/VerticalAligner.pm index 3b64396a..b8898442 100644 --- a/lib/Perl/Tidy/VerticalAligner.pm +++ b/lib/Perl/Tidy/VerticalAligner.pm @@ -337,6 +337,11 @@ sub push_group_line { use constant DEBUG_VALIGN => 0; +# Flag for use during conversion to using new $list_seqno +# to identify lists of items. The flag 'is_forced_break' will +# be removed when conversion is complete. +use constant TEST_LIST_SEQNO => 0; + sub valign_input { # Place one line in the current vertical group. @@ -397,6 +402,7 @@ sub valign_input { my $level_adj = $rline_hash->{level_adj}; my $indentation = $rline_hash->{indentation}; my $is_forced_break = $rline_hash->{is_forced_break}; + my $list_seqno = $rline_hash->{list_seqno}; my $outdent_long_lines = $rline_hash->{outdent_long_lines}; my $is_terminal_ternary = $rline_hash->{is_terminal_ternary}; my $is_terminal_statement = $rline_hash->{is_terminal_statement}; @@ -693,13 +699,13 @@ EOM indentation => $indentation, leading_space_count => $leading_space_count, outdent_long_lines => $outdent_long_lines, + list_seqno => $list_seqno, list_type => "", is_hanging_side_comment => $is_hanging_side_comment, maximum_line_length => $maximum_line_length_for_level, rvertical_tightness_flags => $rvertical_tightness_flags, is_terminal_ternary => $is_terminal_ternary, j_terminal_match => $j_terminal_match, - is_forced_break => $is_forced_break, end_group => $break_alignment_after, Kend => $Kend, ci_level => $ci_level, @@ -709,11 +715,13 @@ EOM # -------------------------------------------------------------------- # Decide if this is a simple list of items. - # There are 3 list types: none, comma, comma-arrow. - # We use this below to be less restrictive in deciding what to align. + # We use this to be less restrictive in deciding what to align. # -------------------------------------------------------------------- - if ($is_forced_break) { - decide_if_list($new_line); + if (TEST_LIST_SEQNO) { + decide_if_list($new_line) if ($list_seqno); + } + else { + decide_if_list($new_line) if ($is_forced_break); } # -------------------------------------------------------------------- @@ -1823,10 +1831,11 @@ sub sweep_left_to_right { # Special treatment of two one-line groups isolated from other lines, # unless they form a simple list or a terminal match. Otherwise the # alignment can look strange in some cases. + my $list_type = $rlines->[$jbeg]->get_list_type(); if ( $jend == $jbeg && $jend_m == $jbeg_m - && !$rlines->[$jbeg]->get_list_type() + && !( $list_type && !TEST_LIST_SEQNO ) && ( $ng == 1 || $istop_mm < 0 ) && ( $ng == $ng_max || $istop < 0 ) && !$line->get_j_terminal_match() @@ -1839,8 +1848,10 @@ sub sweep_left_to_right { ) { - # We will just align a leading equals - next unless ( $imax_min >= 0 && $rtokens->[0] =~ /^=\d/ ); + # We will just align assignments and simple lists + next + unless ( $imax_min >= 0 && $rtokens->[0] =~ /^=\d/ + || ( TEST_LIST_SEQNO && $list_type ) ); # In this case we will limit padding to one indent distance. This # is a compromise to keep some vertical alignment but prevent large @@ -2992,10 +3003,11 @@ sub match_line_pairs { # Previous line vars my ( $line_m, $rtokens_m, $rpatterns_m, $rfield_lengths_m, $imax_m, - $list_type_m ); + $list_type_m, $ci_level_m ); # Current line vars - my ( $line, $rtokens, $rpatterns, $rfield_lengths, $imax, $list_type ); + my ( $line, $rtokens, $rpatterns, $rfield_lengths, $imax, $list_type, + $ci_level ); use constant EXPLAIN_COMPARE_PATTERNS => 0; @@ -3115,6 +3127,7 @@ sub match_line_pairs { $rfield_lengths_m = $rfield_lengths; $imax_m = $imax; $list_type_m = $list_type; + $ci_level_m = $ci_level; $line = $rnew_lines->[$jj]; $rtokens = $line->get_rtokens(); @@ -3122,10 +3135,13 @@ sub match_line_pairs { $rfield_lengths = $line->get_rfield_lengths(); $imax = @{$rtokens} - 2; $list_type = $line->get_list_type(); + $ci_level = $line->get_ci_level(); # nothing to do for first line next if ( $jj == $jbeg ); + my $ci_jump = $ci_level - $ci_level_m; + my $imax_min = $imax_m < $imax ? $imax_m : $imax; my $imax_align = -1; @@ -3146,6 +3162,9 @@ sub match_line_pairs { ############################## elsif ( $list_type && $list_type eq $list_type_m ) { + # do not align lists across a ci jump with new list method + if ( TEST_LIST_SEQNO && $ci_jump ) { $imax_min = -1 } + my $i_nomatch = $imax_min + 1; for ( my $i = 0 ; $i <= $imax_min ; $i++ ) { my $tok = $rtokens->[$i]; @@ -3155,6 +3174,7 @@ sub match_line_pairs { last; } } + $imax_align = $i_nomatch - 1; } diff --git a/lib/Perl/Tidy/VerticalAligner/Line.pm b/lib/Perl/Tidy/VerticalAligner/Line.pm index a674ac6d..9ea32100 100644 --- a/lib/Perl/Tidy/VerticalAligner/Line.pm +++ b/lib/Perl/Tidy/VerticalAligner/Line.pm @@ -21,6 +21,7 @@ BEGIN { _indentation_ => $i++, _leading_space_count_ => $i++, _outdent_long_lines_ => $i++, + _list_seqno_ => $i++, _list_type_ => $i++, _is_hanging_side_comment_ => $i++, _ralignments_ => $i++, @@ -29,7 +30,6 @@ BEGIN { _is_terminal_ternary_ => $i++, _is_terminal_else_ => $i++, _j_terminal_match_ => $i++, - _is_forced_break_ => $i++, _end_group_ => $i++, _Kend_ => $i++, _ci_level_ => $i++, @@ -76,13 +76,13 @@ EOM $self->[_leading_space_count_] = $ri->{leading_space_count}; $self->[_outdent_long_lines_] = $ri->{outdent_long_lines}; $self->[_list_type_] = $ri->{list_type}; + $self->[_list_seqno_] = $ri->{list_seqno}; $self->[_is_hanging_side_comment_] = $ri->{is_hanging_side_comment}; $self->[_maximum_line_length_] = $ri->{maximum_line_length}; $self->[_rvertical_tightness_flags_] = $ri->{rvertical_tightness_flags}; $self->[_is_terminal_ternary_] = $ri->{is_terminal_ternary}; $self->[_is_terminal_else_] = $ri->{is_terminal_else}; $self->[_j_terminal_match_] = $ri->{j_terminal_match}; - $self->[_is_forced_break_] = $ri->{is_forced_break}; $self->[_end_group_] = $ri->{end_group}; $self->[_Kend_] = $ri->{Kend}; $self->[_ci_level_] = $ri->{ci_level}; @@ -102,6 +102,7 @@ EOM sub get_indentation { return $_[0]->[_indentation_] } sub get_Kend { return $_[0]->[_Kend_] } sub get_ci_level { return $_[0]->[_ci_level_] } + sub get_list_seqno { return $_[0]->[_list_seqno_] } sub get_imax_pair { return $_[0]->[_imax_pair_] } @@ -129,10 +130,6 @@ EOM return $_[0]->[_is_terminal_ternary_]; } - sub get_is_forced_break { - return $_[0]->[_is_forced_break_]; - } - sub get_leading_space_count { return $_[0]->[_leading_space_count_]; } @@ -256,4 +253,3 @@ EOM } 1; - -- 2.39.5