From 554614d944862cc69a6c19061bd1618c5f152345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Mat=C4=9Bna?= Date: Thu, 25 Apr 2019 10:08:55 +0200 Subject: [PATCH] Update PrettyPrinter.php This could be solution to #61. This counts lines difference between stms and insert new line character for every empty line. --- src/parser/PrettyPrinter.php | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/parser/PrettyPrinter.php b/src/parser/PrettyPrinter.php index fc97c6b..3106c20 100644 --- a/src/parser/PrettyPrinter.php +++ b/src/parser/PrettyPrinter.php @@ -6,6 +6,44 @@ class PrettyPrinter extends Standard { + /** + * Pretty prints an array of nodes (statements) and indents them optionally. + * + * @param Node[] $nodes Array of nodes + * @param bool $indent Whether to indent the printed nodes + * + * @return string Pretty printed statements + */ + protected function pStmts(array $nodes, $indent = true) { + $result = ''; + $nodeBefore = NULL; + foreach ($nodes as $node) { + $comments = $node->getAttribute('comments', array()); + if ($comments) { + $result .= "\n" . $this->pComments($comments); + if ($node instanceof Stmt\Nop) { + continue; + } + } + + if ($nodeBefore && $nodeBefore->getLine() && $node->getLine()) { + $diff = $node->getLine()- $nodeBefore->getLine(); + if ($diff > 0) { + $result .= str_repeat("\n", $diff - 1); + } + } + + $result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : ''); + $nodeBefore = $node; + } + + if ($indent) { + return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result); + } else { + return $result; + } + } + public function pExpr_Array(Array_ $node) { return '[' . $this->pCommaSeparated($node->items) . ']'; }