From 88be6127fa91145ebf4f02a75e4e813f09d7c99c Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 22 Sep 2020 22:41:02 +0200 Subject: [PATCH] FPPP: Fix remove + add at start of list --- lib/PhpParser/PrettyPrinterAbstract.php | 48 +++++++++---------- .../formatPreservation/listInsertion.test | 21 +++++++- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 80de763..bc85f76 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -756,7 +756,7 @@ abstract class PrettyPrinterAbstract $itemStartPos = $origArrItem->getStartTokenPos(); $itemEndPos = $origArrItem->getEndTokenPos(); - \assert($itemStartPos >= 0 && $itemEndPos >= 0); + \assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos); $origIndentLevel = $this->indentLevel; $lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment; @@ -767,16 +767,25 @@ abstract class PrettyPrinterAbstract $commentStartPos = $origComments ? $origComments[0]->getStartTokenPos() : $itemStartPos; \assert($commentStartPos >= 0); - $commentsChanged = $comments !== $origComments; - if ($commentsChanged) { - // Remove old comments - $itemStartPos = $commentStartPos; + if ($commentStartPos < $pos) { + // Comments may be assigned to multiple nodes if they start at the same position. + // Make sure we don't try to print them multiple times. + $commentStartPos = $itemStartPos; + } + + if ($skipRemovedNode) { + if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) { + // We'd remove the brace of a code block. + // TODO: Preserve formatting. + $this->setIndentLevel($origIndentLevel); + return null; + } + } else { + $result .= $this->origTokens->getTokenCode( + $pos, $commentStartPos, $indentAdjustment); } if (!empty($delayedAdd)) { - $result .= $this->origTokens->getTokenCode( - $pos, $commentStartPos, $indentAdjustment); - /** @var Node $delayedAddNode */ foreach ($delayedAdd as $delayedAddNode) { if ($insertNewline) { @@ -795,25 +804,16 @@ abstract class PrettyPrinterAbstract } } - $result .= $this->origTokens->getTokenCode( - $commentStartPos, $itemStartPos, $indentAdjustment); - $delayedAdd = []; - } else if (!$skipRemovedNode) { - $result .= $this->origTokens->getTokenCode( - $pos, $itemStartPos, $indentAdjustment); - } else { - if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) { - // We'd remove the brace of a code block. - // TODO: Preserve formatting. - $this->setIndentLevel($origIndentLevel); - return null; - } } - if ($commentsChanged && $comments) { - // Add new comments - $result .= $this->pComments($comments) . $this->nl; + if ($comments !== $origComments) { + if ($comments) { + $result .= $this->pComments($comments) . $this->nl; + } + } else { + $result .= $this->origTokens->getTokenCode( + $commentStartPos, $itemStartPos, $indentAdjustment); } // If we had to remove anything, we have done so now. diff --git a/test/code/formatPreservation/listInsertion.test b/test/code/formatPreservation/listInsertion.test index a179971..0795c9d 100644 --- a/test/code/formatPreservation/listInsertion.test +++ b/test/code/formatPreservation/listInsertion.test @@ -316,4 +316,23 @@ $stmts[0]->returnType->types[] = new Node\Name('C'); ----- stmts; +array_splice($fnStmts, 0, 1, $fnStmts[0]->stmts); +----- +