-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tokens starting on the same offset
- Loading branch information
Showing
7 changed files
with
59 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Tempest\Highlight\Tokens; | ||
|
||
final readonly class GroupTokens | ||
{ | ||
public function __invoke(array $tokens): array | ||
{ | ||
// Sort tokens in the right order | ||
usort($tokens, function (Token $a, Token $b) { | ||
if ($a->start === $b->start) { | ||
return $b->end <=> $a->end; | ||
} | ||
|
||
return $a->start <=> $b->start; | ||
}); | ||
|
||
// Group tokens by parent and child | ||
/** @var \Tempest\Highlight\Tokens\Token[] $groupedTokens */ | ||
$groupedTokens = []; | ||
|
||
while($token = current($tokens)) { | ||
$token = $token->cloneWithoutParent(); | ||
|
||
foreach ($tokens as $compareKey => $compareToken) { | ||
if ($token->contains($compareToken)) { | ||
if ($token->canContain($compareToken)) { | ||
$token->addChild($compareToken); | ||
} | ||
|
||
unset($tokens[$compareKey]); | ||
} | ||
} | ||
|
||
if ($token->parent === null) { | ||
$groupedTokens[] = $token; | ||
} | ||
|
||
next($tokens); | ||
} | ||
|
||
return $groupedTokens; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
```php | ||
public function __construct( | ||
?string $encoding = null, | ||
) {} | ||
|
||
'namespace '; | ||
``` |