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

(PHP) Attributes with \ highlighted as comment instead of meta #4115

Open
DanielBadura opened this issue Sep 10, 2024 · 6 comments · May be fixed by #4119
Open

(PHP) Attributes with \ highlighted as comment instead of meta #4115

DanielBadura opened this issue Sep 10, 2024 · 6 comments · May be fixed by #4119
Labels
bug help welcome Could use help from community language

Comments

@DanielBadura
Copy link

Describe the issue
Using Attributes with \ are hilighted as comments instead of meta. If it's only a leading \ it is working as expected. But as soons as we add a \ after that, the highlighting is not correct anymore. This is a common situation when using attributes with PHP for example using Doctrine or Swagger etc.

Which language seems to have the issue?
php

Are you using highlight or highlightAuto?

highlight

Sample Code to Reproduce

use Doctrine\ORM\Mapping as ORM;

#[\ORM\Entity]
final class Account
{
    #[ORM\Id]
    #[ORM\Column('uuid')]
    private AccountId $id;

    #[Column('string')]
    #[\Column('string')]
    private string $name;
}

Bildschirmfoto 2024-09-10 um 11 28 09

Expected behavior
Same meta highlighting in these cases.

Additional context
Using highlight js through https://github.com/rehypejs/rehype-highlight which is using https://github.com/wooorm/lowlight. But i dont think the problem comes from there, since it's also wrong highlighted in the demo.

@DanielBadura DanielBadura added bug help welcome Could use help from community language labels Sep 10, 2024
@joshgoebel
Copy link
Member

What is the definition of an attribute in PHP, just #[...] or are there very precise rules?

@DanielBadura
Copy link
Author

Yes, Attributes are beginning with #[ and ending with ]. They can applied on various things in the language:

  • functions (including closures and short closures)
  • classes (including anonymous classes), interfaces, traits
  • class constants
  • class properties
  • class methods
  • function/method parameters

Also, each attribute may have values associated with it, but doesn't have to, similar to how a constructor of a class works. Also Attributes can be nested.

class Value 
{
}

#[Attribute('value', new Value(), new Attribute()]
function foo() {}

Here an example how PHPStorm is highlighting it:
Bildschirmfoto 2024-09-21 um 15 13 16

joshgoebel added a commit that referenced this issue Sep 22, 2024
@joshgoebel joshgoebel linked a pull request Sep 22, 2024 that will close this issue
2 tasks
@joshgoebel
Copy link
Member

Could you test #4119?

@DanielBadura
Copy link
Author

Yeah, this worked. So it was just the all uppercase.. :D

I just realised that i found another case which is not correctly working:

#[Event('car_ride.booked')]
final class RideBooked
{
    public function __construct(
        #[AccountNormalizer]
        public readonly Account $account
    ){}
}

Here, the attribute over a method argument of a function definition is not highlighted.

Bildschirmfoto 2024-09-23 um 15 55 01

@joshgoebel
Copy link
Member

Try that fix.

@DanielBadura
Copy link
Author

Yes this works, thank you! Very much appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants