Skip to content

Commit

Permalink
Fix blur bleed
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 2, 2024
1 parent e2e6cd3 commit 042d8b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Tokens/GroupTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public function __invoke(array $tokens): array
{
// dump($tokens);
// dump($tokens);
// Sort tokens in the right order
usort($tokens, function (Token $a, Token $b) {
if ($a->start === $b->start) {
Expand Down Expand Up @@ -42,6 +42,7 @@ public function __invoke(array $tokens): array
unset($tokens[$compareKey]);
}
}

if ($token->parent === null) {
$groupedTokens[] = $token;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Tokens/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public function cloneWithoutParent(): self

public function canContain(Token $otherToken): bool
{
foreach ($this->children as $childToken) {
// If there's already a child token at the exact position,
// we're not going to allow another child to be added
if (
$childToken->start === $otherToken->start
&& $childToken->end === $otherToken->end
) {
return false;
}
}

return $this->type->canContain($otherToken->type);
}
}
18 changes: 18 additions & 0 deletions tests/Tokens/GroupTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Tempest\Highlight\Tests\Tokens;

use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Languages\Php\PhpLanguage;
use Tempest\Highlight\Tokens\DynamicTokenType;
use Tempest\Highlight\Tokens\GroupTokens;
use Tempest\Highlight\Tokens\ParseTokens;
use Tempest\Highlight\Tokens\Token;
use Tempest\Highlight\Tokens\TokenTypeEnum;

Expand All @@ -28,4 +30,20 @@ public function test_exact_overlaps(): void
$this->assertSame(TokenTypeEnum::KEYWORD, $grouped[0]->children[0]->type);
$this->assertSame(TokenTypeEnum::TYPE, $grouped[1]->type);
}

public function test_exact_overlaps_within_children(): void
{
$tokens = (new ParseTokens())('->addExtension(new CommonMarkCoreExtension())', new PhpLanguage());

$tokens[] = new Token(
offset: 0,
value: '->addExtension(new CommonMarkCoreExtension())',
type: new DynamicTokenType('hl-blur'),
);

$tokens = (new GroupTokens())($tokens);

$this->assertCount(1, $tokens);
$this->assertCount(3, $tokens[0]->children);
}
}
2 changes: 1 addition & 1 deletion tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

$environment = new Environment();

$highlighter = (new Highlighter(new InlineTheme(__DIR__ . '/../src/Themes/solarized-dark.css')));
$highlighter = (new Highlighter(new InlineTheme(__DIR__ . '/../src/Themes/highlight-light-lite.css')))->withGutter();

$environment
->addExtension(new CommonMarkCoreExtension())
Expand Down
14 changes: 3 additions & 11 deletions tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
```php{1}
$environment = new Environment();
$theme = new InlineTheme(__DIR__ . {+'/../src/Themes/solarized-dark.css'+}));
$highlighter = (new Highlighter($theme))->withGutter();
$environment
->addExtension(new CommonMarkCoreExtension())
->addRenderer(FencedCode::class, new CodeBlockRenderer($highlighter))
->addRenderer(Code::class, new InlineCodeBlockRenderer($highlighter));
```php
{~->addExtension(new CommonMarkCoreExtension())~}
dd();
```

0 comments on commit 042d8b3

Please sign in to comment.