From 4c325d974492f4038041eb7dd61f0ae03dbca2ad Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Fri, 17 May 2024 21:43:04 +0200 Subject: [PATCH] Add support for PHP attributes --- src/Templates/highlight.php/php.json | 50 ++++++++----- .../blocks/code-blocks/php-attributes.html | 75 +++++++++---------- .../blocks/code-blocks/php-attributes.rst | 7 +- .../source/blocks/code-blocks/text.rst | 2 + 4 files changed, 75 insertions(+), 59 deletions(-) diff --git a/src/Templates/highlight.php/php.json b/src/Templates/highlight.php/php.json index 50b53fe7..3f127b81 100644 --- a/src/Templates/highlight.php/php.json +++ b/src/Templates/highlight.php/php.json @@ -8,8 +8,23 @@ "php7" ], "case_insensitive": true, - "keywords": "and include_once list abstract global private echo interface as static endswitch array null if endwhile or const for endforeach self var while isset public protected exit foreach throw elseif include __FILE__ empty require_once do xor return parent clone use __CLASS__ __LINE__ else break print eval new catch __METHOD__ case exception default die require __FUNCTION__ enddeclare final try switch continue endfor endif declare unset true false trait goto instanceof insteadof __DIR__ __NAMESPACE__ yield finally", + "keywords": "and include_once list abstract global private echo interface as static endswitch array null if endwhile or const for endforeach self var while isset public protected exit foreach throw elseif include __FILE__ empty require_once do xor return parent clone use __CLASS__ __LINE__ else break print eval new catch __METHOD__ case exception default die require __FUNCTION__ enddeclare final try match switch continue endfor endif declare unset true false goto instanceof insteadof __DIR__ __NAMESPACE__ yield finally", "contains": [ + { + "className": "php-attribute", + "begin": "#\\[\\s*(\\\\?[A-Z][A-Za-z0-9_\\x7f-\\xff]+|\\\\?[A-Z]+(?=[A-Z][a-z0-9_\\x7f-\\xff])){1,}(?![A-Za-z0-9])(?![$])", + "end": "]", + "contains": [ + { + "begin": "\\(", + "end": "\\)", + "keywords": "true false null new array", + "contains": { + "$ref": "#contains.10.contains.1.contains" + } + } + ] + }, { "className": "comment", "begin": "#", @@ -35,7 +50,7 @@ "begin": "<\\?(php)?|\\?>" }, { - "$ref": "#contains.0.contains.0" + "$ref": "#contains.1.contains.0" }, { "className": "doctag", @@ -54,7 +69,7 @@ "begin": "@[A-Za-z]+" }, { - "$ref": "#contains.0.contains.0" + "$ref": "#contains.1.contains.0" }, { "className": "doctag", @@ -69,7 +84,7 @@ "end": false, "contains": [ { - "$ref": "#contains.0.contains.0" + "$ref": "#contains.1.contains.0" }, { "className": "doctag", @@ -105,7 +120,7 @@ ] }, { - "$ref": "#contains.1.contains.0" + "$ref": "#contains.2.contains.0" }, { "className": "variable", @@ -137,10 +152,11 @@ "className": "params", "begin": "\\(", "end": "\\)", + "keywords": "true false null new array", "contains": [ "self", { - "$ref": "#contains.7" + "$ref": "#contains.8" }, { "className": "comment", @@ -148,7 +164,7 @@ "end": "\\*/", "contains": [ { - "$ref": "#contains.0.contains.0" + "$ref": "#contains.1.contains.0" }, { "className": "doctag", @@ -161,10 +177,10 @@ "className": "string", "contains": [ { - "$ref": "#contains.4.contains.0" + "$ref": "#contains.5.contains.0" }, { - "$ref": "#contains.1.contains.0" + "$ref": "#contains.2.contains.0" } ], "variants": [ @@ -183,7 +199,7 @@ "illegal": null, "contains": [ { - "$ref": "#contains.4.contains.0" + "$ref": "#contains.5.contains.0" } ] }, @@ -194,7 +210,7 @@ "illegal": null, "contains": [ { - "$ref": "#contains.4.contains.0" + "$ref": "#contains.5.contains.0" }, { "className": "subst", @@ -230,7 +246,7 @@ }, { "className": "class", - "beginKeywords": "class interface", + "beginKeywords": "class interface trait enum", "end": "{", "excludeEnd": true, "illegal": "[:\\(\\$\"]", @@ -239,7 +255,7 @@ "beginKeywords": "extends implements" }, { - "$ref": "#contains.9.contains.0" + "$ref": "#contains.10.contains.0" } ] }, @@ -249,7 +265,7 @@ "illegal": "[\\.']", "contains": [ { - "$ref": "#contains.9.contains.0" + "$ref": "#contains.10.contains.0" } ] }, @@ -258,7 +274,7 @@ "end": ";", "contains": [ { - "$ref": "#contains.9.contains.0" + "$ref": "#contains.10.contains.0" } ] }, @@ -266,10 +282,10 @@ "begin": "=>" }, { - "$ref": "#contains.9.contains.1.contains.3" + "$ref": "#contains.10.contains.1.contains.3" }, { - "$ref": "#contains.9.contains.1.contains.4" + "$ref": "#contains.10.contains.1.contains.4" } ] } diff --git a/tests/fixtures/expected/blocks/code-blocks/php-attributes.html b/tests/fixtures/expected/blocks/code-blocks/php-attributes.html index 5b73756c..541681cd 100644 --- a/tests/fixtures/expected/blocks/code-blocks/php-attributes.html +++ b/tests/fixtures/expected/blocks/code-blocks/php-attributes.html @@ -1,4 +1,4 @@ -
+
1
 2
@@ -47,7 +47,13 @@
 45
 46
 47
-48
+48 +49 +50 +51 +52 +53 +54
             
                 // src/SomePath/SomeClass.php
@@ -63,85 +69,72 @@
                 
                     $ property1
                 ;
-                #[AttributeName(
-                )]
+                #[AttributeName()]
                 private
                 
                     $ property2
                 ;
-                #[AttributeName(
-                'value'
-                )]
+                #[AttributeName('value')]
                 private
                 
                     $ property3
                 ;
-                #[AttributeName(
-                'value'
-                , option:
-                'value'
-                )]
+                #[AttributeName('value', option: 'value')]
                 private
                 
                     $ property4
                 ;
-                #[AttributeName(
-['value' => 'value'])]
+                #[AttributeName(['value' => 'value'])]
                 private
                 
                     $ property5
                 ;
-                #[AttributeName(
-                'value'
-                , option:
-                'value'
-                )]
+                #[AttributeName(
+                    'value',
+                    option: 'value'
+                )]
                 private
                 
                     $ property6
                 ;
-                #[Assert\AttributeName(
-                'value'
-                )]
+                #[Assert\AttributeName('value')]
                 private
                 
                     $ property7
                 ;
-                #[Assert\AttributeName(
-                'value'
-                , option:
-                'value'
-                )]
+                #[Assert\AttributeName(
+                    'value',
+                    option: 'value'
+                )]
                 private
                 
                     $ property8
                 ;
-                #[Route(
-                '/blog/{page<\d+>}'
-                , name:
-                'blog_list'
-                )]
+                #[Route('/blog/{page<\d+>}', name: 'blog_list')]
                 private
                 
                 $ property9
                 ;
-                #[Assert\GreaterThanOrEqual(
-                value:
-                18
-                ,
-                )]
+                #[Assert\GreaterThanOrEqual(
+                    value: 18,
+                )]
                 private
                 
                 $ property10
                 ;
-                #[ORM\CustomIdGenerator(
-                class:
-                'doctrine.uuid_generator'
-                )]
+                #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
                 private
                 
                 $ property11
                 ;
+                #[Assert\AtLeastOneOf([
+                    new Assert\Regex('/#/'),
+                    new Assert\Length(min: 10),
+                ])]
+                private
+                
+                $ property12
+                ;
 }
diff --git a/tests/fixtures/source/blocks/code-blocks/php-attributes.rst b/tests/fixtures/source/blocks/code-blocks/php-attributes.rst index d43f8ad3..31b5f998 100644 --- a/tests/fixtures/source/blocks/code-blocks/php-attributes.rst +++ b/tests/fixtures/source/blocks/code-blocks/php-attributes.rst @@ -47,5 +47,10 @@ #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')] private $property11; - } + #[Assert\AtLeastOneOf([ + new Assert\Regex('/#/'), + new Assert\Length(min: 10), + ])] + private $property12; + } diff --git a/tests/fixtures/source/blocks/code-blocks/text.rst b/tests/fixtures/source/blocks/code-blocks/text.rst index 50774bb1..59f2c11e 100644 --- a/tests/fixtures/source/blocks/code-blocks/text.rst +++ b/tests/fixtures/source/blocks/code-blocks/text.rst @@ -1,3 +1,5 @@ .. code-block:: text some text with special chars < > " & and some text with special chars already escaped < > " & + + .. image:: image.png