IntelliJ IDEA 2016.2 Help

Using Language Injections

In this section:

Introduction

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.

Prerequisites

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

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:
    inject_language

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

    ij_inject_language

    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"):
    ij_inject_language_sql
  • Comments such as //language=<language_ID>, /*language=<language_ID>*/, #language=<language_ID>, <!--language=<language_ID>-->, --language=<language_ID>.

    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.

For example, SQL injection in JavaScript code will look as follows:

var i; for (var j=1;j<10;j++){ var s= "SELECT * FROM A WHERE B=C"; }

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/Preferences 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.

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.

MySQL:

ij_inject_language_comment_mysql

JavaScript 1.8:

ij_inject_language_comment_javascript

See Also

Last modified: 23 November 2016