Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. TokenStream.php

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\Internal

Code

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal