References between HTML fragments and custom template language

Answered

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'}">
    ...
</div>

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:

<span>${someValue}</span>

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?

6 comments
Comment actions Permalink

Hi Karol,

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

psiFile.getViewProvider().getPsi(lang)

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

1
Comment actions Permalink

Thank you Dennis, that's what I needed.

0
Comment actions Permalink

I got back to my plugin and read this article: http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/reference_contributor.html

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?

0
Comment actions Permalink

Sorry for the delayed reply.

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

0
Comment actions Permalink

Hi Dennis, no problem :-)

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

${someValue}
0
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.

0

Please sign in to leave a comment.