Skip to content

Commit

Permalink
- Bugfix for arrays within attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 19, 2024
1 parent 93d40fa commit 18be2c7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
4 changes: 4 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

- Bugfix for arrays within attributes

## 2.0.0

- `Language` interface has two new methods: `getName` and `getAliases`
Expand Down
30 changes: 30 additions & 0 deletions src/Languages/Php/Injections/PhpAttributeInstanceInjection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Php\Injections;

use Tempest\Highlight\Escape;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Injection;
use Tempest\Highlight\IsInjection;
use Tempest\Highlight\Tokens\TokenTypeEnum;

final readonly class PhpAttributeInstanceInjection implements Injection
{
use IsInjection;

public function getPattern(): string
{
return '(?<match>\#\[(.|\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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 '(?<match>\#\[(.|\n)*?\])';
return '(?<match>\#\[[\w]+\])';
}

public function parseContent(string $content, Highlighter $highlighter): string
Expand Down
6 changes: 4 additions & 2 deletions src/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +62,8 @@ public function getInjections(): array
...parent::getInjections(),
new PhpHeredocInjection(),
new PhpDocCommentInjection(),
new PhpAttributeInjection(),
new PhpAttributeInstanceInjection(),
new PhpAttributePlainInjection(),
new PhpFunctionParametersInjection(),
];
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Languages/Php/PhpLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public static function data(): array
['/** @var Foo $var */', '<span class="hl-comment">/** <span class="hl-value">@var</span> <span class="hl-type">Foo</span> <span class="hl-variable">$var</span> */</span>'],
['{~}): Foo {}~}', '<span class="hl-blur">}): <span class="hl-type">Foo</span> {}</span>'],
['{~class~} Foo {}', '<span class="hl-blur"><span class="hl-keyword">class</span></span> <span class="hl-type">Foo</span> {}'],
['#[ConsoleCommand()]', '<span class="hl-attribute">#[<span class="hl-type">ConsoleCommand</span>()]</span>'],
['#[ConsoleCommand]', '<span class="hl-attribute">#[<span class="hl-type">ConsoleCommand</span>]</span>'],
['#[ConsoleCommand(foo: [])]', '<span class="hl-attribute">#[<span class="hl-type">ConsoleCommand</span>(<span class="hl-property">foo</span>: [])]</span>'],
[
'public string $fullName {
get => $this->first . " " . $this->last;
Expand Down

0 comments on commit 18be2c7

Please sign in to comment.