final public function addWithSuggestedLineBreaks()

in src/BaseCodeBuilder.hack [288:335]


  final public function addWithSuggestedLineBreaks(?string $code): this {
    if ($code === null) {
      return $this;
    }

    // If there's more than 1 line, add them 1 by 1
    $lines = Str\split($code, "\n");
    if (C\count($lines) > 1) {
      // The last line shouldn't have a finishing end line,
      // so add it manually
      $last_line = VecP\pop_backx(inout $lines);
      $this->addLinesWithSuggestedLineBreaks($lines);
      return $this->addWithSuggestedLineBreaks($last_line);
    }

    // Subtracting two to allow space for indenting and/or opening braces.
    $max_length = $this->getMaxCodeLength() - 2;
    if ($this->isInsideFunction) {
      // Please note that this method is called both from inside a function or
      // stuff like class/func declaration.
      $max_length = $max_length - 2;
    }

    $lines_with_sugg_breaks = \explode(self::DELIMITER, $code);
    $final_lines = vec[];
    foreach ($lines_with_sugg_breaks as $line) {
      if (!$line) {
        // Continue if the line is empty
        continue;
      }
      $line = $line as string;
      if (C\is_empty($final_lines)) {
        $final_lines[] = $line;
      } else {
        $last_line = C\lastx($final_lines);
        $final_lines = Vec\take($final_lines, C\count($final_lines) - 1);
        $composite_line = $last_line.' '.$line;
        if (\strlen($composite_line) > $max_length) {
          $final_lines[] = $last_line;
          $final_lines[] = $line;
        } else {
          // Concatenate the line to the last line
          $final_lines[] = $composite_line;
        }
      }
    }
    return $this->add(Str\join($final_lines, "\n  "));
  }