IntelliJ Platform Plugin SDK
 
IntelliJ Platform Explorer

Language Injection

Edit pageLast modified: 07 August 2024

Language injection is the way the IntelliJ Platform handles different languages within the same source file. Well-known examples are:

  • Regular expressions in Java string literals

  • SQL queries in Java string literals

  • Fenced code blocks within Markdown files

Injected code is always bound to a specific context that depends on the surrounding code, and the IntelliJ Platform treats injected fragments as separate small files that are in a different language. To ensure highlighting and code-insight features work correctly, these fragments must be a valid statement or expression in the injected language. The three examples from above would then be shown like this in IntelliJ IDEs:

Regex Language Injection

It's not unusual that injected fragments are distributed among, e.g., several strings that are concatenated like it is common for SQL queries. To solve this, the IntelliJ Platform allows injecting a language into several fragments at once. Multiple parts are then considered belonging together.

As a plugin author, you can provide language injection in different ways:

  • For simple cases, the bundled IntelliLang plugin can handle injections, and plugin authors need to provide a configuration with patterns that specify the context where languages should be injected. IntelliLang can also be extended to support unknown custom languages.

  • Implementing the com.intellij.languageInjectionContributor extension point (EP) provides a high-level API for the injection of other languages. For more control over how a language is injected, plugin authors use the com.intellij.languageInjectionPerformer EP.

  • Implementing the com.intellij.multiHostInjector EP gives plugin authors the most control over where and how language injection will take place.

In the following sections, we'll discuss these three options in more detail.