function TokenStream::calcIndentMap
Precalculate the indentation at every token position.
Return value
int[] Token position to indentation map
1 call to TokenStream::calcIndentMap()
- TokenStream::__construct in vendor/
nikic/ php-parser/ lib/ PhpParser/ Internal/ TokenStream.php - Create token stream instance.
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ Internal/ TokenStream.php, line 251
Class
- TokenStream
- Provides operations on token streams, for use by pretty printer.
Namespace
PhpParser\InternalCode
private function calcIndentMap(int $tabWidth) : array {
$indentMap = [];
$indent = 0;
foreach ($this->tokens as $i => $token) {
$indentMap[] = $indent;
if ($token->id === \T_WHITESPACE) {
$content = $token->text;
$newlinePos = \strrpos($content, "\n");
if (false !== $newlinePos) {
$indent = $this->getIndent(\substr($content, $newlinePos + 1), $tabWidth);
}
elseif ($i === 1 && $this->tokens[0]->id === \T_OPEN_TAG && $this->tokens[0]->text[\strlen($this->tokens[0]->text) - 1] === "\n") {
// Special case: Newline at the end of opening tag followed by whitespace.
$indent = $this->getIndent($content, $tabWidth);
}
}
}
// Add a sentinel for one past end of the file
$indentMap[] = $indent;
return $indentMap;
}