Using Language Injections
You can inject a language (such as HTML, CSS, XML, SQL, RegExp, etc.) into a string literal in your code and, as a result, get comprehensive coding assistance when editing that literal.
- Example: Injecting HTML. Opening a fragment editor
- Accessing language injection functions
- Ways to inject a language
- Using language injection comments
- Using the @Language annotation
- Accessing injection settings
- Using language injection prefixes and suffixes
Example: Injecting HTML. Opening a fragment editor
To get an impression of how language injections work:
- Create a Java class and open that class in the editor.
- Within the class body, type:
String s = "";
- Place the cursor between the quotation marks.
- Click
or press Alt+Enter, select Inject language or reference, and then select HTML (HTML files).
- Type:
<body><h1>Hello, World!</h1></body>
When typing, note that auto-completion for HTML tags is now available. Also note how the HTML code is highlighted.
- Let's now open a fragment editor for the injected HTML code: press Alt+Enter and select Edit HTML Fragment.
Here is the result:
You can use the fragment editor as an alternative (or in addition) to editing injected string literals in the "main editor".
- To complete the example, let's cancel the injection: switch to the main editor, press Alt+Enter and select Un-inject Language/Reference.
Note that the text between the quotation marks has become green which is the default color for string values. This indicates that the value in the quotation marks is now treated simply as text.
Don't close the editor yet. Later in this topic, we'll use our Java class for showing other language injection features.
Accessing language injection functions
Most of the functions related to language injections are accessed through a "light bulb menu" ( or Alt+Enter).
Ways to inject a language
You can inject a language by using:
- Inject language or reference command. You have already seen that in Example: Injecting HTML. Opening a fragment editor. This way of injecting a language is temporary: the string literal stays injected for a limited period of time.
- // language=<language_ID>, see Using language injection comments.
- @Language("<language_ID>"), see Using the @Language annotation.
Using language injection comments
To inject a language by means of an injection comment, on a separate line preceding the one that contains the target string literal, add:
// language=<language_ID>
e.g.
// language=HTML
NOTE: The syntax of comments should be appropriate for the language that you are using. So you may want to use # language=...
or -- language=...
rather than // language=...
.
Example
- On the line preceding
String s = "...";
, type// language=HTML
. - Check the light bulb menu (Alt+Enter).
As you can see, HTML has been injected into the string literal.
- Remove the commented line (e.g. Ctrl+Y) to come back to the previous state.
Language IDs
The language IDs, generally, are intuitive, e.g. MySQL, RegExp, XML, HTML. If not sure about the language ID, use the suggestion list for the Inject language or reference command. What precedes the opening parentheses there is the language IDs.
Using the @Language annotation
In Java code, you can use the @Language("language_ID")
annotation.
To be able to use this annotation: 1) the annotations.jar
(or annotations-java8.jar
) file should be included in your module dependencies and 2) the import org.intellij.lang.annotations.Language;
statement should be added to your class file. For both these tasks, IntelliJ IDEA provides the intention actions as shown in the Example that follows.
In other respects, the @Language
annotation works similarly to the injection comments.
Example
- On the line preceding
String s = "...";
, type@Language("HTML")
.If you haven't used this annotation in your project yet:
- To add
annotations.jar
to the module dependencies: place the cursor withinLanguage
, press Alt+Enter and select Add 'annotations' to classpath.In the dialog that opens, just click OK.
- If IntelliJ IDEA suggests adding the import statement, just press Alt+Enter. Otherwise, press Alt+Enter and select Import class.
In both cases, the result will look something like this:
- Place the cursor within the string literal and check the light bulb menu (Alt+Enter) to see that HTML has been injected.
- Remove the
@Language("HTML")
line to return to the previous state (Ctrl+Y).
Accessing injection settings
To access the language injection settings:
- Open the Settings / Preferences dialog (e.g. Ctrl+Alt+S).
- Go to the Language Injections page: .
For more info, see Language Injections page.
Using language injection prefixes and suffixes
Injecting a language may be accompanied with adding a prefix and a suffix. The prefix is added before the injected fragment, and the suffix - after the fragment.
Adding the prefix and the suffix is "imaginary". It doesn't change the actual string value. The prefix and the suffix act as a "wrapper" and their main purpose is to turn the injected fragment into a syntactically complete language unit. In this way, you give IntelliJ IDEA a broader context for validating the injected code fragment.
When editing your code, you can see the prefix and the suffix only in the fragment editor; the prefix and the suffix are not shown in the main editor.
The prefix and the suffix can be included in the injection comment whose complete form is
// language=<language_ID> prefix=<prefix> suffix=<suffix>
where the prefix and the suffix are optional.
Example
In this example, we'll remove the opening and closing <body>
tags from the injected code fragment and add these tags to the injection comment as the prefix and suffix.
- Remove the opening and closing
<body>
tags: e.g. place the cursor within the injected fragment, press Ctrl+Shift+Delete and select Remove Enclosing Tag body. - On the line preceding
String s = "...";
, type// language=HTML prefix=<body> suffix=</body>
- For the injected fragment, open the fragment editor.
Compare the fragments shown in the main and in the fragment editors.