Java code Injection in custom language.

Hello,

I'm trying to develop a basic plugin for CUP (http://www2.cs.tum.edu/projects/cup/index.php) support.

In this language we have Java code embedded between {: :}. 

Everything between {: :} is a Lexer Token (JavaCodeToken) in my current implementation.

Is it possible to inject Java for highlight and completion only in this part of the language ? 

The documentation for language injection in custom language is a bit lacking and every related forum posts is either vague or not answered.

Thanks you.

6 comments
Comment actions Permalink

You can do this programatically using com.intellij.psi.LanguageInjector

1
Comment actions Permalink

Ok, I guess I will try that.

 

Thanks you for the fast support :)

0
Comment actions Permalink

I’ve succeeded in injecting the Java Lang, I have one last question:

Is it possible to access a subset of Java, because the custom code doesn’t always contain a class definition ?

I’m wondering if I should remove the feature or let it be.

2
Comment actions Permalink

You might experiment with supplying a prefix/suffix so that injection text will only be valid in that context (e.g. class body, method body)

0
Comment actions Permalink

Again, perfect answer, thanks a lot Yann !

0
Comment actions Permalink

Hi!
I’m currently using injection of Java into my custom language too. I’ve already implemented LanguageInjector and added suffix and prefix. This ones work fine.
What I’ve stumbled upon is that IDE doesn’t analyze injectes code.
For example, my prefix is ‘class C { void m() {‘, suffix is ‘}}’ and injected fragment is ‘return “string”; return “string”;
I see no errors about returning String from void method or unreachable code at second return. I can also define private variables and do every other forbidden stuff that will never get compiled.

What must be done in order to enable code analysis of injected fragments?

1

Please sign in to leave a comment.