]> git.donarmstrong.com Git - perltidy.git/commitdiff
minor reorganization of length calc
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 17 Apr 2020 14:16:05 +0000 (07:16 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 17 Apr 2020 14:16:05 +0000 (07:16 -0700)
CHANGES.md
lib/Perl/Tidy.pm
lib/Perl/Tidy/Formatter.pm

index 3ec41d2deaa2b0098d2e0dc4762f5ca98772d0e5..4e8febe4b1ce227e3b98ec0980d9eaeac709cd89 100644 (file)
@@ -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
index 1f7ab6a19bb8766da9c9b37212b60451aa507efa..4825d1fa46e3b5d5def02da986379b63fcbc54ea 100644 (file)
@@ -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 {
index 148cda539a00e5e3a4a573123f460bdad3082f6b..efbbc4e58f62a140067b58c9e6a2ab5fd3070c7a 100644 (file)
@@ -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;