Avoid negative indendation in formatting-preserving printer

Fixes #1015.
This commit is contained in:
Nikita Popov 2024-09-29 15:34:59 +02:00
parent e50c67b7a9
commit 961f158f6d
2 changed files with 35 additions and 2 deletions

View File

@ -736,7 +736,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
$result .= $extraLeft;
$origIndentLevel = $this->indentLevel;
$this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment);
$this->setIndentLevel(max($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment, 0));
// If it's the same node that was previously in this position, it certainly doesn't
// need fixup. It's important to check this here, because our fixup checks are more
@ -839,7 +839,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
\assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos);
$origIndentLevel = $this->indentLevel;
$lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
$lastElemIndentLevel = max($this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment, 0);
$this->setIndentLevel($lastElemIndentLevel);
$comments = $arrItem->getComments();

View File

@ -35,3 +35,36 @@ if ($a) {
@@{"\t"}@@$x;
@@{"\t"}@@$y;
}
-----
<?php
array_merge(
[
$x,
$y,
]
);
-----
$call = $stmts[0]->expr;
$stmts[0]->expr = new Expr\StaticCall(new Node\Name\FullyQualified('Compat'), $call->name, $call->args);
-----
<?php
\Compat::array_merge([
$x,
$y,
]);
-----
<?php
if ($a) {
if (
$b
) {}
}
-----
$stmts[0] = new Stmt\While_($stmts[0]->cond, $stmts[0]->stmts);
-----
<?php
while ($a) {
if (
$b
) {}
}