plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

PHP 8.1 - Formatting for a match a lot of cases (multi-line) could be improved

Open AndreasA opened this issue 2 years ago • 6 comments

Not 100% sure how the best formatting for this would be, maybe some indentation for sub-sequent lines and the => also on a new line or maybe a line per case but the current syntax makes it difficult to find when the combined cases stop.

This might be related to https://github.com/prettier/plugin-php/issues/1964 though I don't think so.

Prettier 2.6.2

PHP Plugin 0.18.4

{
  "printWidth": 80,
  "tabWidth": 4,
  "useTabs": false,
  "singleQuote": true,
  "phpVersion": "8.1",
  "trailingCommaPHP": true,
  "braceStyle": "psr-2",
  "requirePragma": false,
  "insertPragma": false
}

Input:

<?php declare(strict_types=1);

match ($case) { 
     'foo', 'foo2', 'foo3', 'foo4', 'foo5', 'foo6', 'foo7', 'foo8', 'foo9', 'foo10', 'foo11', 'foo12' => ['bar'], 
     'cd' => [],
     default => [],
};

Output:

<?php declare(strict_types=1);

match ($case) {
    'foo', 'foo2', 'foo3', 'foo4', 'foo5', 'foo6', 'foo7', 'foo8', 'foo9',
    'foo10', 'foo11', 'foo12' => ['bar'],
    'cd' => [],
    default => [],
};


Expected behavior:

Not 100% sure what the best formatting is but the current is not optimal. Maybe some indentation for sub-sequent lines and placing the => on a separate line would help.

Playground sample

AndreasA avatar Apr 08 '22 09:04 AndreasA

We could apply for formatting of a list, but its not pretty

<?php declare(strict_types=1);

match ($case) {
    'foo',
    'foo2',
    'foo3',
    'foo4',
    'foo5',
    'foo6',
    'foo7',
    'foo8',
    'foo9',
    'foo10',
    'foo11',
    'foo12',
      => ['bar'],
    'cd' => [],  
    default => [],
};

cseufert avatar Jun 01 '22 02:06 cseufert

@cseufert Not 100% sure about the best solution either but to behonest I don't find you suggestion that bad as it would be similar to how a switch is formatted if one combines a lot of cases. Though I think the => should be intended more.

Otherwise another solution might be something like this but I think I prefer your suggestion as it also gives the function the most space:

<?php declare(strict_types=1);

match ($case) {
    'foo', 'foo2', 'foo3', 'foo4',
        'foo5',` 'foo6', 'foo7', 'foo8', 
        'foo9', 'foo10', 'foo11', 'foo12',
            => ['bar'],
    'cd' => [],  
    default => [],
};

AndreasA avatar Jun 01 '22 03:06 AndreasA

I have a prelimiary patch for this, but struggling to get the line breaking to behave however for your case we get the following: https://github.com/prettier/plugin-php/blob/5d9ff96626c45b6088dbc5efd61c69588c756b24/tests/match/snapshots/jsfmt.spec.js.snap#L100-L127

see PR #2003

cseufert avatar Jun 03 '22 01:06 cseufert

@cseufert Looks good. Though I think moving the => to a new line and using some indentation might be better as one sees that a new case chain starts more easily but it isn't that important.

AndreasA avatar Jun 03 '22 17:06 AndreasA

Yer, haven't managed to get prettier to actually work doing that, and I'm not sure why it doesn't format that way

cseufert avatar Jun 04 '22 09:06 cseufert

@cseufert Maybe some other formatting rule that runs after match is formatted? Though I guess it is unlikely as it could lead to interesting cross results but not impossible.

AndreasA avatar Jun 06 '22 20:06 AndreasA

Released in v0.19.0 :tada:

czosel avatar Sep 07 '22 07:09 czosel