-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow opting in to multiline arrays explicitly #1008
Comments
You can do this by yourself by extending the |
Why would I do this myself if it's already fully supported, the only issue is you can't opt-in sanely, which this issue is trying to remedy. |
Because it doesn't make sense to add options for everything. People might have different preferences for different code structures (many things can be formatted in multi-line fashion in PHP). What about a single item array? Would you still want to have it formatted on multiple lines? There are many unknowns and many people have different preferences. That's why (IMHO) it's not worth it to burden the maintainer and all the users with additional complexity. You can implement exactly what you need already today. |
Which is why it says "opting in" in the title of the issue.
Maybe. I should get a chance to do that since it's literally already supported, the hard part is done. |
@dkarlovi pmjones/php-styler may be useful to you here. |
@pmjones That looks like an external package so I'm not sure how it can help allowing to opt in into multiline arrays explicitly in this one? |
@ondrejmirtes just realized your argument
doesn't make sense here because there's literally the This could in theory be an extension to that: long / short, single line / multiline, do a Cartesian product of those. |
Love it, I did that in my project, but in another place for for arguments. class MyBetterStandardPrinter extends BetterStandardPrinter
{
#[\Override]
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false): string {
if ($this->onMultiline($nodes)) { // Note: Only theses two lines are differents from the original implementation
return $this->pCommaSeparatedMultiline($nodes, true) . $this->nl;
} elseif (!$this->hasNodeWithComments($nodes)) {
return $this->pCommaSeparated($nodes);
} else {
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
}
}
private function onMultiline(array $nodes): bool
{
foreach ($nodes as $node) {
if ($node->getAttribute('multiline')) {
return true;
}
}
return false;
}
} An BTW, this was a bit problematic, because PHP-Parser/lib/PhpParser/PrettyPrinter/Standard.php Lines 1173 to 1180 in 14f9c9d
So having an attribute to control the flow here, would be very convenient |
@lyrixx what do you think about |
Where should I put this? I don't know very well the printer :/ |
IMO, this should be a feature in PHP-Parser:
This allows opting in to dumping by just setting the attribute yourself if you're building the AST in memory and it also preserves the multiline-ness of the parsed code. |
I won't work for my use case since it's not about array, but about method/function parameter. But from userland point of view, how do you configure the kind? is it an attribute? |
@lyrixx yes, you pass it like new Array_($items, ['kind' => Array_::KIND_SHORT]) this is what the parser does when parsing too, but you can construct the same AST manually and get the same result. |
Since multiline arrays are fully supported, it seems weird to not allow a feature to enable them when printing except by adding a dummy comment in the array.
An attribute like
multiline = true
seems like it should be enough to check in maybeMultiline? This could then be set when parsing, preserving the intent.The text was updated successfully, but these errors were encountered: