Language Plugin for a Thymeleaf like templating language
Hello,
I recently spotted that an adapter for ANTLR based grammers was created to use them in IntelliJ plugins and therefore stared developing a plugin for a templating language called HTL which is similar to Thymeleaf. Although I can understand the basics of custom language plugins after reading through the SimplePlugin in your DevGuide I have problems understanding how this could work in a templating language. I tried to inject my language into the HTML attributes which works fine for highlighting, however I got stuck when dealing with references. How should I handle variables or references to Java beans defined in an HTML attribute? What I would like to achieve is to have code completion for Java getters in my HTL Expressions for the following case:
<div data-sly-use.bean="com.htl.language.JavaBean">
<h1 data-sly-text="${bean.textFromBean}">Title</h1> <!-- completion for getters -->
</div>
public class JavaBean {
public String getTextFromBean() {
return "Some text from a bean";
}
}
Does anyone have some experience with similar language plugins or are there even some examples somewhere?
Please sign in to leave a comment.
If you only have custom language fragments inside attributes, then language injection seems to be the best tool for that job.
Your reference's resolve() can do anything. In particular, it can find the getter by name (e.g. using com.intellij.psi.util.PropertyUtil) and return it. getVariants() could enumerate all getters and create completion elements based on those. You might also want to reuse BeanProperty/BeanPropertyElement to resolve to, its API might be useful to you.
For Find Usages/Rename to work, references with the property name should be searched in HTML files, see org.jetbrains.plugins.groovy.findUsages.AccessorMethodReferencesSearcher for an example. Reference's handleElementRename() would need to be aware of getter syntax as well.
Thanks for confirming that this seems to be the correct way, this already helped me a lot. :)
How can I access the HTML tree from within my expression fragment? The highest parent in my tree is the "file" of my fragment but not the HTML file where my fragment is placed in.
file.getContext() should return the HTML attribute value.