IntelliJ IDEA 2016.1 Help

Using Language Injections

On this page:

See also:


Before you start working with language injections, make sure that IntelliLang plugin is enabled. The plugin is bundled with IntelliJ IDEA and activated by default. If it is not, enable the plugin as described in the section Enabling and Disabling Plugins.


IntelliJ IDEA makes it possible to work with islands of different languages embedded in the source code. You can inject other languages into string literals. This can be done within the source code written in most (but not all) of the supported languages (Java, JavaScript, Groovy, Python, Ruby, XML, PHP, and CSS for IntelliJ IDEA Ultimate Edition). The typical examples are HTML fragments injected into JavaScript code, SQL statements in Java or XML, and so on.

When the editor recognizes a string as a language injection:

  • Syntax and error highlighting and coding assistance are extended to this string.
  • You can open and modify it in a separate tab in the editor, as if you were working with the source code in the corresponding language.

    To open an injection in the editor, use the Edit <Language> Fragment intention action.

Injecting a language into the source code and cancelling language injection

To tell IntelliJ IDEA that certain text should be treated as an embedded source code fragment, you can use:

  • The Inject Language/Reference intention action, which is applied to a particular string:

    After that, you have to select the desired language from the menu:


    Note that if you use this method, it’s possible that the string literal will stay marked as a language injection only within a limited period of time. That is, IntelliJ IDEA, at a certain moment, may "forget" that the corresponding literal is a language injection. The period of the injection "persistence" will depend on the language, context and the modifications that you make in other parts of your source code.

  • A dedicated @Language annotation in Java source code, for example, @Language("JavaScript") or @Language("HTML").
  • Comments such as //language=<language_ID>, /*language=<language_ID>*/, #language=<language_ID>, <!--language=<language_ID>-->, --language=<language_ID>. For example:
    //language=SQLString s ="select*fromtest.users";

    For more information, see Using comments.

  • The Language Injections page of the Settings dialog. By creating new language injection configurations on that page, you can specify that a certain method parameter, text in an XML tag, or XML attribute should always be treated as an embedded source code fragment in another language.

To cancel language injection, choose the intention action Un-inject Language/Reference.

Moving injections between the IDE and the project levels

User-defined injections are assigned either to the IDE or the project level. The IDE-level injections are available in all of your projects. The project-level injections can be used only in the project in which they are defined.

To move the injections between the IDE and the project levels, use the Move to Project/IDE Scope icon  icon_injection_move_to_project_make_global  on the Language Injections page of the Settings dialog.

The built-in injections are the IDE-level ones, and their scope cannot be changed.

Using comments

Here is the syntax to be used when declaring language injections by means of comments:

//language="<language_ID>" prefix="<prefix>" suffix="<suffix>"

<language_ID> is an ID of the injected language. The prefix and suffix are optional.

If specified, the prefix is added before the string literal and the suffix - after. The prefix, the string literal itself and the suffix are added all together to form a single injected language fragment.

Quotation marks are used if there are spaces within the language ID, prefix or suffix. Only double quotes can be used (").

In most of the cases the language ID is intuitive, e.g. CSS, HTML, SQL, MySQL, DB2, Oracle, PostgreSQL and so on. If you are not sure about the language ID, use the suggestion list for the Inject Language intention action as the source of information. For example, type "" in the editor and place the cursor between " and ". Press Alt+Enter and select Inject Language. In the list that is shown, what precedes the opening parentheses are language IDs. For example, within the list item AIDL (Android IDL files), AIDL is a language ID.

The comment should precede the string literal to which it applies and should be placed as close as possible to that literal.

The following examples illustrate the use of comments for language injections in Java code.




myMethod//language=CSS prefix="body {" suffix=}("\ncolor: #00ff00;\nfont-size: 120%\n");

JavaScript 1.8:

String s =//language="JavaScript 1.8""vara;\na =1;";

See Also

Last modified: 13 July 2016