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

Breadcrumb

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

function PrettyPrinterAbstract::printFormatPreserving

Perform a format-preserving pretty print of an AST.

The format preservation is best effort. For some changes to the AST the formatting will not be preserved (at least not locally).

In order to use this method a number of prerequisites must be satisfied:

  • The startTokenPos and endTokenPos attributes in the lexer must be enabled.
  • The CloningVisitor must be run on the AST prior to modification.
  • The original tokens must be provided, using the getTokens() method on the lexer.

Parameters

Node[] $stmts Modified AST with links to original AST:

Node[] $origStmts Original AST with token offset information:

Token[] $origTokens Tokens of the original code:

Overrides PrettyPrinter::printFormatPreserving

File

vendor/nikic/php-parser/lib/PhpParser/PrettyPrinterAbstract.php, line 556

Class

PrettyPrinterAbstract

Namespace

PhpParser

Code

public function printFormatPreserving(array $stmts, array $origStmts, array $origTokens) : string {
    $this->initializeNodeListDiffer();
    $this->initializeLabelCharMap();
    $this->initializeFixupMap();
    $this->initializeRemovalMap();
    $this->initializeInsertionMap();
    $this->initializeListInsertionMap();
    $this->initializeEmptyListInsertionMap();
    $this->initializeModifierChangeMap();
    $this->resetState();
    $this->origTokens = new TokenStream($origTokens, $this->tabWidth);
    $this->preprocessNodes($stmts);
    $pos = 0;
    $result = $this->pArray($stmts, $origStmts, $pos, 0, 'File', 'stmts', null);
    if (null !== $result) {
        $result .= $this->origTokens
            ->getTokenCode($pos, count($origTokens) - 1, 0);
    }
    else {
        // Fallback
        // TODO Add <?php properly
        $result = "<?php" . $this->newline . $this->pStmts($stmts, false);
    }
    return $this->handleMagicTokens($result);
}

API Navigation

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