Skip to content

Commit 554614d

Browse files
committed
Update PrettyPrinter.php
This could be solution to phpowermove#61. This counts lines difference between stms and insert new line character for every empty line.
1 parent d67b8f9 commit 554614d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/parser/PrettyPrinter.php

+38
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@
66

77
class PrettyPrinter extends Standard {
88

9+
/**
10+
* Pretty prints an array of nodes (statements) and indents them optionally.
11+
*
12+
* @param Node[] $nodes Array of nodes
13+
* @param bool $indent Whether to indent the printed nodes
14+
*
15+
* @return string Pretty printed statements
16+
*/
17+
protected function pStmts(array $nodes, $indent = true) {
18+
$result = '';
19+
$nodeBefore = NULL;
20+
foreach ($nodes as $node) {
21+
$comments = $node->getAttribute('comments', array());
22+
if ($comments) {
23+
$result .= "\n" . $this->pComments($comments);
24+
if ($node instanceof Stmt\Nop) {
25+
continue;
26+
}
27+
}
28+
29+
if ($nodeBefore && $nodeBefore->getLine() && $node->getLine()) {
30+
$diff = $node->getLine()- $nodeBefore->getLine();
31+
if ($diff > 0) {
32+
$result .= str_repeat("\n", $diff - 1);
33+
}
34+
}
35+
36+
$result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
37+
$nodeBefore = $node;
38+
}
39+
40+
if ($indent) {
41+
return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
42+
} else {
43+
return $result;
44+
}
45+
}
46+
947
public function pExpr_Array(Array_ $node) {
1048
return '[' . $this->pCommaSeparated($node->items) . ']';
1149
}

0 commit comments

Comments
 (0)