Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaPrinter: Directives support 🤩 #996

Closed
wants to merge 22 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored how lines combined into block of text.
LastDragon-ru committed Nov 20, 2021
commit d521d1bd8754ee9deb4ce0a5ce5a7a6952a80767
85 changes: 49 additions & 36 deletions src/Utils/SchemaPrinter.php
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
use function array_filter;
use function array_keys;
use function array_map;
use function array_sum;
use function array_values;
use function count;
use function explode;
@@ -40,6 +41,7 @@
use function ksort;
use function ltrim;
use function mb_strlen;
use function mb_strpos;
use function self;
use function sprintf;
use function str_replace;
@@ -239,7 +241,7 @@ protected static function printDescription(array $options, $def, string $indenta
return static::printDescriptionWithComments($description, $indentation, $firstInBlock);
}

$preferMultipleLines = static::isMultiline($description);
$preferMultipleLines = static::isLineTooLong($description);
$blockString = BlockString::print($description, '', $preferMultipleLines);
$prefix = $indentation !== '' && ! $firstInBlock
? "\n" . $indentation
@@ -286,16 +288,8 @@ protected static function printArgs(array $options, array $args, string $indenta
$arguments[] = $value;
}

// Multiline?
$serialized = '';

if ($description || static::isMultiline($length)) {
$serialized = "(\n" . implode("\n", $arguments) . "\n{$indentation})";
} else {
$serialized = '(' . implode(', ', array_map('trim', $arguments)) . ')';
}

return $serialized;
// Return
return static::printLines($arguments, '(', ')', $description || static::isLineTooLong($length), $indentation);
}

/**
@@ -304,13 +298,10 @@ protected static function printArgs(array $options, array $args, string $indenta
*/
protected static function printArg(FieldArgument $type, array $options, string $indentation = '', bool $firstInBlock = true): string
{
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
return static::printDescription($options, $type, $indentation, $firstInBlock) .
$indentation .
static::printInputValue($type) .
static::printTypeDirectives($type, $options, $indentation);
$field = static::printLine($field, $firstInBlock);

return $field;
}

/**
@@ -375,16 +366,13 @@ static function (FieldDefinition $f, int $i) use ($options): string {
*/
protected static function printField(FieldDefinition $type, array $options, string $indentation = '', bool $firstInBlock = true): string
{
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
return static::printDescription($options, $type, $indentation, $firstInBlock) .
' ' .
$type->name .
static::printArgs($options, $type->args, ' ') .
': ' .
(string) $type->getType() .
static::printTypeDirectives($type, $options, $indentation);
$field = static::printLine($field, $firstInBlock);

return $field;
}

/**
@@ -483,13 +471,10 @@ static function (EnumValueDefinition $value, int $i) use ($options): string {
*/
protected static function printEnumValue(EnumValueDefinition $type, array $options, string $indentation = '', bool $firstInBlock = false): string
{
$value = static::printDescription($options, $type, $indentation, $firstInBlock) .
return static::printDescription($options, $type, $indentation, $firstInBlock) .
' ' .
$type->name .
static::printTypeDirectives($type, $options, $indentation);
$value = static::printLine($value, $firstInBlock);

return $value;
}

/**
@@ -519,23 +504,18 @@ static function (InputObjectField $f, $i) use ($options): string {
*/
protected static function printInputObjectField(InputObjectField $type, array $options, string $indentation = '', bool $firstInBlock = true): string
{
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
return static::printDescription($options, $type, $indentation, $firstInBlock) .
' ' .
static::printInputValue($type) .
static::printTypeDirectives($type, $options, $indentation);
$field = static::printLine($field, $firstInBlock);

return $field;
}

/**
* @param array<string> $items
*/
protected static function printBlock(array $items): string
{
return count($items) > 0
? " {\n" . implode("\n", $items) . "\n}"
: '';
return static::printLines($items, ' {', '}', true);
}

/**
@@ -588,7 +568,7 @@ protected static function printTypeDirectives($type, array $options, string $ind
$serialized = '';

if ($directives) {
$delimiter = static::isMultiline($length) ? "\n{$indentation}" : ' ';
$delimiter = static::isLineTooLong($length) ? "\n{$indentation}" : ' ';
$serialized = $delimiter.implode($delimiter, $directives);
}

@@ -605,18 +585,51 @@ protected static function printTypeDirective(DirectiveNode $directive, array $op
return Printer::doPrint($directive);
}

protected static function printLine(string $string, bool $firstInBlock = true): string {
if (!$firstInBlock && static::isMultiline($string)) {
$string = "\n".ltrim($string, "\n");
/**
* @param array<string> $lines
*/
protected static function printLines(array $lines, string $begin, string $end, bool $multiline, string $indentation = ''): string
{
$block = '';

if ($lines) {
$multiline = $multiline || static::isLineTooLong(array_sum(array_map('mb_strlen', $lines)));

if ($multiline) {
$wrapped = false;

for ($i = 0, $c = count($lines); $i < $c; $i++) {
// If line too long and contains LF we wrap it by empty lines
$line = trim($lines[$i], "\n");
$wrap = static::isLineTooLong($line) || mb_strpos($line, "\n") !== false;

if ($i === 0) {
$line = "\n{$line}";
}

if (($wrap && $i > 0) || $wrapped) {
$line = "\n{$line}";
}

$block .= "{$line}\n";
$wrapped = $wrap;
}

$block .= $indentation;
} else {
$block = implode(', ', array_map('trim', $lines));
}

$block = $begin.$block.$end;
}

return $string;
return $block;
}

/**
* @param string|int $string
*/
protected static function isMultiline($string): bool {
protected static function isLineTooLong($string): bool {
return (is_string($string) ? mb_strlen($string) : $string) > static::LINE_LENGTH;
}
}