1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

FPPP: Support insert at start of list node (#446)

This commit is contained in:
Nikita Popov 2017-12-01 22:09:51 +01:00
parent e5453f0d46
commit dc3ace55c3
4 changed files with 161 additions and 22 deletions

View File

@ -5,6 +5,10 @@ Version 4.0.0-dev
* Fixed comment indentation in formatting-preserving pretty printer.
### Added
* Added support for inserting at the start of list nodes in formatting-preserving pretty printer.
Version 4.0.0-alpha2 (2017-11-10)
---------------------------------

View File

@ -688,6 +688,10 @@ abstract class PrettyPrinterAbstract
) {
$diff = $this->nodeListDiffer->diffWithReplacements($origNodes, $nodes);
/** @var Node|null $delayedAddNode Used for insertions at the start of the list */
$delayedAddNode = null;
$delayedAddInsertStr = '';
$result = '';
foreach ($diff as $i => $diffElem) {
$diffType = $diffElem->type;
@ -726,23 +730,46 @@ abstract class PrettyPrinterAbstract
$comments = $arrItem->getComments();
$origComments = $origArrItem->getComments();
if ($comments !== $origComments) {
if ($origComments) {
$commentStartPos = $origComments[0]->getTokenPos();
\assert($commentStartPos >= 0);
$commentStartPos = $origComments ? $origComments[0]->getTokenPos() : $itemStartPos;
\assert($commentStartPos >= 0);
// Remove old comments
$itemStartPos = $commentStartPos;
$commentsChanged = $comments !== $origComments;
if ($commentsChanged) {
// Remove old comments
$itemStartPos = $commentStartPos;
}
if (null !== $delayedAddNode) {
$result .= $this->origTokens->getTokenCode(
$pos, $commentStartPos, $indentAdjustment);
if ($delayedAddInsertStr === "\n") {
$delayedAddComments = $delayedAddNode->getComments();
if ($delayedAddComments) {
$result .= $this->pComments($delayedAddComments) . $this->nl;
}
}
$result .= $this->origTokens->getTokenCode($pos, $itemStartPos, $indentAdjustment);
$this->safeAppend($result, $this->p($delayedAddNode));
if ($comments) {
// Add new comments
$result .= $this->pComments($comments) . $this->nl;
if ($delayedAddInsertStr === "\n") {
$result .= $this->nl;
} else {
$result .= $delayedAddInsertStr;
}
$result .= $this->origTokens->getTokenCode(
$commentStartPos, $itemStartPos, $indentAdjustment);
$delayedAddNode = null;
} else {
$result .= $this->origTokens->getTokenCode($pos, $itemStartPos, $indentAdjustment);
$result .= $this->origTokens->getTokenCode(
$pos, $itemStartPos, $indentAdjustment);
}
if ($commentsChanged && $comments) {
// Add new comments
$result .= $this->pComments($comments) . $this->nl;
}
} else if ($diffType === DiffElem::TYPE_ADD) {
if (null === $insertStr) {
@ -751,8 +778,15 @@ abstract class PrettyPrinterAbstract
}
if ($i === 0) {
// TODO Handle insertion at the start
return null;
if (count($diff) === 1) {
// TODO Handle insertion into empty list
return null;
}
// These will be inserted at the next "replace" or "keep" element
$delayedAddNode = $arrItem;
$delayedAddInsertStr = $insertStr;
continue;
}
$itemStartPos = $pos;

View File

@ -89,6 +89,7 @@ call1(
);
-----
<?php
x;
function test() {
call1(
$bar
@ -98,15 +99,14 @@ call2(
$foo
);
-----
$tmp = $stmts[0]->stmts[0];
$stmts[0]->stmts[0] = $stmts[1];
$stmts[1] = $tmp;
// Same test, but also prepending to $stmts, triggering fallback
array_unshift($stmts, new Stmt\Echo_([new Scalar\LNumber(42)]));
$tmp = $stmts[1]->stmts[0];
$stmts[1]->stmts[0] = $stmts[2];
$stmts[2] = $tmp;
// Same test, but also removing first statement, triggering fallback
array_splice($stmts, 0, 1, []);
-----
<?php
echo 42;
function test() {
call2(
$foo

View File

@ -63,11 +63,21 @@ try {
function test(Foo $param1) {}
-----
array_unshift($stmts[0]->params, new Node\Param(new Expr\Variable('param0')));
/* Insertion at the start not handled yet */
-----
<?php
function test($param0, Foo $param1)
function test($param0, Foo $param1) {}
-----
<?php
function test() {}
-----
$stmts[0]->params[] = new Node\Param(new Expr\Variable('param0'));
/* Insertion into empty list not handled yet */
-----
<?php
function test($param0)
{
}
-----
@ -138,4 +148,95 @@ $stmts[0]->name->parts[0] = 'Xyz';
-----
<?php
namespace
Xyz;
Xyz;
-----
<?php
function test() {
$foo; $bar;
}
-----
$node = new Stmt\Expression(new Expr\Variable('baz'));
array_unshift($stmts[0]->stmts, $node);
-----
<?php
function test() {
$baz;
$foo; $bar;
}
-----
<?php
function test() {
$foo; $bar;
}
-----
$node = new Stmt\Expression(new Expr\Variable('baz'));
$node->setAttribute('comments', [new Comment('// Test')]);
array_unshift($stmts[0]->stmts, $node);
-----
<?php
function test() {
// Test
$baz;
$foo; $bar;
}
-----
<?php
function test() {
// Foo bar
$foo; $bar;
}
-----
$node = new Stmt\Expression(new Expr\Variable('baz'));
$node->setAttribute('comments', [new Comment('// Test')]);
array_unshift($stmts[0]->stmts, $node);
-----
<?php
function test() {
// Test
$baz;
// Foo bar
$foo; $bar;
}
-----
<?php
function test() {
// Foo bar
$foo; $bar;
}
-----
$node = new Stmt\Expression(new Expr\Variable('baz'));
$node->setAttribute('comments', [new Comment('// Test')]);
array_unshift($stmts[0]->stmts, $node);
$stmts[0]->stmts[1]->setAttribute('comments', [new Comment('// Bar foo')]);
-----
<?php
function test() {
// Test
$baz;
// Bar foo
$foo; $bar;
}
-----
<?php
function test() {
// Foo bar
$foo; $bar;
}
-----
$node = new Stmt\Expression(new Expr\Variable('baz'));
$node->setAttribute('comments', [new Comment('// Test')]);
array_unshift($stmts[0]->stmts, $node);
$stmts[0]->stmts[1]->setAttribute('comments', []);
-----
<?php
function test() {
// Test
$baz;
$foo; $bar;
}