Language injection PsiElement acquisition

Answered

How to query all elements in xml file injected by language PsiElement

 

I have an xml file, I customized a language myself, and then injected it into this xml file, now I have a requirement, that is, I get the language injected block of this xml, but I don't know how to convert it into Psi in custom language

6 comments
Comment actions Permalink

Hi,

You have to find all elements in an XML file that are of type PsiLanguageInjectionHost and get injections with InjectedLanguageManager.getInjectedPsiFiles(host).

1
Comment actions Permalink

Hello, when I get the xml file, the elements I see in it are all xml, I don't seem to see that the element type is PsiLanguageInjectionHost, how to get this element type, thank you

0
Comment actions Permalink

Hi,

PsiLanguageInjectionHost is an interface and there won't be any elements that are exactly PsiLanguageInjectionHost. You should check it with:

  • element is PsiLanguageInjectionHost (for Kotlin)
  • element instanceof PsiLanguageInjectionHost (for Java)

See also:

0
Comment actions Permalink

Hello
  I have implemented the getLanguagesToInject method of MultiHostInjector
I don't know if this is what you said above
The xml psi I am getting now is obtained by DomUtils, I want to use getContainingFile to get a PsiFile file in the obtained DomElement
Then I want to find in this psifile all the psi's that I have injected into my custom language

0
Comment actions Permalink

Yes, MultiHostInjector is related to PsiLanguageInjectionHost, and it's even mentioned in MultiHostInjector's Javadoc.

Once you have XmlFile obtained from the element, you can use:
PsiTreeUtil.collectElementsOfType(xmlFile, PsiLanguageInjectionHost.class)

to collect all elements where your language can be potentially injected. You can optimize it eg. to XmlAttributeValue if you are sure only XML values can contain your language.

After that use mentioned:
InjectedLanguageManager.getInjectedPsiFiles(host)

0
Comment actions Permalink

thanks, i will try

0

Please sign in to leave a comment.