Today we'll take a tour through a unique feature found in IntelliJ IDEA called Language Injection.
Working on a project these days frequently makes you combine various languages and embed them into one another. Think of all the SQL or EJBQL queries scattered across your Java or XML files. Maybe your Java code returns snippets of HTML, CSS or XML and you use XPath a couple of times here and there.
Language Injection has been designed to help you live in such an environment.
What is it?
Look at the piece of code below:
This is how a String variable holding a piece of html code looks like in most IDEs. Now check this one out:
The same variable, but this time annotated with @Language. Annotating the variable made IntelliJ IDEA start-up the html support for the string value, inspect the code and offer us html-specific code assistance.
Now the IDE puts all its html editing power into our String Java variable and helps us code fast and possibly avoid mistakes.
How about all the other languages out there?
Consistently you can get this type of support for all the supported languages. Use the Alt + Enter key shortcut while positioned over a String value and select Inject language from the pop-up menu.
You get a pretty long list of languages you can inject.
So you can edit, for example, CSS
obviously in all the supported different dialects
How does it all integrate with my project?
The code inside strings with injected languages is attached to your project. When looking at a column inside an embedded SQL statement, for example, press Control + Q to get the column definition.
Or hit Control + B to jump right to its definition in an SQL script.
Anything else to show?
As for the @Language annotation itself, it follows the rules set by JSR-305 and so may one day become something other IDEs will recognize or even support. Until then, keep the annotations.jar file, that comes with IntelliJ IDEA, on your classpath for other IDEs to recognize them.
Feeling more polyglot already?