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

Breadcrumb

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

function Renderer::renderComments

Render node's comments

Parameters

Syntax\Node\Node $node Node:

bool $leading False to render trailing comments:

bool|null $blockContent This paramater can have 3 values::

  • null: the node is not a block
  • false: the node is an empty block
  • true: the node is a block with content

Return value

string

2 calls to Renderer::renderComments()
Renderer::renderNode in vendor/mck89/peast/lib/Peast/Renderer.php
Renders a node
Renderer::renderStatementBlock in vendor/mck89/peast/lib/Peast/Renderer.php
Renders a node as a block statement

File

vendor/mck89/peast/lib/Peast/Renderer.php, line 1114

Class

Renderer
Nodes renderer class

Namespace

Peast

Code

protected function renderComments($node, $leading = true, $blockContent = null) {
    $code = "";
    $fn = $leading ? "getLeadingComments" : "getTrailingComments";
    $comments = $node ? $node->{$fn}() : array();
    $numComments = count($comments);
    if ($numComments) {
        $lastFormatted = $blockContent === false ? true : $leading;
        $refNode = $node;
        $refKey = $leading ? "end" : "start";
        $refNodeKey = $leading ? "start" : "end";
        $indent = $this->getIndentation();
        foreach ($comments as $k => $comment) {
            $lastComment = $k === $numComments - 1;
            $isMultilineComment = $comment->getKind() === Comment::KIND_MULTILINE;
            // Check if the comment must be formatted with new line and indentations
            $format = true;
            if ($refNode && $isMultilineComment && $comment->location && $refNode->location && $comment->location->{$refKey}
                ->getLine() === $refNode->location->{$refNodeKey}
                ->getLine()) {
                $format = false;
            }
            
            //If the last comment wasn't formatted but this one must be formatted, add the new
            
            //line and the indentation
            if ($format && !$lastFormatted) {
                $code .= $this->renderOpts->nl . $indent;
            }
            
            //Leading comments on empty blocks must render the initial indentation if format
            
            //is enabled
            if ($format && $blockContent === false && !$k) {
                $code .= $indent;
            }
            $commentRaw = $comment->getRawText();
            
            //Reindent multiline comments if necessary
            if ($isMultilineComment && $indent && $this->renderOpts->rci) {
                $commentRaw = preg_replace("/^\\s+\\*/m", $indent . " *", $commentRaw);
            }
            $code .= $commentRaw;
            
            //If format is enabled, add the new line character and the indentation if the node
            
            //is not an empty block or the it's not the last comment
            if ($format && ($blockContent !== true || !$lastComment || !$isMultilineComment)) {
                
                //For non multiline comments the new line is mandatory, even if the formatter
                
                //disables it
                $code .= !$isMultilineComment && !$this->renderOpts->nl ? "\n" : $this->renderOpts->nl;
                
                //Last comment on blocks must not render indentation
                if ($blockContent === null || !$lastComment) {
                    $code .= $indent;
                }
            }
            $refNode = $comment;
            $lastFormatted = $format;
        }
    }
    return $code;
}

API Navigation

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