From: Steve Hancock Date: Mon, 26 Jun 2023 01:47:34 +0000 (-0700) Subject: add option -ipscl X-Git-Tag: 20230309.04~3 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=a5a946c16ae8f9b8c8c0ac0f95fde1a1710d6238;p=perltidy.git add option -ipscl --- diff --git a/CHANGES.md b/CHANGES.md index a6c8ae4c..57f33edf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,13 @@ ## 2023 03 09.03 + - Add parameter -ipscl, or --ignore-perlcritic-side-comment-lengths. This + is on by default to help avoid problems due to unwanted line breaks + caused by long side comments beginning with '## no critic'. Perlcritic + users can also use the more general parameter B<-iscl> for this purpose, + but that parameter is off by default. This new parameter is on by + default and serves as a backup in case B<-iscl> has not been set. + - Issue git #121. Added parameters -xbt, or --extended-block-tightness, and -xbtl=s, or --extended-block-tightness-list=s, to allow certain small code blocks to have internal spacing controlled by diff --git a/bin/perltidy b/bin/perltidy index f5cec5a8..228d13db 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -1793,6 +1793,14 @@ whereas without the -iscl flag the line will be broken: $vmsfile =~ s/;[\d\-]*$// ; # Clip off version number; we can use a newer version as well +=item B<-ipscl>, B<--ignore-perlcritic-side-comment-lengths> + +The parameter B<-ipscl> tells perltidy to ignore the length of side comments +beginning with B<## no critic> when making line breaks, even if the previous +parameter B<-iscl> has not been set. This parameter is on by default to avoid +allowing these special B comments, which are often quite long, to +cause an unwanted line break, and thereby making the side comment ineffective +in controlling B. =item B<-hsc>, B<--hanging-side-comments> diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 9c0f56a0..35436e7c 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3243,34 +3243,35 @@ sub generate_options { ######################################## $category = 4; # Comment controls ######################################## - $add_option->( 'closing-side-comment-else-flag', 'csce', '=i' ); - $add_option->( 'closing-side-comment-interval', 'csci', '=i' ); - $add_option->( 'closing-side-comment-list', 'cscl', '=s' ); - $add_option->( 'closing-side-comment-maximum-text', 'csct', '=i' ); - $add_option->( 'closing-side-comment-prefix', 'cscp', '=s' ); - $add_option->( 'closing-side-comment-warnings', 'cscw', '!' ); - $add_option->( 'closing-side-comments', 'csc', '!' ); - $add_option->( 'closing-side-comments-balanced', 'cscb', '!' ); - $add_option->( 'code-skipping', 'cs', '!' ); - $add_option->( 'code-skipping-begin', 'csb', '=s' ); - $add_option->( 'code-skipping-end', 'cse', '=s' ); - $add_option->( 'format-skipping', 'fs', '!' ); - $add_option->( 'format-skipping-begin', 'fsb', '=s' ); - $add_option->( 'format-skipping-end', 'fse', '=s' ); - $add_option->( 'hanging-side-comments', 'hsc', '!' ); - $add_option->( 'indent-block-comments', 'ibc', '!' ); - $add_option->( 'indent-spaced-block-comments', 'isbc', '!' ); - $add_option->( 'fixed-position-side-comment', 'fpsc', '=i' ); - $add_option->( 'minimum-space-to-comment', 'msc', '=i' ); - $add_option->( 'non-indenting-braces', 'nib', '!' ); - $add_option->( 'non-indenting-brace-prefix', 'nibp', '=s' ); - $add_option->( 'outdent-long-comments', 'olc', '!' ); - $add_option->( 'outdent-static-block-comments', 'osbc', '!' ); - $add_option->( 'static-block-comment-prefix', 'sbcp', '=s' ); - $add_option->( 'static-block-comments', 'sbc', '!' ); - $add_option->( 'static-side-comment-prefix', 'sscp', '=s' ); - $add_option->( 'static-side-comments', 'ssc', '!' ); - $add_option->( 'ignore-side-comment-lengths', 'iscl', '!' ); + $add_option->( 'closing-side-comment-else-flag', 'csce', '=i' ); + $add_option->( 'closing-side-comment-interval', 'csci', '=i' ); + $add_option->( 'closing-side-comment-list', 'cscl', '=s' ); + $add_option->( 'closing-side-comment-maximum-text', 'csct', '=i' ); + $add_option->( 'closing-side-comment-prefix', 'cscp', '=s' ); + $add_option->( 'closing-side-comment-warnings', 'cscw', '!' ); + $add_option->( 'closing-side-comments', 'csc', '!' ); + $add_option->( 'closing-side-comments-balanced', 'cscb', '!' ); + $add_option->( 'code-skipping', 'cs', '!' ); + $add_option->( 'code-skipping-begin', 'csb', '=s' ); + $add_option->( 'code-skipping-end', 'cse', '=s' ); + $add_option->( 'format-skipping', 'fs', '!' ); + $add_option->( 'format-skipping-begin', 'fsb', '=s' ); + $add_option->( 'format-skipping-end', 'fse', '=s' ); + $add_option->( 'hanging-side-comments', 'hsc', '!' ); + $add_option->( 'indent-block-comments', 'ibc', '!' ); + $add_option->( 'indent-spaced-block-comments', 'isbc', '!' ); + $add_option->( 'fixed-position-side-comment', 'fpsc', '=i' ); + $add_option->( 'minimum-space-to-comment', 'msc', '=i' ); + $add_option->( 'non-indenting-braces', 'nib', '!' ); + $add_option->( 'non-indenting-brace-prefix', 'nibp', '=s' ); + $add_option->( 'outdent-long-comments', 'olc', '!' ); + $add_option->( 'outdent-static-block-comments', 'osbc', '!' ); + $add_option->( 'static-block-comment-prefix', 'sbcp', '=s' ); + $add_option->( 'static-block-comments', 'sbc', '!' ); + $add_option->( 'static-side-comment-prefix', 'sscp', '=s' ); + $add_option->( 'static-side-comments', 'ssc', '!' ); + $add_option->( 'ignore-side-comment-lengths', 'iscl', '!' ); + $add_option->( 'ignore-perlcritic-side-comment-lengths', 'ipcl', '!' ); ######################################## $category = 5; # Linebreak controls @@ -3559,6 +3560,7 @@ sub generate_options { function-paren-vertical-alignment fuzzy-line-length hanging-side-comments + ignore-perlcritic-side-comment-lengths indent-block-comments indent-columns=4 iterations=1 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 19e63447..6b5a6922 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -209,6 +209,7 @@ my ( $rOpts_fuzzy_line_length, $rOpts_ignore_old_breakpoints, $rOpts_ignore_side_comment_lengths, + $rOpts_ignore_perlcritic_side_comment_lengths, $rOpts_indent_closing_brace, $rOpts_indent_columns, $rOpts_indent_only, @@ -2444,6 +2445,8 @@ sub initialize_global_option_vars { $rOpts_ignore_old_breakpoints = $rOpts->{'ignore-old-breakpoints'}; $rOpts_ignore_side_comment_lengths = $rOpts->{'ignore-side-comment-lengths'}; + $rOpts_ignore_perlcritic_side_comment_lengths = + $rOpts->{'ignore-perlcritic-side-comment-lengths'}; $rOpts_indent_closing_brace = $rOpts->{'indent-closing-brace'}; $rOpts_indent_columns = $rOpts->{'indent-columns'}; $rOpts_indent_only = $rOpts->{'indent-only'}; @@ -9184,12 +9187,43 @@ sub store_token { $item->[_TOKEN_] = $token; } - # Mark length of side comments as just 1 if sc lengths are ignored - if ( $rOpts_ignore_side_comment_lengths - && ( !$CODE_type || $CODE_type eq 'HSC' ) ) + my $ignore_sc_length = $rOpts_ignore_side_comment_lengths; + + # Ignore length of '## no critic' comments if requested + if ( $rOpts_ignore_perlcritic_side_comment_lengths + && $token_length > 10 + && substr( $token, 1, 1 ) eq '#' + && $token =~ /^##\s*no\s+critic\b/ ) { + + # Is it a side comment or a block comment? + if ( $Ktoken_vars > $Kfirst_old ) { + + # This is a side comment. If we do not ignore its length, and + # -iscl has not been set, then the line could be broken and + # perlcritic will complain. So this is essential: + $ignore_sc_length ||= 1; + + # It would be a good idea to also make this behave like a + # static side comment, but this is not essential and would + # change existing formatting. So we will leave it to the user + # to set -ssc if desired. + } + else { + + # This is a full-line (block) comment. + # It would be a good idea to make this behave like a static + # block comment, but this is not essential and would change + # existing formatting. So we will leave it to the user to + # set -sbc if desired + } + } + + # Set length of ignored side comments as just 1 + if ( $ignore_sc_length && ( !$CODE_type || $CODE_type eq 'HSC' ) ) { $token_length = 1; } + my $seqno = $seqno_stack{ $depth_next - 1 }; if ( defined($seqno) ) { $self->[_rblank_and_comment_count_]->{$seqno} += 1 diff --git a/t/snippets/comments.in b/t/snippets/comments.in index 261c4db8..0c34f161 100644 --- a/t/snippets/comments.in +++ b/t/snippets/comments.in @@ -65,6 +65,10 @@ sub macro_get_names { # 1, 4, 6, 4, 1,); #>> + local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + + ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow diff --git a/t/snippets/comments3.par b/t/snippets/comments3.par index a7e7943e..c3b787cb 100644 --- a/t/snippets/comments3.par +++ b/t/snippets/comments3.par @@ -1,2 +1,4 @@ -# testing --maximum-consecutive-blank-lines=2 and --indent-spaced-block-comments --no-format-skipping --mbl=2 -isbc -nfs +--maximum-consecutive-blank-lines=2 # -mbl=2 +--indent-spaced-block-comments # -isbc +--no-format-skipping # -nfs +--noignore-perlcritic-side-comment-lengths # -nipscl diff --git a/t/snippets/expect/comments.comments1 b/t/snippets/expect/comments.comments1 index aedc7d89..66b7fdfe 100644 --- a/t/snippets/expect/comments.comments1 +++ b/t/snippets/expect/comments.comments1 @@ -68,6 +68,10 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow =pod diff --git a/t/snippets/expect/comments.comments2 b/t/snippets/expect/comments.comments2 index cddf93a8..e401a53e 100644 --- a/t/snippets/expect/comments.comments2 +++ b/t/snippets/expect/comments.comments2 @@ -50,6 +50,8 @@ sub macro_get_names { # my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + __END__ diff --git a/t/snippets/expect/comments.comments3 b/t/snippets/expect/comments.comments3 index 732a5d49..15191c2e 100644 --- a/t/snippets/expect/comments.comments3 +++ b/t/snippets/expect/comments.comments3 @@ -68,6 +68,11 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = + $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow diff --git a/t/snippets/expect/comments.comments4 b/t/snippets/expect/comments.comments4 index f6139ec1..962adf74 100644 --- a/t/snippets/expect/comments.comments4 +++ b/t/snippets/expect/comments.comments4 @@ -74,6 +74,10 @@ sub macro_get_names { # 1, 4, 6, 4, 1,); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow diff --git a/t/snippets/expect/comments.comments5 b/t/snippets/expect/comments.comments5 index df9774d9..b7f05437 100644 --- a/t/snippets/expect/comments.comments5 +++ b/t/snippets/expect/comments.comments5 @@ -62,6 +62,10 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow =pod diff --git a/t/snippets/expect/comments.def b/t/snippets/expect/comments.def index 4f2440e0..be8db911 100644 --- a/t/snippets/expect/comments.def +++ b/t/snippets/expect/comments.def @@ -67,6 +67,10 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow =pod diff --git a/t/snippets17.t b/t/snippets17.t index 033da5f9..16ea5ce4 100644 --- a/t/snippets17.t +++ b/t/snippets17.t @@ -52,8 +52,10 @@ BEGIN { -msc=10 -dbc -dp ---------- 'comments3' => <<'----------', -# testing --maximum-consecutive-blank-lines=2 and --indent-spaced-block-comments --no-format-skipping --mbl=2 -isbc -nfs +--maximum-consecutive-blank-lines=2 # -mbl=2 +--indent-spaced-block-comments # -isbc +--no-format-skipping # -nfs +--noignore-perlcritic-side-comment-lengths # -nipscl ---------- 'comments4' => <<'----------', # testing --keep-old-blank-lines=2 [=all] and @@ -172,6 +174,10 @@ sub macro_get_names { # 1, 4, 6, 4, 1,); #>> + local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + + ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow @@ -446,6 +452,10 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow =pod @@ -522,6 +532,8 @@ sub macro_get_names { # my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + __END__ @@ -608,6 +620,11 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = + $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow @@ -711,6 +728,10 @@ sub macro_get_names { # 1, 4, 6, 4, 1,); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow @@ -809,6 +830,10 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow =pod diff --git a/t/snippets18.t b/t/snippets18.t index 2dec3547..6f532b05 100644 --- a/t/snippets18.t +++ b/t/snippets18.t @@ -184,6 +184,10 @@ sub macro_get_names { # 1, 4, 6, 4, 1,); #>> + local $Test::Builder::Level = $Test::Builder::Level + 1; ## no critic (Variables::ProhibitPackageVars) + + ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow @@ -430,6 +434,10 @@ my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, ); #>> +local $Test::Builder::Level = $Test::Builder::Level + 1; + +## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) + # some blank lines follow =pod