driade / phpfmt8

PHP formatter for Sublime Text 4, with PHP 8 support.
BSD 3-Clause "New" or "Revised" License
46 stars 2 forks source link

New Transformation: AlignHashComments #76

Closed phuze closed 4 months ago

phuze commented 4 months ago

Could we add a new transformation option for hash comments, that would behave similar to AlignDoubleSlashComments?

Example:

//From:
$a = 1; # Comment 1
$bb = 22;  # Comment 2
$ccc = 333;  # Comment 3

//To:
$a = 1;      # Comment 1
$bb = 22;    # Comment 2
$ccc = 333;  # Comment 3
driade commented 4 months ago

Hi @phuze , good morning. I'll try to take it a look.

phuze commented 4 months ago

Hi, and thank you 🙏.

I'll mention there was a change in PHP 8 onwards, where #[ marks the beginning of an attribute. Anything else is still treated as a comment.

class VersionedObject
{
    #[JsonSerialize]   # attribute example
    public const version = '0.0.1';
}
driade commented 4 months ago

Thank you. Could you please elaborate this last message? We're is the bug?

phuze commented 4 months ago

My example was demonstrating the particular meaning of #[, not suggesting a bug. I thought it was worth mentioning, incase it could be of concern when parsing within phpfmt.

Hash comments (# hello) are otherwise parsed the same as double slash comments (// hello).

I wonder if we could rename AlignDoubleSlashComments to simply AlignComments, and make a small adjustment to support # comments.

Example: https://github.com/phuze/phpfmt8/commit/df7e3637681dfdc1b269c04efba2adbe47f4d4da

phuze commented 4 months ago

I tested the change by modifying my local vscode extension, and I can confirm it seems to work as intended. Extension file: %USERPROFILE%\.vscode\extensions\kokororin.vscode-phpfmt-1.2.10\dist\fmt.stub.php

Before: image

After: image

driade commented 4 months ago

Thank you! I've integrated your code, added some tests, and it's already added to the repo. The main difference is that we shouldn't rename an existing rule, because all the people that it's already using it would lose it.