From a03c281dafeb30ed3b9f70533048eeb6b3a2e67a Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sun, 1 Dec 2024 09:34:55 -0800 Subject: [PATCH] add --indent-leading-semicolon, -ils; see git #171 --- CHANGES.md | 4 ++++ bin/perltidy | 29 ++++++++++++++++++++++++++++- lib/Perl/Tidy.pm | 2 ++ lib/Perl/Tidy/Formatter.pm | 12 ++++++++++++ t/snippets/expect/ils.def | 1 + t/snippets/expect/ils.ils | 2 ++ t/snippets/ils.in | 2 ++ t/snippets/ils.par | 1 + t/snippets/packing_list.txt | 6 ++++-- t/snippets31.t | 25 +++++++++++++++++++++++++ 10 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 t/snippets/expect/ils.def create mode 100644 t/snippets/expect/ils.ils create mode 100644 t/snippets/ils.in create mode 100644 t/snippets/ils.par diff --git a/CHANGES.md b/CHANGES.md index f0ac1b67..0ba126ce 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## 2024 09 03.06 + - Added parameter --indent-leading-semicolon, -ils; see git #171. When + this is negated, a line with a leading semicolon does not get the extra + leading continuation indentation spaces (defined with -ci=n). + - Space around here doc delimiters follow spacing controls better. For example, a space is now added before the closing paren here: diff --git a/bin/perltidy b/bin/perltidy index 3bd39c35..b69b1d07 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -1115,6 +1115,25 @@ terminates a code block . For example, The default is not to do this, indicated by B<-nicb>. +=item B<-ils>, B<--indent-leading-semicolon> + +A line which begins with a leading semicolon will, by default, have the extra +number of indentation spaces defined by B<--continuation-indentation=n>. +This extra indentation can be removed by setting B<-nils>. + + # default + $z = sqrt( $x**2 + $y**2 ) + + ; # <-- indented by ci spaces + + # -nils + $z = sqrt( $x**2 + $y**2 ) + + ; # <-- not indented by ci spaces + +Note that leading semicolons do not normally occur unless requested with +B<--break-at-old-semicolon-breakpoints> or forced, for example by +a blank line as in this example. =item B<-nib>, B<--non-indenting-braces> @@ -4680,11 +4699,19 @@ The default formatting will be: $z = sqrt( $x**2 + $y**2 ); -The result using B keeps the isolated semicolon: +Using the <-bos> flag keeps the isolated semicolon: + # perltidy -bos $z = sqrt( $x**2 + $y**2 ) ; +The extra continuation indentation spaces on the semicolon can be +removed by also setting B<--noindent-leading-semicolon>. + + # perltidy -bos -nils + $z = sqrt( $x**2 + $y**2 ) + ; + The default is not to do this, B<-nbos>. diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index e6ec263f..98492b96 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -3558,6 +3558,7 @@ sub generate_options { $add_option->( 'outdent-labels', 'ola', '!' ); $add_option->( 'outdent-long-quotes', 'olq', '!' ); $add_option->( 'indent-closing-brace', 'icb', '!' ); + $add_option->( 'indent-leading-semicolon', 'ils', '!' ); $add_option->( 'closing-token-indentation', 'cti', '=i' ); $add_option->( 'closing-paren-indentation', 'cpi', '=i' ); $add_option->( 'closing-brace-indentation', 'cbi', '=i' ); @@ -3929,6 +3930,7 @@ sub generate_options { hanging-side-comments indent-block-comments indent-columns=4 + indent-leading-semicolon integer-range-check=2 interbracket-arrow-complexity=1 iterations=1 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index cabaef23..42af4e3e 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -234,6 +234,7 @@ my ( $rOpts_ignore_perlcritic_comments, $rOpts_indent_closing_brace, $rOpts_indent_columns, + $rOpts_indent_leading_semicolon, $rOpts_indent_only, $rOpts_keep_interior_semicolons, $rOpts_line_up_parentheses, @@ -3336,6 +3337,7 @@ sub initialize_global_option_vars { $rOpts_ignore_perlcritic_comments = $rOpts->{'ignore-perlcritic-comments'}; $rOpts_indent_closing_brace = $rOpts->{'indent-closing-brace'}; $rOpts_indent_columns = $rOpts->{'indent-columns'}; + $rOpts_indent_leading_semicolon = $rOpts->{'indent-leading-semicolon'}; $rOpts_indent_only = $rOpts->{'indent-only'}; $rOpts_keep_interior_semicolons = $rOpts->{'keep-interior-semicolons'}; $rOpts_line_up_parentheses = $rOpts->{'line-up-parentheses'}; @@ -38876,6 +38878,16 @@ sub make_paren_name { ); } + #----------------------------------------- + # Section 1B: + # if line starts with a non-sequenced item + #----------------------------------------- + else { + if ( $type_beg eq ';' && !$rOpts_indent_leading_semicolon ) { + $adjust_indentation = 1; + } + } + #--------------------------------------------------------- # Section 2: set indentation according to flag set above # diff --git a/t/snippets/expect/ils.def b/t/snippets/expect/ils.def new file mode 100644 index 00000000..eacf48e7 --- /dev/null +++ b/t/snippets/expect/ils.def @@ -0,0 +1 @@ +$z = sqrt( $x**2 + $y**2 ); diff --git a/t/snippets/expect/ils.ils b/t/snippets/expect/ils.ils new file mode 100644 index 00000000..ed17984a --- /dev/null +++ b/t/snippets/expect/ils.ils @@ -0,0 +1,2 @@ +$z = sqrt( $x**2 + $y**2 ) +; diff --git a/t/snippets/ils.in b/t/snippets/ils.in new file mode 100644 index 00000000..ed17984a --- /dev/null +++ b/t/snippets/ils.in @@ -0,0 +1,2 @@ +$z = sqrt( $x**2 + $y**2 ) +; diff --git a/t/snippets/ils.par b/t/snippets/ils.par new file mode 100644 index 00000000..def94c99 --- /dev/null +++ b/t/snippets/ils.par @@ -0,0 +1 @@ +-nils -bos diff --git a/t/snippets/packing_list.txt b/t/snippets/packing_list.txt index 19d8c8fc..1dcd5e78 100644 --- a/t/snippets/packing_list.txt +++ b/t/snippets/packing_list.txt @@ -464,6 +464,8 @@ ../snippets31.t btct.btct2 ../snippets31.t btct.btct3 ../snippets31.t btct.def +../snippets31.t c424.c424 +../snippets31.t c424.def ../snippets4.t gnu1.gnu ../snippets4.t gnu2.def ../snippets4.t gnu2.gnu @@ -584,5 +586,5 @@ ../snippets9.t rt98902.def ../snippets9.t rt98902.rt98902 ../snippets9.t rt99961.def -../snippets31.t c424.c424 -../snippets31.t c424.def +../snippets31.t ils.def +../snippets31.t ils.ils diff --git a/t/snippets31.t b/t/snippets31.t index e4536826..e36c5b68 100644 --- a/t/snippets31.t +++ b/t/snippets31.t @@ -6,6 +6,8 @@ #3 btct.def #4 c424.c424 #5 c424.def +#6 ils.def +#7 ils.ils # To locate test #13 you can search for its name or the string '#13' @@ -27,6 +29,7 @@ BEGIN { 'btct3' => "-btct=1 -atc -wtc=1", 'c424' => "-naws -qwaf", 'def' => "", + 'ils' => "-nils -bos", }; ############################ @@ -53,6 +56,11 @@ $lut = byte [ [ 0, 0, 0 ], [ 10, 1, 10 ], [ 2, 20, 20 ], [ 30, 30, 3 ], ]; 'c424' => <<'----------', my @chars = qw( | / - \ | / - \ ); my @chars = qw(| / - \ | / - \ ); +---------- + + 'ils' => <<'----------', +$z = sqrt( $x**2 + $y**2 ) +; ---------- }; @@ -162,6 +170,23 @@ my @chars = qw( | / - \ | / - \ ); my @chars = qw(| / - \ | / - \ ); #5........... }, + + 'ils.def' => { + source => "ils", + params => "def", + expect => <<'#6...........', +$z = sqrt( $x**2 + $y**2 ); +#6........... + }, + + 'ils.ils' => { + source => "ils", + params => "ils", + expect => <<'#7...........', +$z = sqrt( $x**2 + $y**2 ) +; +#7........... + }, }; my $ntests = 0 + keys %{$rtests}; -- 2.39.5