From 05a9129b482769d9e6d1c0c18a5d5088f1d46f3a Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Fri, 17 Apr 2020 07:16:05 -0700 Subject: [PATCH] minor reorganization of length calc --- CHANGES.md | 10 ++++++---- lib/Perl/Tidy.pm | 17 +++++++++++------ lib/Perl/Tidy/Formatter.pm | 28 ++++++++++------------------ 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 3ec41d2d..4e8febe4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,10 +8,12 @@ - Added --use-unicode-gcstring to control use of Unicode::GCString for evaluating character widths of encoded data. By default, for encoded files perltidy will now look for Unicode::GCString and, if found, will use it - to evaluate character display widths. This improves displayed - vertical alignment. This flag is mainly intended for testing. Perltidy - installation does not require Unicode::GCString, so users wanting - to use this feature need to install it separately. + to evaluate character display widths. This can improve displayed + vertical alignment for files with wide characters. This flag is mainly intended + for testing, but can also prevent the use of this module in the event that some + unforseen issue arises. Perltidy installation does not require + Unicode::GCString, so users wanting to use this feature need to install it + separately. - Added --character-encoding=guess or -guess to have perltidy guess if a file is encoded as -utf8 or some older single-byte encoding. This diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 1f7ab6a1..4825d1fa 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1020,7 +1020,8 @@ EOM # we must not treat it as encoded data. my $is_encoded_data = $encoding_in ? 'utf8' : ""; - my $use_unicode_gcstring; + # Define the function to determine the display width of character strings + my $length_function = sub { return length( $_[0] ) }; if ($is_encoded_data) { # Delete any Byte Order Mark (BOM), which can cause trouble @@ -1032,7 +1033,11 @@ EOM eval { require Unicode::GCString }; $loaded_unicode_gcstring = !$@; } - $use_unicode_gcstring = $loaded_unicode_gcstring; + if ($loaded_unicode_gcstring) { + $length_function = sub { + return Unicode::GCString->new( $_[0] )->columns; + }; + } } # MD5 sum of input file is evaluated before any prefilter @@ -1251,10 +1256,10 @@ EOM } elsif ( $rOpts->{'format'} eq 'tidy' ) { $formatter = Perl::Tidy::Formatter->new( - logger_object => $logger_object, - diagnostics_object => $diagnostics_object, - sink_object => $sink_object, - use_unicode_gcstring => $use_unicode_gcstring, + logger_object => $logger_object, + diagnostics_object => $diagnostics_object, + sink_object => $sink_object, + length_function => $length_function, ); } else { diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 148cda53..efbbc4e5 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -588,14 +588,14 @@ sub new { # we are given an object with a write_line() method to take lines my %defaults = ( - sink_object => undef, - diagnostics_object => undef, - logger_object => undef, - use_unicode_gcstring => undef, + sink_object => undef, + diagnostics_object => undef, + logger_object => undef, + length_function => sub { return length( $_[0] ) }, ); my %args = ( %defaults, @args ); - my $use_unicode_gcstring = $args{use_unicode_gcstring}; + my $length_function = $args{length_function}; $logger_object = $args{logger_object}; $diagnostics_object = $args{diagnostics_object}; @@ -730,11 +730,11 @@ sub new { rK_phantom_semicolons => undef, # for undoing phantom semicolons if iterating rpaired_to_inner_container => {}, - rbreak_container => {}, # prevent one-line blocks - rshort_nested => {}, # blocks not forced open - rvalid_self_keys => [], # for checking + rbreak_container => {}, # prevent one-line blocks + rshort_nested => {}, # blocks not forced open + rvalid_self_keys => [], # for checking valign_batch_count => 0, - use_unicode_gcstring => $use_unicode_gcstring, + length_function => $length_function, }; my @valid_keys = keys %{$formatter_self}; $formatter_self->{rvalid_self_keys} = \@valid_keys; @@ -2326,15 +2326,7 @@ sub respace_tokens { my $Klimit_old = $self->{Klimit}; my $rlines = $self->{rlines}; my $rpaired_to_inner_container = $self->{rpaired_to_inner_container}; - my $use_unicode_gcstring = $self->{use_unicode_gcstring}; - - # Define a function for evaluating the display width of text - my $length_function = sub { return length( $_[0] ) }; - if ($use_unicode_gcstring) { - $length_function = sub { - return Unicode::GCString->new( $_[0] )->columns; - }; - } + my $length_function = $self->{length_function}; my $rLL_new = []; # This is the new array my $KK = 0; -- 2.39.5