From 488e715209a4ef57f1057a499ff66ede205d9263 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 22 Nov 2021 10:00:10 -0800 Subject: [PATCH] code simplification --- lib/Perl/Tidy/Formatter.pm | 45 ++++++++++++++------------------ lib/Perl/Tidy/IndentationItem.pm | 16 ++++-------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 7fe8180a..22316b17 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -19559,7 +19559,6 @@ sub get_available_spaces_to_go { # option. The coding is rather complex, but is only for -lp. my $gnu_position_predictor; - my $gnu_sequence_number; my $line_start_index_to_go; my $max_gnu_item_index; my $max_gnu_stack_index; @@ -19617,7 +19616,6 @@ sub get_available_spaces_to_go { # initialize gnu variables for a new batch; # must be called before each new batch - $gnu_sequence_number++; # increment output batch counter %last_gnu_equals = (); %gnu_comma_count = (); %gnu_arrow_count = (); @@ -19644,15 +19642,14 @@ sub get_available_spaces_to_go { } my $item = Perl::Tidy::IndentationItem->new( - spaces => $spaces, - level => $level, - ci_level => $ci_level, - available_spaces => $available_spaces, - index => $index, - gnu_sequence_number => $gnu_sequence_number, - align_paren => $align_paren, - stack_depth => $max_gnu_stack_index, - starting_index_K => $starting_index_K, + spaces => $spaces, + level => $level, + ci_level => $ci_level, + available_spaces => $available_spaces, + lp_stack_index => $index, + align_paren => $align_paren, + stack_depth => $max_gnu_stack_index, + starting_index_K => $starting_index_K, ); if ( $level >= 0 ) { @@ -19887,26 +19884,24 @@ sub get_available_spaces_to_go { # Undo any extra indentation if we saw no commas my $available_spaces = $gs_object->get_available_spaces(); + my $K_start = $gs_object->get_starting_index_K(); - if ( $available_spaces > 0 + if ( $available_spaces > 0 + && $K_start >= $K_to_go[0] && ( $comma_count <= 0 || $arrow_count > 0 ) ) { - my $i = $gs_object->get_index(); - my $seqno = $gs_object->get_sequence_number(); + my $i = $gs_object->get_lp_stack_index(); - # Be sure this item was created in this batch. - # This should be true because we delete any - # available space from open items at the end of - # each batch. - if ( $gnu_sequence_number != $seqno - || $i > $max_gnu_item_index ) - { - # non-fatal, keep going except in DEVEL_MODE - # DEACTIVATED for testing -xlp - if ( 0 && DEVEL_MODE ) { + # Safety check for a valid stack index. It + # should be ok because we just checked that the + # index K of the token associated with this + # indentation is in this batch. + if ( $i < 0 || $i > $max_gnu_item_index ) { + if (DEVEL_MODE) { + my $lno = $rLL->[$KK]->[_LINE_INDEX_]; Fault(<=0 and <= max=$max_gnu_item_index EOM } } diff --git a/lib/Perl/Tidy/IndentationItem.pm b/lib/Perl/Tidy/IndentationItem.pm index aee0a7bc..f85e39a7 100644 --- a/lib/Perl/Tidy/IndentationItem.pm +++ b/lib/Perl/Tidy/IndentationItem.pm @@ -23,7 +23,7 @@ BEGIN { _closed_ => $i++, _comma_count_ => $i++, _sequence_number_ => $i++, - _index_ => $i++, + _lp_stack_index_ => $i++, _have_child_ => $i++, _recoverable_spaces_ => $i++, _align_paren_ => $i++, @@ -74,8 +74,7 @@ sub new { # # for this level # closed => # index where we saw closing '}' # comma_count => # how many commas at this level? - # sequence_number => # output batch number - # index => # index in output batch list + # lp_stack_index => # index in output batch list # have_child => # any dependents? # recoverable_spaces => # how many spaces to the right # # we would like to move to get @@ -94,8 +93,7 @@ sub new { $self->[_available_spaces_] = $input_hash{available_spaces}; $self->[_closed_] = -1; $self->[_comma_count_] = 0; - $self->[_sequence_number_] = $input_hash{gnu_sequence_number}; - $self->[_index_] = $input_hash{index}; + $self->[_lp_stack_index_] = $input_hash{lp_stack_index}; $self->[_have_child_] = 0; $self->[_recoverable_spaces_] = 0; $self->[_align_paren_] = $input_hash{align_paren}; @@ -227,12 +225,8 @@ sub get_spaces_level_ci { return [ $self->[_spaces_], $self->[_level_], $self->[_ci_level_] ]; } -sub get_sequence_number { - return $_[0]->[_sequence_number_]; -} - -sub get_index { - return $_[0]->[_index_]; +sub get_lp_stack_index { + return $_[0]->[_lp_stack_index_]; } sub get_starting_index_K { -- 2.39.5