master 7861c306427: cperl-mode.el: Support Perl 5.44: named parameters in signatures

Harald Jörg <[email protected]>
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit 7861c306427e7488a623d296e1efb05509cc7cc6
Author: Harald Jörg <[email protected]>
Commit: Harald Jörg <[email protected]>

    cperl-mode.el: Support Perl 5.44: named parameters in signatures
    
    * lisp/progmodes/cperl-mode.el (cperl--signature-rx): Allow for a
    colon before a variable name to declare a named parameter
    (cperl--sloppy-signature-rx): dito
    (cperl-init-faces): dito
    (introduction): Delete an obsolete comment
    
    * test/lisp/progmodes/cperl-mode-tests.el
    (cperl-test-signature-rx): Add a test case for a signature with
    named parameters
    (cperl-test-fontify-attrs-and-signatures): Verify correct
    fontification of a named parameter
    
    * test/lisp/progmodes/cperl-mode-resources/proto-and-attrs.pl: Add
    test code for named signatures
    
    * etc/NEWS: Announce Perl 5.44 syntax support
---
 etc/NEWS                                           |  8 ++++++++
 lisp/progmodes/cperl-mode.el                       | 22 ++++++++++++----------
 .../cperl-mode-resources/proto-and-attrs.pl        | 16 ++++++++++++++++
 test/lisp/progmodes/cperl-mode-tests.el            |  3 ++-
 4 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 43c667647e1..c0656510677 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -233,6 +233,14 @@ as well as the default nil and arbitrary user functions.
 It allows users to navigate the current buffer's outline using Xref.
 It is bound to 'M-o'.
 
+** CPerl mode
+
+*** Perl 5.44 support: Signatures with named parameters
+
+Perl 5.44 allows to declare a signature like "sub subname (:$par)", see
+https://perldoc.perl.org/5.44.0/perldelta#Named-Parameters-in-Signatures
+CPerl mode now recognizes this as a variable declaration
+
 ** Newsticker
 
 ---
diff --git a/lisp/progmodes/cperl-mode.el b/lisp/progmodes/cperl-mode.el
index 22d312d6be1..bc976e07f0e 100644
--- a/lisp/progmodes/cperl-mode.el
+++ b/lisp/progmodes/cperl-mode.el
@@ -51,9 +51,6 @@
 ;;
 ;;     (define-key global-map [M-S-down-mouse-3] #'imenu)
 
-;; This version supports the syntax added by the MooseX::Declare CPAN
-;; module, as well as Perl 5.10 keywords.
-
 ;;; Code:
 
 ;;; Compatibility with older versions (for publishing on ELPA)
@@ -1373,13 +1370,16 @@ prototypes from signatures.")
                (optional
                 (sequence
                  (0+ (sequence ,cperl--ws*-rx
-                               (or ,cperl--basic-scalar-rx "$")
+                               (or (sequence (optional ":")
+                                             ,cperl--basic-scalar-rx)
+                                   "$")
                                ,cperl--ws*-rx
                                ","))
                  ,cperl--ws*-rx
-                 (or ,cperl--basic-scalar-rx
-                     ,cperl--basic-array-rx
-                     ,cperl--basic-hash-rx
+                 (or (sequence (optional ":")
+                               (or ,cperl--basic-scalar-rx
+                                   ,cperl--basic-array-rx
+                                   ,cperl--basic-hash-rx))
                      "$" "%" "@")))
                (optional (sequence ,cperl--ws*-rx) "," )
                ,cperl--ws*-rx
@@ -1392,9 +1392,10 @@ place.")
   (defconst cperl--sloppy-signature-rx
     `(sequence "("
                ,cperl--ws*-rx
-               (or ,cperl--basic-scalar-rx
-                   ,cperl--basic-array-rx
-                   ,cperl--basic-hash-rx)
+               (sequence (optional ":")
+                         (or ,cperl--basic-scalar-rx
+                             ,cperl--basic-array-rx
+                             ,cperl--basic-hash-rx))
                ,cperl--ws*-rx
                (or "," "=" "||=" "//=" ")"))
     "A rx sequence for the begin of a signature with initializers.
@@ -6385,6 +6386,7 @@ functions (which they are not).  Inherits from `default'.")
                   ;; -------- anchored: Signature
                   `(,(rx (sequence (in "(,")
                                    (eval cperl--ws*-rx)
+                                   (optional ":")
                                    (group (eval cperl--basic-variable-rx))))
                     (progn
                       (goto-char (match-beginning 2)) ; pre-match: Back to sig
diff --git a/test/lisp/progmodes/cperl-mode-resources/proto-and-attrs.pl b/test/lisp/progmodes/cperl-mode-resources/proto-and-attrs.pl
index d95b3d0a453..3c022d973e6 100644
--- a/test/lisp/progmodes/cperl-mode-resources/proto-and-attrs.pl
+++ b/test/lisp/progmodes/cperl-mode-resources/proto-and-attrs.pl
@@ -12,6 +12,14 @@ no warnings 'experimental::signatures';
 # are somewhat frowned upon most of the times, but they are required
 # for some Perl magic
 
+# The tests make assumptions about variable names like this:
+# + Subroutine names are "sub_" followed by a number and checked for
+#   font-lock-function-name-face
+# + Prototype content, if present, is fontified as font-lock-string-face
+# + Subroutine attributes, if present, are fontified as
+#   font-lock-constant-face, their paramters as font-lock-string-face
+# + A variable named "$bar" is tested for font-lock-variable-name-face
+
 # Part 1: Named subroutines
 # A plain named subroutine without any optional stuff
 sub sub_0 { ...; }
@@ -48,6 +56,14 @@ sub sub_7
 {
 }
 
+# Named parameters (since Perl 5.44): A trivial one
+sub sub_8 (:$bar) { ...; }
+
+# Named parameters (since Perl 5.44): Sloppy, both positional and named
+sub sub_9 ($version //=5.044, :$bar)
+{
+}
+
 
 # Part 2: Same constructs for anonymous subs
 # A plain named subroutine without any optional stuff
diff --git a/test/lisp/progmodes/cperl-mode-tests.el b/test/lisp/progmodes/cperl-mode-tests.el
index ffb79c6e5a2..cbf43f01742 100644
--- a/test/lisp/progmodes/cperl-mode-tests.el
+++ b/test/lisp/progmodes/cperl-mode-tests.el
@@ -633,7 +633,8 @@ Also includes valid cases with whitespace in strange places."
    "Test subroutine signatures."
    (skip-unless (eq cperl-test-mode #'cperl-mode))
    (let ((valid
-          '("()" "( )" "($self, %params)" "(@params)" "($first,$)"))
+          '("()" "( )" "($self, %params)" "(@params)" "($first,$)"
+            "($pos,:$named)"))
         (invalid
          '("$self"               ; missing paren
            "($!)"                ; globals not permitted in a signature
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.