From: Steve Hancock Date: Mon, 6 Feb 2023 01:36:55 +0000 (-0800) Subject: update output-line-ending code, part 1 X-Git-Tag: 20230309~33 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=06a9c5211c7485ef7a00e9b4a346a3dcd34bef93;p=perltidy.git update output-line-ending code, part 1 Moved from Formatter.pm to Tidy.pm, a more logical place --- diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index ed746b0c..5bd99885 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -4293,6 +4293,8 @@ sub check_options { EOM } + initialize_output_line_ending($rOpts); + # Since -vt, -vtc, and -cti are abbreviations, but under # msdos, an unquoted input parameter like vtc=1 will be # seen as 2 parameters, vtc and 1, so the abbreviations @@ -4491,6 +4493,58 @@ EOM return $tabsize; } ## end sub check_options +sub initialize_output_line_ending { + my ($rOpts) = @_; + + # TODO: + # - need to call with $self + # - the output_line_ending should stored in _line_separator_ + + my $ole = $rOpts->{'output-line-ending'}; + if ($ole) { + my %endings = ( + dos => "\015\012", + win => "\015\012", + mac => "\015", + unix => "\012", + ); + + # Patch for RT #99514, a memoization issue. + # Normally, the user enters one of 'dos', 'win', etc, and we change the + # value in the options parameter to be the corresponding line ending + # character. But, if we are using memoization, on later passes through + # here the option parameter will already have the desired ending + # character rather than the keyword 'dos', 'win', etc. So + # we must check to see if conversion has already been done and, if so, + # bypass the conversion step. + my %endings_inverted = ( + "\015\012" => 'dos', + "\015\012" => 'win', + "\015" => 'mac', + "\012" => 'unix', + ); + + if ( defined( $endings_inverted{$ole} ) ) { + + # we already have valid line ending, nothing more to do + } + else { + $ole = lc $ole; + unless ( $rOpts->{'output-line-ending'} = $endings{$ole} ) { + my $str = join SPACE, keys %endings; + Die(<{'preserve-line-endings'} ) { + Warn("Ignoring -ple; conflicts with -ole\n"); + $rOpts->{'preserve-line-endings'} = undef; + } + } + } + return; +} ## end sub initialize_output_line_ending + sub find_file_upwards { my ( $search_dir, $search_file ) = @_; diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index a0410c22..91c839f7 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -1657,49 +1657,6 @@ EOM $rOpts->{'long-block-line-count'} = 1_000_000; } - my $ole = $rOpts->{'output-line-ending'}; - if ($ole) { - my %endings = ( - dos => "\015\012", - win => "\015\012", - mac => "\015", - unix => "\012", - ); - - # Patch for RT #99514, a memoization issue. - # Normally, the user enters one of 'dos', 'win', etc, and we change the - # value in the options parameter to be the corresponding line ending - # character. But, if we are using memoization, on later passes through - # here the option parameter will already have the desired ending - # character rather than the keyword 'dos', 'win', etc. So - # we must check to see if conversion has already been done and, if so, - # bypass the conversion step. - my %endings_inverted = ( - "\015\012" => 'dos', - "\015\012" => 'win', - "\015" => 'mac', - "\012" => 'unix', - ); - - if ( defined( $endings_inverted{$ole} ) ) { - - # we already have valid line ending, nothing more to do - } - else { - $ole = lc $ole; - unless ( $rOpts->{'output-line-ending'} = $endings{$ole} ) { - my $str = join SPACE, keys %endings; - Die(<{'preserve-line-endings'} ) { - Warn("Ignoring -ple; conflicts with -ole\n"); - $rOpts->{'preserve-line-endings'} = undef; - } - } - } - # hashes used to simplify setting whitespace %tightness = ( '{' => $rOpts->{'brace-tightness'},