References between HTML fragments and custom template language


I'm developing plugin for HTL language which is template language embedded in HTML files. HTL language has feature called blocks. Blocks can be defined by special HTL attributes applied on HTML tags, eg:

<div data-sly-test.someValue="${properties.someValue || properties.otherValue || 'default text'}">

Above block has identifier "someValue" which is actually value storing result of expression (${...}) evaluation. Block identifier can be used later inside of HTL expressions, eg:

<span data-sly-test="${someValue}">blah</span>

or even out of attribute value:


I would like to create references to "someValue" identifier to allow renaming, searching etc. Is it possible to make references between parts defined in HTML (part of data-sly-* attribute name, after dot) and parts defined inside of HTL expressions? I tried to access HTML context from HTL expression (using PSI), but it seems they are isolated. Is there any way to make HTML cooperate with HTL to achieve what I want? Maybe there is plugin with similar language I can base on?

Comment actions Permalink

Hi Karol,

in order to access another language tree in template language you can use FileViewProvider:


where lang in your case most likely should be HTMLLanguage.INSTANCE

Comment actions Permalink

Thank you Dennis, that's what I needed.

Comment actions Permalink

I got back to my plugin and read this article:

There is emphasized sentence: 

Every element which can be renamed or referenced needs to implement com.intellij.psi.PsiNamedElement interface.

As I understand it correctly I have to parse "data-sly-test.someValue" as part of my custom language's tokens and make them implement PsiNamedElement. Is it the only way or I can create references somehow else, without parsing these fragments?

I started to develop it by parsing data-sly-* attributes as HTL tokens, but they doesn't integrate with outer HTML tags. HTML parser complains that tag start is not closed:


I can't find any idea how to solve this issue. Can I somehow wrap my tokens to fix tags parsing or there is any other solution?

Comment actions Permalink

Sorry for the delayed reply.

What parts of `data-sly-test.someValue` could possibly be renamed?

Comment actions Permalink

Hi Dennis, no problem :-)

someValue is identifier which can be renamed. It could be also used in expressions (inside of ${}), like:

Comment actions Permalink

You can probably try following approach - return html attribute as a resolve result and handle rename in attribute descriptor.
To provide attribute descriptor you'll need AttributeDescriptorProvider (see example). Example of handling rename can be found here, you'll need to extend XmlAttributeDescriptorEx.


Please sign in to leave a comment.