-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first shot at format preserving pretty printer
- Loading branch information
Showing
18 changed files
with
1,895 additions
and
782 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Foo; | ||
|
||
use PhpParser; | ||
use PhpParser\Node; | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
$lexer = new PhpParser\Lexer\Emulative([ | ||
'usedAttributes' => [ | ||
'comments', | ||
'startLine', 'endLine', | ||
'startFilePos', 'endFilePos', | ||
'startTokenPos', 'endTokenPos', | ||
], | ||
]); | ||
|
||
$parser = new PhpParser\Parser\Php7($lexer, [ | ||
'useIdentifierNodes' => true, | ||
'useConsistentVariableNodes' => true, | ||
] | ||
); | ||
|
||
$traverser = new PhpParser\NodeTraverser(); | ||
$traverser->addVisitor(new PhpParser\NodeVisitor\CloningVisitor()); | ||
|
||
$printer = new PhpParser\PrettyPrinter\Standard(); | ||
|
||
$code = <<<'PHP' | ||
<?php | ||
function foo() { | ||
echo | ||
1 | ||
+ | ||
2 | ||
+ | ||
3; | ||
echo "a"; | ||
echo "b"; | ||
} | ||
PHP; | ||
|
||
$oldStmts = $parser->parse($code); | ||
$oldTokens = $lexer->getTokens(); | ||
|
||
$newStmts = $traverser->traverse($oldStmts); | ||
|
||
$newStmts[0]->stmts[0]->exprs[0]->left->right->value = 42; | ||
$newStmts[0]->stmts[1] = new Node\Expr\Assign(new Node\Expr\Variable('a'), new Node\Scalar\LNumber(42)); | ||
|
||
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $code, $oldTokens); | ||
echo $newCode, "\n"; |
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
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,19 @@ | ||
<?php | ||
|
||
namespace PhpParser\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
/** | ||
* Visitor cloning all nodes and linking to the original nodes using an attribute. | ||
* | ||
* This visitor is required to perform format-preserving pretty prints. | ||
*/ | ||
class CloningVisitor extends NodeVisitorAbstract { | ||
public function enterNode(Node $origNode) { | ||
$node = clone $origNode; | ||
$node->setAttribute('origNode', $origNode); | ||
return $node; | ||
} | ||
} |
Oops, something went wrong.