mixing two languages in one file?

I would like to include some javascript in my java files.  I am using rhino to build an application.

This bug was filed four years ago... is there something in v11 which lets me mix up languages in one file?

6 comments
Comment actions Permalink

The (bundled) IntelliLang plug-in will allow you to annotate Strings (including fields, local variables, method parameters, and method returns) as a Language type via the @Language annotation. This provides syntac highlighting and code completion when editing that String (or a value being passed into a method). Below are some sample screenshots. In File > Settings, in the "Project Settings" section you can configure automatic language injections. For example such that a parameter passed in a Library method is seen as JavaScript. To define for all future projects, make the configuration in File > Other Settings > Template Settings

You will need to add the IntelliJ annotations jar as a dependency (available in the "redist" directory of the install directory of intelliJ IDEA). Note that with your cursor on a string literal, you can type Alt+Enter, and one of the intentions will be to "Inject Language". Select that and IDEA will add the @Language annotation and offer to add the needed dependency if it is not already available.

See the "IntelliLang" section in the help guide for more information.


screenshot.png

0
Comment actions Permalink

Thank you, I did not know about that functionality.  That is very helpful.

However, your proposed solution requires my Javascript to be inside a Java string.

I need my javascript to be right up next to my Java code, not wrapped in a string.  I suppose I could write a preprocessor to yank it out of a string, but it would be nice to avoid that.

0
Comment actions Permalink

The syntax of the Java language does not allow you to put JavaScript code next to Java in the same file. If your files contain such code, they are not written in Java, and you would need to write a plugin to let IntelliJ IDEA understand them.

0
Comment actions Permalink

Thank you, I assumed as much but was hoping something like Intellilang would support this "out of the box".  I will look into plugin development.
It would be great if rhino was supported directly.

0
Comment actions Permalink

I don't see how Rhino allows you to do this either.

0
Comment actions Permalink

Thanks.

0

Please sign in to leave a comment.