-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use ast visitors to detect icon annotations and functions
- Loading branch information
1 parent
7601070
commit d3713f7
Showing
4 changed files
with
128 additions
and
26 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
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,77 @@ | ||
<?php | ||
|
||
namespace PHPIcons\Console\Visitors; | ||
|
||
use PHPIcons\Config\PHPIconsConfig; | ||
use PHPIcons\Console\Icon; | ||
use PHPIcons\Console\IconData; | ||
use PhpParser\Comment; | ||
use PhpParser\Comment\Doc; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
class IconsAnnotationsVisitor extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @var array<Doc|Comment> | ||
*/ | ||
public array $comments = []; | ||
|
||
public function __construct( | ||
private readonly string $filePath, | ||
private readonly IconData $iconData, | ||
private readonly PHPIconsConfig $config | ||
) { | ||
} | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
foreach ($node->getComments() as $nodeComment) { | ||
if (! in_array($nodeComment, $this->comments)) { | ||
$this->comments = [...$this->comments, $nodeComment]; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function afterTraverse(array $nodes) | ||
{ | ||
foreach ($this->comments as $commentNode) { | ||
// parse comment to get icon keys | ||
$matched = preg_match_all( | ||
"/@icon\s*\(\s*([\'\"])(?<iconKey>[a-z0-9\-\:]+)\\1\s*\)/", | ||
$commentNode->getText(), | ||
$matches, | ||
PREG_OFFSET_CAPTURE | ||
); | ||
|
||
if (! $matched) { | ||
continue; | ||
} | ||
|
||
foreach ($matches['iconKey'] as $iconKeyMatch) { | ||
$startFilePosition = $commentNode->getStartFilePos() + $iconKeyMatch[1]; | ||
$line = substr_count(substr($commentNode->getText(), 0, $iconKeyMatch[1]), PHP_EOL); | ||
$this->iconData->addIcon( | ||
new Icon( | ||
$this->filePath, | ||
new String_( | ||
$iconKeyMatch[0], | ||
[ | ||
'startLine' => $commentNode->getStartLine() + $line, | ||
'endLine' => $commentNode->getStartLine() + $line, | ||
'startFilePos' => $startFilePosition, | ||
'endFilePos' => $startFilePosition + strlen($iconKeyMatch[0]), | ||
] | ||
), | ||
$this->config->getDefaultPrefix() | ||
) | ||
); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace PHPIcons\Console\Visitors; | ||
|
||
use PHPIcons\Config\PHPIconsConfig; | ||
use PHPIcons\Console\Icon; | ||
use PHPIcons\Console\IconData; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
class IconsFunctionsVisitor extends NodeVisitorAbstract | ||
{ | ||
public function __construct( | ||
private readonly string $filePath, | ||
private readonly IconData $iconData, | ||
private readonly PHPIconsConfig $config | ||
) { | ||
} | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
if ( | ||
($node instanceof FuncCall || $node instanceof MethodCall) | ||
&& ($node->name instanceof Name || $node->name instanceof Identifier) | ||
&& in_array($node->name->toString(), $this->config->getIdentifiers(), true) | ||
&& $node->getArgs() !== [] && $node->getArgs()[0]->value instanceof String_ | ||
) { | ||
/** @var String_ $strNode */ | ||
$strNode = $node->getArgs()[0] | ||
->value; | ||
|
||
$this->iconData->addIcon(new Icon($this->filePath, $strNode, $this->config->getDefaultPrefix())); | ||
} | ||
|
||
return null; | ||
} | ||
} |