From 9764478dba87e81a0bde9b16677a9c2c2c73089c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 14 May 2024 18:13:06 +0200 Subject: [PATCH 1/2] Add tests for highlighting PHP attributes --- tests/IntegrationTest.php | 4 + .../blocks/code-blocks/php-attributes.html | 147 ++++++++++++++++++ .../blocks/code-blocks/php-attributes.rst | 51 ++++++ 3 files changed, 202 insertions(+) create mode 100644 tests/fixtures/expected/blocks/code-blocks/php-attributes.html create mode 100644 tests/fixtures/source/blocks/code-blocks/php-attributes.rst diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index cf34cde..69b3681 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -290,6 +290,10 @@ public function parserUnitBlockProvider() 'blockName' => 'code-blocks/php-annotations', ]; + yield 'code-block-php-attributes' => [ + 'blockName' => 'code-blocks/php-attributes', + ]; + yield 'code-block-text' => [ 'blockName' => 'code-blocks/text', ]; diff --git a/tests/fixtures/expected/blocks/code-blocks/php-attributes.html b/tests/fixtures/expected/blocks/code-blocks/php-attributes.html new file mode 100644 index 0000000..5b73756 --- /dev/null +++ b/tests/fixtures/expected/blocks/code-blocks/php-attributes.html @@ -0,0 +1,147 @@ +
+
+
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+
+            
+                // src/SomePath/SomeClass.php
+namespace App\SomePath;
+                useSymfony\Component\Validator\Constraints as Assert;
+                
+                    class
+                    SomeClass
+                
+                {
+                #[AttributeName]
+                private
+                
+                    $ property1
+                ;
+                #[AttributeName(
+                )]
+                private
+                
+                    $ property2
+                ;
+                #[AttributeName(
+                'value'
+                )]
+                private
+                
+                    $ property3
+                ;
+                #[AttributeName(
+                'value'
+                , option:
+                'value'
+                )]
+                private
+                
+                    $ property4
+                ;
+                #[AttributeName(
+['value' => 'value'])]
+                private
+                
+                    $ property5
+                ;
+                #[AttributeName(
+                'value'
+                , option:
+                'value'
+                )]
+                private
+                
+                    $ property6
+                ;
+                #[Assert\AttributeName(
+                'value'
+                )]
+                private
+                
+                    $ property7
+                ;
+                #[Assert\AttributeName(
+                'value'
+                , option:
+                'value'
+                )]
+                private
+                
+                    $ property8
+                ;
+                #[Route(
+                '/blog/{page<\d+>}'
+                , name:
+                'blog_list'
+                )]
+                private
+                
+                $ property9
+                ;
+                #[Assert\GreaterThanOrEqual(
+                value:
+                18
+                ,
+                )]
+                private
+                
+                $ property10
+                ;
+                #[ORM\CustomIdGenerator(
+                class:
+                'doctrine.uuid_generator'
+                )]
+                private
+                
+                $ property11
+                ;
+}
+
+
diff --git a/tests/fixtures/source/blocks/code-blocks/php-attributes.rst b/tests/fixtures/source/blocks/code-blocks/php-attributes.rst new file mode 100644 index 0000000..d43f8ad --- /dev/null +++ b/tests/fixtures/source/blocks/code-blocks/php-attributes.rst @@ -0,0 +1,51 @@ +.. code-block:: php-attributes + + // src/SomePath/SomeClass.php + namespace App\SomePath; + + use Symfony\Component\Validator\Constraints as Assert; + + class SomeClass + { + #[AttributeName] + private $property1; + + #[AttributeName()] + private $property2; + + #[AttributeName('value')] + private $property3; + + #[AttributeName('value', option: 'value')] + private $property4; + + #[AttributeName(['value' => 'value'])] + private $property5; + + #[AttributeName( + 'value', + option: 'value' + )] + private $property6; + + #[Assert\AttributeName('value')] + private $property7; + + #[Assert\AttributeName( + 'value', + option: 'value' + )] + private $property8; + + #[Route('/blog/{page<\d+>}', name: 'blog_list')] + private $property9; + + #[Assert\GreaterThanOrEqual( + value: 18, + )] + private $property10; + + #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')] + private $property11; + } + From 020a28109d304ee7f34ba65eb1ed6658dda54eba Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Fri, 17 May 2024 21:43:04 +0200 Subject: [PATCH 2/2] 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 +- 3 files changed, 73 insertions(+), 59 deletions(-) diff --git a/src/Templates/highlight.php/php.json b/src/Templates/highlight.php/php.json index 50b53fe..3f127b8 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 5b73756..541681c 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 d43f8ad..31b5f99 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; + }