Skip to content

Commit 88be612

Browse files
committed
FPPP: Fix remove + add at start of list
1 parent 8a97fa1 commit 88be612

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ protected function pArray(
756756

757757
$itemStartPos = $origArrItem->getStartTokenPos();
758758
$itemEndPos = $origArrItem->getEndTokenPos();
759-
\assert($itemStartPos >= 0 && $itemEndPos >= 0);
759+
\assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos);
760760

761761
$origIndentLevel = $this->indentLevel;
762762
$lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
@@ -767,16 +767,25 @@ protected function pArray(
767767
$commentStartPos = $origComments ? $origComments[0]->getStartTokenPos() : $itemStartPos;
768768
\assert($commentStartPos >= 0);
769769

770-
$commentsChanged = $comments !== $origComments;
771-
if ($commentsChanged) {
772-
// Remove old comments
773-
$itemStartPos = $commentStartPos;
770+
if ($commentStartPos < $pos) {
771+
// Comments may be assigned to multiple nodes if they start at the same position.
772+
// Make sure we don't try to print them multiple times.
773+
$commentStartPos = $itemStartPos;
774774
}
775775

776-
if (!empty($delayedAdd)) {
776+
if ($skipRemovedNode) {
777+
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
778+
// We'd remove the brace of a code block.
779+
// TODO: Preserve formatting.
780+
$this->setIndentLevel($origIndentLevel);
781+
return null;
782+
}
783+
} else {
777784
$result .= $this->origTokens->getTokenCode(
778785
$pos, $commentStartPos, $indentAdjustment);
786+
}
779787

788+
if (!empty($delayedAdd)) {
780789
/** @var Node $delayedAddNode */
781790
foreach ($delayedAdd as $delayedAddNode) {
782791
if ($insertNewline) {
@@ -795,25 +804,16 @@ protected function pArray(
795804
}
796805
}
797806

798-
$result .= $this->origTokens->getTokenCode(
799-
$commentStartPos, $itemStartPos, $indentAdjustment);
800-
801807
$delayedAdd = [];
802-
} else if (!$skipRemovedNode) {
803-
$result .= $this->origTokens->getTokenCode(
804-
$pos, $itemStartPos, $indentAdjustment);
805-
} else {
806-
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
807-
// We'd remove the brace of a code block.
808-
// TODO: Preserve formatting.
809-
$this->setIndentLevel($origIndentLevel);
810-
return null;
811-
}
812808
}
813809

814-
if ($commentsChanged && $comments) {
815-
// Add new comments
816-
$result .= $this->pComments($comments) . $this->nl;
810+
if ($comments !== $origComments) {
811+
if ($comments) {
812+
$result .= $this->pComments($comments) . $this->nl;
813+
}
814+
} else {
815+
$result .= $this->origTokens->getTokenCode(
816+
$commentStartPos, $itemStartPos, $indentAdjustment);
817817
}
818818

819819
// If we had to remove anything, we have done so now.

test/code/formatPreservation/listInsertion.test

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,23 @@ $stmts[0]->returnType->types[] = new Node\Name('C');
316316
-----
317317
<?php
318318
function test(): A
319-
|B|C {}
319+
|B|C {}
320+
-----
321+
<?php
322+
function test() {
323+
if ($x) {
324+
$a;
325+
$b;
326+
}
327+
$z;
328+
}
329+
-----
330+
$fnStmts =& $stmts[0]->stmts;
331+
array_splice($fnStmts, 0, 1, $fnStmts[0]->stmts);
332+
-----
333+
<?php
334+
function test() {
335+
$a;
336+
$b;
337+
$z;
338+
}

0 commit comments

Comments
 (0)