From: Steve Hancock Date: Sat, 21 Nov 2020 00:41:01 +0000 (-0800) Subject: extend capability of the -wnxl flag X-Git-Tag: 20201202~24 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=8e528e3438605784cf3f3930369aaeaa70c881b1;p=perltidy.git extend capability of the -wnxl flag --- diff --git a/bin/perltidy b/bin/perltidy index 821e4507..0622a563 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -2445,30 +2445,30 @@ any other container stacking flags. This is because any welding is done first. =item B<-wnxl=s>, B<--weld-nested-exclusion-list> The B<-wnxl=s> flag provides some control over the types of containers which -can be welded. The B<-wn> flag by default is "greedy" in welding -adjacent containers. If it welds more types of containers than desired, -this flag can reduce the amount of welding by specifying a list of -things which should B be welded. +can be welded. The B<-wn> flag by default is "greedy" in welding adjacent +containers. If it welds more types of containers than desired, this flag +provides a capability to reduce the amount of welding by specifying a list +of things which should B be welded. -The logic in perltidy to apply this is straightforward. As each token is being -considered for joining a weld, any exclusion rules are consulted and used to -reject the weld if necessary. +The logic in perltidy to apply this is straightforward. As each container +token is being considered for joining a weld, any exclusion rules are consulted +and used to reject the weld if necessary. This list is a string with space-separated items. Each item consists of up to three pieces of information: (1) an optional positiion, (2) an optional preceding type, and (3) a container type. -The required piece of information, a container type, is one of '(', '[', '{' or -'q'. The first three of these are container tokens and the last represents a -quoted list. For example the string +The only required piece of information is a container type, which is one of +'(', '[', '{' or 'q'. The first three of these are container tokens and the +last represents a quoted list. For example the string -wnxl='[ { q' means do B include square-bracets, braces, or quotes in any welds. The only unspecified container is '(', so this string means that only welds involving parens will be made. -To illustrate this terminology, following welded snippet consists of -a chain of three welded containers with types '(' '[' and 'q': +To illustrate, following welded snippet consists of a chain of three welded +containers with types '(' '[' and 'q': # perltidy -wn skip_symbols( [ qw( @@ -2487,20 +2487,26 @@ indicator which is either '^', to indicate the first token of a welded sequence, or '.', to indicate an interior token of a welded sequence. (Since a quoted string 'q' always ends a chain it does need a position indicator). -If we do not want a sequence of welded containers to start with a square -bracket we could use +For example, if we do not want a sequence of welded containers to start with a +square bracket we could use -wnxl='^[' In the above snippet, there is a square bracket but it does not start the chain, -so the formatting would be unchanged with this restriction. +so the formatting would be unchanged if it were formatted with this restriction. A third optional item of information which can be given is an alphanumeric -letter which limits the selection depending on the type of token immediately -before the container. If given, it goes just before the container symbol. -There are, at present, just two possible letters: 'k' matches if the previous -token is a perl builtin keyword (such as 'if', 'while'), and 'K' matches if the -previous token is not a keyword. +letter which is used to limit the selection further depending on the type of +token immediately before the container. If given, it goes just before the +container symbol. The possible letters are currently 'k', 'K', 'f', 'F', +'w', and 'W', with these meanings: + + 'k' matches if the previous nonblank token is a perl builtin keyword (such as 'if', 'while'), + 'K' matches if 'k' does not, meaning that the previous token is not a keyword. + 'f' matches if the previous token is a function other than a keyword. + 'F' matches if 'f' does not. + 'w' matches if either 'k' or 'f' match. + 'W' matches if 'w' does not. For example, compare @@ -2519,7 +2525,7 @@ with The first case does maximum welding. In the second case the leading paren is retained by the rule (it would have been rejected if preceded by a non-keyword) -but the curly brace is rejected. +but the curly brace is rejected by the rule. Here are some additional example strings and their meanings: diff --git a/docs/BugLog.html b/docs/BugLog.html index 187a4585..bc184552 100644 --- a/docs/BugLog.html +++ b/docs/BugLog.html @@ -21,6 +21,42 @@
+
fixed problem with vertical alignments involving 'if' statements
+
+ +

An update was made to break vertical alignment when a new sequence of if-like statements or ternary statements is encountered. This situation was causing a loss of alignment in some cases. For example

+ +
  OLD:
+    $m1 = 0;
+    if ( $value =~ /\bset\b/i )      { $m0 = 1; }
+    if ( $value =~ /\barithmetic/i ) { $m1 = 1; }
+    if    ( $m0 && !$m1 ) { $CONFIG[1] = 0; }
+    elsif ( !$m0 && $m1 ) { $CONFIG[1] = 1; }
+    else                  { $ok        = 0; last; }
+
+ NEW:
+    $m1 = 0;
+    if    ( $value =~ /\bset\b/i )      { $m0        = 1; }
+    if    ( $value =~ /\barithmetic/i ) { $m1        = 1; }
+    if    ( $m0 && !$m1 )               { $CONFIG[1] = 0; }
+    elsif ( !$m0 && $m1 )               { $CONFIG[1] = 1; }
+    else                                { $ok        = 0; last; }
+ +

This update was made 15 Nov 2020.

+ +
+
added option -wnxl=s to give control of welding by the -wn parameter
+
+ +

The parameter string can restrict the types of containers which are welded. This was added 11 Nov 2020 in 'added -wnxl=s for control of -wn', 2e642d2.

+ +
+
merged pull request git #46
+
+ +

The man page gave the incorrect string for -fse. This was fixed 11 Nov 2020 in 1f9869e.

+ +
recognize overloaded RPerl operators to avoid error messages
diff --git a/docs/ChangeLog.html b/docs/ChangeLog.html index d03a0267..57847dc1 100644 --- a/docs/ChangeLog.html +++ b/docs/ChangeLog.html @@ -61,7 +61,7 @@ - Added 'state' as a keyword. -- This version is about 15% faster than previous versions due to some optimizations +- This version is about 20% faster than the previous version due to optimizations made with the help of Devel::NYTProf. - Line breaks are now automatically placed after 'use overload' to diff --git a/docs/perltidy.html b/docs/perltidy.html index 299e0020..edcbeda8 100644 --- a/docs/perltidy.html +++ b/docs/perltidy.html @@ -1945,19 +1945,19 @@
-wnxl=s, --weld-nested-exclusion-list
-

The -wnxl=s flag provides some control over the types of containers which can be welded. The -wn flag by default is "greedy" in welding adjacent containers. If it welds more types of containers than desired, this flag can reduce the amount of welding by specifying a list of things which should not be welded.

+

The -wnxl=s flag provides some control over the types of containers which can be welded. The -wn flag by default is "greedy" in welding adjacent containers. If it welds more types of containers than desired, this flag provides a capability to reduce the amount of welding by specifying a list of things which should not be welded.

-

The logic in perltidy to apply this is straightforward. As each token is being considered for joining a weld, any exclusion rules are consulted and used to reject the weld if necessary.

+

The logic in perltidy to apply this is straightforward. As each container token is being considered for joining a weld, any exclusion rules are consulted and used to reject the weld if necessary.

This list is a string with space-separated items. Each item consists of up to three pieces of information: (1) an optional positiion, (2) an optional preceding type, and (3) a container type.

-

The required piece of information, a container type, is one of '(', '[', '{' or 'q'. The first three of these are container tokens and the last represents a quoted list. For example the string

+

The only required piece of information is a container type, which is one of '(', '[', '{' or 'q'. The first three of these are container tokens and the last represents a quoted list. For example the string

  -wnxl='[ { q'

means do NOT include square-bracets, braces, or quotes in any welds. The only unspecified container is '(', so this string means that only welds involving parens will be made.

-

To illustrate this terminology, following welded snippet consists of a chain of three welded containers with types '(' '[' and 'q':

+

To illustrate, following welded snippet consists of a chain of three welded containers with types '(' '[' and 'q':

    # perltidy -wn
     skip_symbols( [ qw(
@@ -1971,13 +1971,20 @@
 
 

Any of the container types '[', '{', and '(' may be prefixed with a position indicator which is either '^', to indicate the first token of a welded sequence, or '.', to indicate an interior token of a welded sequence. (Since a quoted string 'q' always ends a chain it does need a position indicator).

-

If we do not want a sequence of welded containers to start with a square bracket we could use

+

For example, if we do not want a sequence of welded containers to start with a square bracket we could use

  -wnxl='^['
-

In the above snippet, there is a square bracket but it does not start the chain, so the formatting would be unchanged with this restriction.

+

In the above snippet, there is a square bracket but it does not start the chain, so the formatting would be unchanged if it were formatted with this restriction.

-

A third optional item of information which can be given is an alphanumeric letter which limits the selection depending on the type of token immediately before the container. If given, it goes just before the container symbol. There are, at present, just two possible letters: 'k' matches if the previous token is a perl builtin keyword (such as 'if', 'while'), and 'K' matches if the previous token is not a keyword.

+

A third optional item of information which can be given is an alphanumeric letter which is used to limit the selection further depending on the type of token immediately before the container. If given, it goes just before the container symbol. The possible letters are currently 'k', 'K', 'f', 'F', 'w', and 'W', with these meanings:

+ +
 'k' matches if the previous nonblank token is a perl builtin keyword (such as 'if', 'while'), 
+ 'K' matches if 'k' does not, meaning that the previous token is not a keyword.
+ 'f' matches if the previous token is a function other than a keyword.
+ 'F' matches if 'f' does not.
+ 'w' matches if either 'k' or 'f' match.
+ 'W' matches if 'w' does not.

For example, compare

@@ -1994,7 +2001,7 @@ {'username'} } ) )
-

The first case does maximum welding. In the second case the leading paren is retained by the rule (it would have been rejected if preceded by a non-keyword) but the curly brace is rejected.

+

The first case does maximum welding. In the second case the leading paren is retained by the rule (it would have been rejected if preceded by a non-keyword) but the curly brace is rejected by the rule.

Here are some additional example strings and their meanings:

diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index d66fd1f3..34173067 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -1535,7 +1535,7 @@ sub initialize_weld_nested_exclusion_rules { # with spaces separating any number of items. Each item consists of three # pieces of information: # - # < ^ or . > < k or K > < ( [ { > + # < ^ or . > <[k K f F w W]> < ( [ { > # The last character is the required container type and must be one of: # ( = paren @@ -1551,6 +1551,10 @@ sub initialize_weld_nested_exclusion_rules { # token selects to which the rule applies: # k = any keyword # K = any non-keyword + # f = function + # F = not a function call + # w = function or keyword + # W = not a function or keyword # no letter means any preceding type matches # Examples: @@ -1587,7 +1591,7 @@ sub initialize_weld_nested_exclusion_rules { my $pos = '*'; my $select = '*'; if ($item) { - if ( $item =~ /^([\^\.])?([kK])?$/ ) { + if ( $item =~ /^([\^\.])?([kKfFwW])?$/ ) { $pos = $1 if ($1); $select = $2 if ($2); } @@ -6325,14 +6329,35 @@ sub is_excluded_weld { my $flag = $is_leading ? $rflags->[0] : $rflags->[1]; return 0 unless ( defined($flag) ); return 1 if $flag eq '*'; - my $Kp = $self->K_previous_nonblank($KK); - my $type_p = 'b'; - if ( defined($Kp) ) { $type_p = $rLL->[$Kp]->[_TYPE_] } - if ( $flag eq 'k' && $type_p eq 'k' || $flag eq 'K' && $type_p ne 'k' ) { - return 1; - } - return 0; + my ( $is_f, $is_k, $is_w ); + my $Kp = $self->K_previous_nonblank($KK); + if ( defined($Kp) ) { + my $type_p = $rLL->[$Kp]->[_TYPE_]; + my $token_p = $rLL->[$Kp]->[_TOKEN_]; + + # keyword? + $is_k = $type_p eq 'k'; + + # function call? Use the same definition as used for + # the parameter 'space-function-paren' + $is_f = + $type_p =~ /^[wUG]$/ + || $type_p eq '->' + || $type_p =~ /^[wi]$/ && $token_p =~ /^(\&|->)/; + + # either keyword or function call? + $is_w = $is_k || $is_f; + } + + my $match; + if ( $flag eq 'k' ) { $match = $is_k } + elsif ( $flag eq 'K' ) { $match = !$is_k } + elsif ( $flag eq 'f' ) { $match = $is_f } + elsif ( $flag eq 'F' ) { $match = !$is_f } + elsif ( $flag eq 'w' ) { $match = $is_w } + elsif ( $flag eq 'W' ) { $match = !$is_w } + return $match; } sub weld_nested_containers {