Mix and Match Languages with Language Injections in IntelliJ IDEA
In almost any application, there are situations when you need to work on small elements of code in other languages embedded in your project code. For example, these can be fragments of HTML code in Java, or regular expressions sprinkled around your application. Working with such code elements is super easy in IntelliJ IDEA because it features language injections. They bring code highlighting, completion, error detection, and other coding assistance features to embedded code fragments. Let’s take a closer look at how language injections work.
Let’s try to embed a piece of HTML code into Java. All you need to do is to press Alt + Enter, select Inject language or reference, and pick HTML from the list.
By default, the IDE injects a language temporarily. It means that if you delete the embedded element, the injection will be lost as well. If you want to keep the injection permanently, press Alt + Enter again when you see the popup prompting you to replace the injection with an annotation. The IDE inserts the @Language annotation with the corresponding language ID. For method calls, the IDE creates a language injection pattern that looks like this: // language=HTML.
Now even if you remove the original injection, the IDE will already know which language is intended there and will be ready to offer coding assistance.
Dedicated Editor for Injected Code
In case you want to focus only on the language of the embedded code fragment, IntelliJ IDEA features a dedicated editor. It allows you to work with the code as if it were in a separate document. This can be really handy if the chunks of embedded code are large, or they are hard to make out from the rest of the code.
Of course, the editor for language injections provides the usual coding assistance features: code completion, intentions, inspections, and so on. To open the editor, press Alt + Enter and select Edit Fragment.
Inject Regular Expressions
Regular expressions is a powerful tool that has a broad range of applications, it can help with everything from search to input validation and refactorings. However, sometimes we all struggle with composing them correctly. If you use regular expressions in your code, IntelliJ IDEA has a nice little bonus for you. First, make sure to embed your regular expressions using language injections.
When regular expressions are injected, you can check whether they are compiled correctly. Press Alt + Enter and select Check RegExp. Now you can type a sequence of characters to make sure your expression matches the intended combination. Voilà!
Automatically Inject Languages
In IntelliJ IDEA, there is a set of predefined rules according to which the IDE automatically injects languages in certain places in your code. You can find these rules in Settings/Preferences | Editor | Language Injections.
You can modify the existing injection rules, duplicate them, and disable any that you don’t need.
If none of the existing rules work for you, you can create your own and then share them with other members of your team.
To share a custom rule via a VCS, move it to the Project scope using the corresponding option on the toolbar in Settings/Preferences | Editor | Language Injections. By doing so, you will create a new file in the .idea directory named IntelliLang.xml with your custom rules that you can place under version control.
If you don’t use a VCS, you can share your rules by exporting them to XML files and then importing them to another project.
The Bottom Line is…
Language injections are great for mixing in multiple languages in one project. They provide code completion, analysis, highlighting, and even a dedicated editor for embedded chunks of code. They can automatically inject the necessary languages in specific places in your code according to predefined rules or your own custom ones.
If you want to get more information on injections, visit the IntelliJ IDEA documentation. This feature works out of the box and is powered by the IntelliLang plugin that is bundled and enabled by default.
Have you ever used language injections in IntelliJ IDEA? What do you think of them? Let us know in the comments below.
In the meantime, IntelliJ IDEA 2020.2 is well under way. Join the early access program and be the first to try new features and improvements.