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

Possibility to exclude some parts of file from formatting

Open pczerkas opened this issue 1 year ago • 4 comments

Might be usefull sometimes

pczerkas avatar Nov 07 '24 06:11 pczerkas

@pczerkas I've been thinking about this and have formed a plan that should be workable, but I was wondering if you would mind sharing some examples where this would be helpful? Or even just one example 😊

And please let me know if you have an opinion about what syntax would be appropriate to delimit sections of code that shouldn't be formatted.

lkrms avatar Jan 16 '25 22:01 lkrms

@lkrms I've come up with a sound example:

having such function signature:

private function registerSql($sql, $bind = array(), $fetchMode = null, $resultType = null, $result = null)
{
[...]

I need this function (in package) to be PHP backward-compatible (so no "magic comma" is possible here), but still I want to reformat it, so it reads as:

private function registerSql(
    $sql,
    $bind = array(),
    $fetchMode = null,
    $resultType = null,
    $result = null
) {
[...]

where for some other parts of the code/files I still prefer pretty-php's auto formatting.

But pretty-php reformats this function's signature back to one-liner after save (VSCode extension).

pczerkas avatar Mar 20 '25 08:03 pczerkas

Thanks for following up, @pczerkas :)

That's an interesting example, because if you weren't using the symfony preset (which forces function/method declarations onto one line), what you're doing would work without needing to exclude anything from formatting. I'll have a think about ways this scenario could be improved for you.

Setting that aside, though, excluding a parameter list from formatting via, say, a @noformat tag in the relevant PHPDoc wouldn't be a great solution, because one would expect the rest of the method to be excluded too:

/**
 * @noformat
 */
private function registerSql(
    $sql,
    $bind = array(),
    $fetchMode = null,
    $resultType = null,
    $result = null
) {
[...]

So we probably need something like this instead (or in addition to the above):

// @noformat-start
private function registerSql(
    $sql,
    $bind = array(),
    $fetchMode = null,
    $resultType = null,
    $result = null
) {
// @noformat-end
[...]

What do you think? Do you have a preference re: tag name? Alternatives might be:

  • @noFormat / @noFormatStart / @noFormatEnd
  • @prettyphpIgnore / @prettyphpIgnoreStart / @prettyphpIgnoreEnd (similar to PHPUnit's @codeCoverageIgnore tags)
  • @prettyphp-ignore / @prettyphp-ignore-start / @prettyphp-ignore-end

lkrms avatar Mar 21 '25 23:03 lkrms

I like these: @noformat / @noformat-start / @noformat-end @prettyphp-ignore / @prettyphp-ignore-start / @prettyphp-ignore-end

all-lowercase is easier to remember ;) maybe (for the sake of easier implementation?) *-start / *-end pair would suffice?

Thx!

pczerkas avatar Mar 24 '25 20:03 pczerkas