Add possibility to pretty print expressions to PrettyPrinter

This commit is contained in:
nikic 2011-08-04 18:19:45 +02:00
parent 29bac2a120
commit 684a638f46
2 changed files with 31 additions and 16 deletions

View File

@ -154,3 +154,5 @@ For the code mentioned in the above section this should create the output:
echo $msg, "\n";
}
printLine('Hallo World!!!');
You can also pretty print only a single expression using the `prettyPrintExpr()` method.

View File

@ -78,6 +78,11 @@ abstract class PHPParser_PrettyPrinterAbstract
protected $precedenceStackPos;
protected $noIndentToken;
public function __construct() {
$this->precedenceStack = array($this->precedenceStackPos = 0 => 19);
$this->noIndentToken = uniqid('_NO_INDENT_');
}
/**
* Pretty prints an array of nodes (statements).
*
@ -86,12 +91,20 @@ abstract class PHPParser_PrettyPrinterAbstract
* @return string Pretty printed nodes
*/
public function prettyPrint(array $nodes) {
$this->precedenceStack = array($this->precedenceStackPos = 0 => 19);
$this->noIndentToken = uniqid('_NO_INDENT_');
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false));
}
/**
* Pretty prints an expression.
*
* @param PHPParser_Node_Expr $node Expression node
*
* @return string Pretty printed node
*/
public function prettyPrintExpr(PHPParser_Node_Expr $node) {
return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
}
/**
* Pretty prints an array of nodes (statements) and indents them optionally.
*