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

Support for Twig Component HTML Syntax

Open weaverryan opened this issue 1 year ago • 14 comments

Hi!

Thank you 💯 for this plugin. This is just a request that I would personally LOVE, in case you're interested and able to add it.

In Symfony UX 2.8, we've added a new HTML-style Twig syntax: https://symfony.com/bundles/ux-twig-component/current/index.html#component-html-syntax

The syntax is pretty straightforward, though I have no idea how hard it would be to support it. Ideas include:

A) When you type <twig: you get an auto-complete from available components. Components all have, in practice, an AsTwigComponent attribute (or its sub-class AsLiveComponent). There is an optional name arg, which defines the name. Else we use the short class name (i.e. class name without the namespace).

B) Currently, PHPStorm complains that the twig namespace is not bound. It'd be awesome if that could just go away.

Screenshot 2023-05-03 at 3 11 23 PM

C) When typing an attribute, it would be cool to auto-complete from public properties on the component class, as that's the most common things you'll pass in - e.g. <twig:Alert ty would autocomplete type if there is a public $type property. This would also work if you had an attribute prefixed with a : - like :ty.

D) When typing an attribute that starts with a :, it would be awesome if it then entered "Twig" mode - basically highlighting what's inside as if you were "inside" of Twig - e.g. currently:

Screenshot 2023-05-03 at 3 15 52 PM

But in theory, the inside of :type would look like:

Screenshot 2023-05-03 at 3 16 09 PM

Again, thanks for your work - no expectations on this - just something I'd love personally.

Cheers!

weaverryan avatar May 03 '23 19:05 weaverryan

Also along this line, auto-completion within a twig component's template would be nice as well.

#[AsTwigComponent]
class Alert
{
    public string $type = 'success';
    public string $message;

    public function someMethod() {}
}
{# templates/components/Alert.html.twig #}
<div class="alert alert-{{ type }}"> {# autocomplete "type", "this.type" #}
    {{ message }} {# autocomplete "message", "this.message" #}
    {{ this.someMethod }} {# autocomplete "this.someMethod" #}
</div>

Thanks for this plugin - as Ryan said, no expectations :)

kbond avatar May 04 '23 14:05 kbond

yeah, watching the symfony ux with interest.

Its an area that will get some love. So feel free to throw in ideas :)

Haehnchen avatar May 04 '23 18:05 Haehnchen

Not ideal, but you can disable the inspection for XML > Unbound namespace prefix

image

dirkjf avatar Oct 20 '23 13:10 dirkjf

Would be really nice, if the plugin could somehow register twig as an XML namespace. Don't know if this is possible at all, but getting rid of the errors without disabling the inspection at all would be nice.

For the time: Highly appretiate the current state of support of Twig Components. It's pure joy!

althaus avatar Nov 23 '23 15:11 althaus

Another idea to throw into the mix: It'd be great to be able to Ctrl + Click through to either the component class and/or template :)

andyexeter avatar Dec 14 '23 14:12 andyexeter

Another idea to throw into the mix: It'd be great to be able to Ctrl + Click through to either the component class and/or template :)

This is already possible, at least for defined components (components with a class). It would be great to do the same for anonymous components !

ker0x avatar Dec 14 '23 16:12 ker0x

@ker0x it doesn't seem to work for me:

Screencast from 14-12-23 17:26:06.webm

Which versions of PhpStorm and the plugin are you using? I'm on PhpStorm 2023.3.1 and 2022.1.261 of the plugin (premium version if that matters).

andyexeter avatar Dec 14 '23 17:12 andyexeter

@andyexeter Same version as yours for PHPStorm and the plugin

https://github.com/Haehnchen/idea-php-symfony2-plugin/assets/5331654/8d52bc2a-b81d-4eab-8e98-8357b48e7c18

ker0x avatar Dec 14 '23 17:12 ker0x

@ker0x how odd. Only difference I can see is that your component is namespaced.

Which version of Symfony and the Twig UX component is that on? I tested on Symfony v6.4.1 and v2.13.3 of symfony/ux-twig-component

andyexeter avatar Dec 14 '23 18:12 andyexeter

Ctrl + Click through to either the component class and/or template

:heavy_check_mark:

anonymous components

:heavy_check_mark: via https://github.com/Haehnchen/idea-php-symfony2-plugin/pull/2264

The ux component itself add some changes related to namespaces, the next plugin update will support it more fluently.

thanks to @weaverryan for this SymfonyCon coding talks, there is now a list of todos :)

Haehnchen avatar Dec 14 '23 19:12 Haehnchen

@ker0x curious to know how you're able to ctrl-click to the template ? ctrl-click takes me straight to the PHP class, but I see no option to go to the template instead ?

yched avatar May 27 '24 13:05 yched

@yched For an anonymous component it will take you straight to the template. However with a defined components (components with a class) it show me a dropdown with component class and template and i can choose where i want to go.

My PHPStorm version is 2024.1 EAP and the Symfony plugins is 2024.1.274

ker0x avatar May 27 '24 17:05 ker0x

Thanls @ker0x . That does work indeed for components defined in the top-level app codebase.

That doesn't work however for components defined in a bundle, ctrl-click only brings me to the class but doesn't seem to ctach the template. I'll try to decribe that better i a separate issue.

yched avatar May 28 '24 08:05 yched

That doesn't work however for components defined in a bundle, ctrl-click only brings me to the class but doesn't seem to ctach the template. I'll try to decribe that better i a separate issue.

That seems related to the use of name_prefix : i opened https://github.com/Haehnchen/idea-php-symfony2-plugin/issues/2382 about it

yched avatar May 28 '24 10:05 yched