trivago / prettier-plugin-twig-melody

Code formatting plugin for Prettier which can handle Twig/Melody templates
Apache License 2.0
155 stars 35 forks source link

Custom twig tags #35

Closed twbartel closed 4 years ago

twbartel commented 4 years ago

ATTENTION: Before merging this, https://github.com/trivago/melody/pull/154/commits has to be merged and released.

This merge request makes use of a new capability in the Melody parser to handle non-standard/unknown Twig tags.

By default, all unknown Twig tags will be handled and printed in a best-effort fashion that should be appropriate for a lot of Twig tags.

Additionally, a new option twigMultiTags is introduced which can be used to tell the parser about tag names that belong together, like nav and endnav. Example inspired by Craft CMS:

twigMultiTags: [
    "nav,endnav",
    "switch,case,default,endswitch",
    "ifchildren,endifchildren",
    "cache,endcache"
]

This will basically add indentation to everything between the first and the last tag. Tag names must be comma separated.

sjelfull commented 4 years ago

One tiny syntax thing: The twigMultiTags tag names could maybe be an array instead of comma separated list.

twbartel commented 4 years ago

@sjelfull Totally agree. I wanted to make them an array. However, I did not find a way to get that through Prettier's validation of options. If you know how to do that, please tell me! :-)

EDIT: To elaborate a bit more, this is how the option is defined:

twigMultiTags: {
    type: "path",
    category: "Global",
    array: true,
    default: [{ value: [] }],
    description: "Make custom Twig tags known to the parser."
}

I use type: "path" because it's the only string type available. There is only a handful of types allowed, and "array" is not one of them. I can tell Prettier that twigMultiTags should be an array by setting array: true, but I have not found a way to tell Prettier that the passed value should be an array of an arrays.