mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 15:18:17 +01:00
Add possibility to pretty print expressions to PrettyPrinter
This commit is contained in:
parent
29bac2a120
commit
684a638f46
@ -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.
|
@ -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.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user