From: Steve Hancock Date: Fri, 18 Jun 2021 13:53:29 +0000 (-0700) Subject: Added flag -atnl, --add-terminal-newline, see git #58 X-Git-Tag: 20210402.01~3 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=6f831708372a039b24112b9045193b7d21356895;p=perltidy.git Added flag -atnl, --add-terminal-newline, see git #58 --- diff --git a/CHANGES.md b/CHANGES.md index 70e90bd4..9ffb99a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,14 @@ prevent formatting instability when line lengths are limited by the maximum line length. Most scripts will not be affected. + - Added flag -atnl, --add-terminal-newline, to help issue git #58. + This flag, which is enabled by default, allows perltidy to terminate + the last line of the output stream with a newline character, regardless + of whether or not the input stream was terminated with a newline + character. If this flag is negated, with -natnl, then perltidy will + add a terminal newline to the the output stream only if the input + stream is terminated with a newline. + - A more complete list of updates is at https://github.com/perltidy/perltidy/blob/master/local-docs/BugLog.pod diff --git a/bin/perltidy b/bin/perltidy index 4c5ee6dc..bdd46ed3 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -518,41 +518,6 @@ unstable editing). =back -=item B<-syn>, B<--check-syntax> - -This flag is now ignored for safety, but the following documentation -has been retained for reference. - -This flag causes perltidy to run C to check syntax of input -and output. (To change the flags passed to perl, see the next -item, B<-pscf>). The results are written to the F<.LOG> file, which -will be saved if an error is detected in the output script. The output -script is not checked if the input script has a syntax error. Perltidy -does its own checking, but this option employs perl to get a "second -opinion". - -If perl reports errors in the input file, they will not be reported in -the error output unless the B<--warning-output> flag is given. - -The default is B to do this type of syntax checking (although -perltidy will still do as much self-checking as possible). The reason -is that it causes all code in BEGIN blocks to be executed, for all -modules being used, and this opens the door to security issues and -infinite loops when running perltidy. - -=item B<-pscf=s>, B<-perl-syntax-check-flags=s> - -When perl is invoked to check syntax, the normal flags are C<-c -T>. In -addition, if the B<-x> flag is given to perltidy, then perl will also be -passed a B<-x> flag. It should not normally be necessary to change -these flags, but it can be done with the B<-pscf=s> flag. For example, -if the taint flag, C<-T>, is not wanted, the flag could be set to be just -B<-pscf=-c>. - -Perltidy will pass your string to perl with the exception that it will -add a B<-c> and B<-x> if appropriate. The F<.LOG> file will show -exactly what flags were passed to perl. - =item B<-xs>, B<--extended-syntax> A problem with formatting Perl code is that some modules can introduce new @@ -574,6 +539,7 @@ This flag is enabled by default but it can be deactivated with B<-nxs>. Probably the only reason to deactivate this flag is to generate more diagnostic messages when debugging a script. +For another method of handling extended syntax see the section L. =item B<-io>, B<--indent-only> @@ -687,6 +653,17 @@ input comes from a filename (rather than stdin, for example). If perltidy has trouble determining the input file line ending, it will revert to the default behavior of using the line ending of the host system. +=item B<-atnl>, B<--add-terminal-newline> + +This flag, which is enabled by default, allows perltidy to terminate the last +line of the output stream with a newline character, regardless of whether or +not the input stream was terminated with a newline character. If this flag is +negated, with B<-natnl>, then perltidy will add a terminal newline to the the +output stream only if the input stream is terminated with a newline. + +Negating this flag may be useful for manipulating one-line scripts intended for +use on a command line. + =item B<-it=n>, B<--iterations=n> This flag causes perltidy to do B complete iterations. The reason for this @@ -4216,15 +4193,16 @@ Space before and after the curly braces is optional. For a specific example, the following line - oneliner { --maximum-line-length=0 --noadd-newlines } + oneliner { --maximum-line-length=0 --noadd-newlines --noadd-terminal-newline} or equivalently with abbreviations - oneliner { -l=0 -nanl } + oneliner { -l=0 -nanl -natnl } could be placed in a F<.perltidyrc> file to temporarily override the maximum -line length with a large value, and to temporarily prevent newlines from being -added. All other settings in the F<.perltidyrc> file still apply. Thus it +line length with a large value, to temporarily prevent new line breaks from +being added, and to prevent an extra newline character from being added the +file. All other settings in the F<.perltidyrc> file still apply. Thus it provides a way to format a long 'one liner' when perltidy is invoked with perltidy --oneliner ... diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index e41ed58f..46bd89d9 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1012,6 +1012,9 @@ EOM $buf .= $line; } + my $remove_terminal_newline = + !$rOpts->{'add-terminal-newline'} && substr( $buf, -1, 1 ) !~ /\n/; + # Decode the input stream if necessary requested my $encoding_in = ""; my $rOpts_character_encoding = $rOpts->{'character-encoding'}; @@ -1275,6 +1278,7 @@ EOM my ( $sink_object, $postfilter_buffer ); my $use_buffer = $postfilter + || $remove_terminal_newline || $rOpts->{'assert-tidy'} || $rOpts->{'assert-untidy'}; @@ -1614,9 +1618,29 @@ EOM rOpts => $rOpts, rpending_logfile_message => $rpending_logfile_message, ); - while ( my $line = $source_object->get_line() ) { - $sink_object->write_line($line); + + # Copy the filtered buffer to the final destination + if ( !$remove_terminal_newline ) { + while ( my $line = $source_object->get_line() ) { + $sink_object->write_line($line); + } } + else { + + # Copy the filtered buffer but remove the newline char from the + # final line + my $line; + while ( my $next_line = $source_object->get_line() ) { + $sink_object->write_line($line) if ($line); + $line = $next_line; + } + if ($line) { + $sink_object->set_line_separator(undef); + chomp $line; + $sink_object->write_line($line); + } + } + $source_object->close_input_file(); } @@ -2222,6 +2246,7 @@ sub generate_options { $add_option->( 'standard-output', 'st', '!' ); $add_option->( 'use-unicode-gcstring', 'gcs', '!' ); $add_option->( 'warning-output', 'w', '!' ); + $add_option->( 'add-terminal-newline', 'atnl', '!' ); # options which are both toggle switches and values moved here # to hide from tidyview (which does not show category 0 flags): @@ -2546,6 +2571,7 @@ sub generate_options { #--------------------------------------------------------------- my @defaults = qw( add-newlines + add-terminal-newline add-semicolons add-whitespace blanks-before-blocks diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 9de60cd6..e8a29473 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,21 @@ =over 4 +=item B + +This flag, which is enabled by default, allows perltidy to terminate the last +line of the output stream with a newline character, regardless of whether or +not the input stream was terminated with a newline character. If this flag is +negated, with B<-natnl>, then perltidy will add a terminal newline to the the +output stream only if the input stream is terminated with a newline. + +Negating this flag may be useful for manipulating one-line scripts intended for +use on a command line. + +This update also removes the description of the obsolete --check-syntax flag from the man pages and help text. + +18 Jun 2021. + =item B The -nanl flag (--noadd-newlines) was preventing side comments from being @@ -46,14 +61,14 @@ This update fixes the problem, case b1162. This is a minor optimization. These subs are eliminated: is_welded_right_at_K, is_welded_left_at_K, weld_len_right_at_K. -17 Jun 2021. +17 Jun 2021, 1691013. =item B This update is necessary to eventually prevent an unwanted terminal newline being added to a file. -17 Jun 2021. +17 Jun 2021, 2600533. =item B