From 18be2c73629552330483b9b05b4ad81f65590829 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Fri, 19 Apr 2024 14:48:14 +0200 Subject: [PATCH] - Bugfix for arrays within attributes --- UPGRADING.md | 4 +++ .../PhpAttributeInstanceInjection.php | 30 +++++++++++++++++++ ...ion.php => PhpAttributePlainInjection.php} | 4 +-- src/Languages/Php/PhpLanguage.php | 6 ++-- tests/Languages/Php/PhpLanguageTest.php | 3 ++ 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/Languages/Php/Injections/PhpAttributeInstanceInjection.php rename src/Languages/Php/Injections/{PhpAttributeInjection.php => PhpAttributePlainInjection.php} (85%) diff --git a/UPGRADING.md b/UPGRADING.md index 21a24f1..badc945 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,3 +1,7 @@ +## 2.0.1 + +- Bugfix for arrays within attributes + ## 2.0.0 - `Language` interface has two new methods: `getName` and `getAliases` diff --git a/src/Languages/Php/Injections/PhpAttributeInstanceInjection.php b/src/Languages/Php/Injections/PhpAttributeInstanceInjection.php new file mode 100644 index 0000000..98373e5 --- /dev/null +++ b/src/Languages/Php/Injections/PhpAttributeInstanceInjection.php @@ -0,0 +1,30 @@ +\#\[(.|\n)*?\)\])'; + } + + public function parseContent(string $content, Highlighter $highlighter): string + { + $theme = $highlighter->getTheme(); + + return Escape::tokens($theme->before(TokenTypeEnum::ATTRIBUTE)) + . $content + . Escape::tokens($theme->after(TokenTypeEnum::ATTRIBUTE)); + } +} diff --git a/src/Languages/Php/Injections/PhpAttributeInjection.php b/src/Languages/Php/Injections/PhpAttributePlainInjection.php similarity index 85% rename from src/Languages/Php/Injections/PhpAttributeInjection.php rename to src/Languages/Php/Injections/PhpAttributePlainInjection.php index a073f89..794b26a 100644 --- a/src/Languages/Php/Injections/PhpAttributeInjection.php +++ b/src/Languages/Php/Injections/PhpAttributePlainInjection.php @@ -10,13 +10,13 @@ use Tempest\Highlight\IsInjection; use Tempest\Highlight\Tokens\TokenTypeEnum; -final readonly class PhpAttributeInjection implements Injection +final readonly class PhpAttributePlainInjection implements Injection { use IsInjection; public function getPattern(): string { - return '(?\#\[(.|\n)*?\])'; + return '(?\#\[[\w]+\])'; } public function parseContent(string $content, Highlighter $highlighter): string diff --git a/src/Languages/Php/PhpLanguage.php b/src/Languages/Php/PhpLanguage.php index 6b48f33..6f9bb4e 100644 --- a/src/Languages/Php/PhpLanguage.php +++ b/src/Languages/Php/PhpLanguage.php @@ -5,7 +5,8 @@ namespace Tempest\Highlight\Languages\Php; use Tempest\Highlight\Languages\Base\BaseLanguage; -use Tempest\Highlight\Languages\Php\Injections\PhpAttributeInjection; +use Tempest\Highlight\Languages\Php\Injections\PhpAttributeInstanceInjection; +use Tempest\Highlight\Languages\Php\Injections\PhpAttributePlainInjection; use Tempest\Highlight\Languages\Php\Injections\PhpDocCommentInjection; use Tempest\Highlight\Languages\Php\Injections\PhpFunctionParametersInjection; use Tempest\Highlight\Languages\Php\Injections\PhpHeredocInjection; @@ -61,7 +62,8 @@ public function getInjections(): array ...parent::getInjections(), new PhpHeredocInjection(), new PhpDocCommentInjection(), - new PhpAttributeInjection(), + new PhpAttributeInstanceInjection(), + new PhpAttributePlainInjection(), new PhpFunctionParametersInjection(), ]; } diff --git a/tests/Languages/Php/PhpLanguageTest.php b/tests/Languages/Php/PhpLanguageTest.php index b0bf4e0..43c10f5 100644 --- a/tests/Languages/Php/PhpLanguageTest.php +++ b/tests/Languages/Php/PhpLanguageTest.php @@ -57,6 +57,9 @@ public static function data(): array ['/** @var Foo $var */', '/** @var Foo $var */'], ['{~}): Foo {}~}', '}): Foo {}'], ['{~class~} Foo {}', 'class Foo {}'], + ['#[ConsoleCommand()]', '#[ConsoleCommand()]'], + ['#[ConsoleCommand]', '#[ConsoleCommand]'], + ['#[ConsoleCommand(foo: [])]', '#[ConsoleCommand(foo: [])]'], [ 'public string $fullName { get => $this->first . " " . $this->last;