Syntax highlighting for injected language into XML attribute value is not working

Answered

I am contributing to the Intellij plugin for the AEM platform (actual version: https://github.com/aemtools/aemtools/tree/v0.9.2). There are XML files where I injected custom language into some attribute values. There lexer and parser for the custom language. Example of XML file:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"

some.attribute.first="value"
some.attribute.isSecond="{Boolean}true"
         some.attribute.array="[attr.val1,attr.val2]" />

Value, {Boolean}true, [attr.val1,attr.val2] - are the code of my custom injected languge. I do injection implementing MultiHostInjector inteface.

class JcrPropertyInjector : MultiHostInjector {
override fun elementsToInjectIn()
: MutableList<out Class<out PsiElement>> = mutableListOf(XmlAttributeValue::class.java)

override fun getLanguagesToInject(
registrar: MultiHostRegistrar,
context: PsiElement) {
val attributeValue = context as? XmlAttributeValue ?: return

val attributeName = attributeValue.findParentByType(XmlAttribute::class.java)
?: return

val parentTag = attributeName.findParentByType(XmlTag::class.java)
?: return
val psiLanguageInjectionHost = context
as? PsiLanguageInjectionHost
?: return

//some logic to check suitable attribute name
registrar.startInjecting(JcrPropertyLanguage)
registrar.addPlace(null, null, context, TextRange.create(1, attributeValue.text.length - 1))
registrar.doneInjecting()
}
}

I decided to have my own highlighting of this custom injected language in the XML attribute value. I implemented ColorSettingsPage interface and registered it as an extension as well. As a result, in the Setting -> Editor -> Color Scheme -> JCR Property Language I see highlighted demo XML text, however, on the real file, injected language in the xml attribute value is highlighted as XML Injected Language Fragment (with green background).

I tried to implement EmbeddedTokenHighlighter too, but it didn't give any result.  

Could you please help me to understand what did I miss or what should I implement to highlight injected language? Maybe some HighlightVisitor or something else?

Let me know if I need to provide more details.

6 comments
Comment actions Permalink

Not sure I understand your problem. Is the problem that it uses "injected fragment background" color?

That's a user/theme specific setting anyway in Preferences | Editor | Color Scheme | XML.

0
Comment actions Permalink

Yann Cebron, thank you for the quick response. No, the main issue is that syntax highlighting for my custom language which is injected into XML attribute value is not working. Instead of my language syntax highlighting, the editor highlights XML attribute values as XML Injected Language Fragment (with green background).

 

0
Comment actions Permalink

Hi Konstantin,

Could you please clarify which branch contains all the changes? I checked out the v0.9.2 tag and can't find e.g. colors page for JCR Property Language (there is only a page for HTL).

0
Comment actions Permalink

Hi Karol Lewandowski, thank you for the quick response. I had this change locally. Pushed it to branch: https://github.com/aemtools/aemtools/tree/jcr_language_highlighting_issue.  

0
Comment actions Permalink

Hi Konstantin,

The reason is that your JpFileType is not a LanguageFileType, so please change it.

You should also register it in plugin.xml file in <fileType> extension point.

0
Comment actions Permalink

Wow, that was easy. It works now. Thank you very much!

Have a good day.

0

Please sign in to leave a comment.