Language injections
You can inject a language (such as HTML, XML, JSON, RegExp, etc.) into a string value in your SQL code (i.e. a fragment enclosed in quotation marks) and, as a result, get comprehensive coding assistance when editing that value (fragment).
Prerequisite
For language injection features to be available, the IntelliLang plugin must be enabled. (This plugin is bundled with the IDE and enabled by default.)
Example: Injecting HTML. Opening a fragment editor
To get an impression of how language injections work:
- Create an SQL file and open it in the editor.
- Specify PostgreSQL as an SQL dialect for that file.
- Type:
CREATE TABLE test ( my_html VARCHAR(100) DEFAULT '' );
- 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 fragments 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 SQL file 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 value stays injected for a limited period of time.
- -- language=<language_ID>, see Using language injection comments.
- Auto-injection for XML and JSON, see Using auto-injection for XML and JSON.
- Data type patterns, see Using pattern-based injections for user-defined data types.
Using language injection comments
To inject a language by means of an injection comment, on a separate line before the statement that contains the target string value, add:
-- language=<language_ID>
e.g.
-- language=HTML
Example
- On the line preceding
CREATE TABLE test (
, type-- language=HTML
. - Check the light bulb menu (Alt+Enter).
As you can see, HTML has been injected into the string value.
- 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. JSON, HTML, XML. 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 auto-injection for XML and JSON
For values defined as XML and JSON types, the corresponding languages are injected automatically.
Example
- Replace
VARCHAR(100)
withXML
. - Place the cursor within the value in the quotation marks.
- Check the light bulb menu (Alt+Enter) to see that XML has been auto-injected.
Using pattern-based injections for user-defined data types
You can create patterns - e.g. for user-defined data types - and associate those patterns with languages. As a result, DataGrip, when it comes across a data type that matches the pattern, will inject the language specified for that pattern.
In the following example, we'll create a pattern for a data type ending in DATA
and associate that pattern with XML.
Example
- Replace
XML
withMYDATA
. - Place the cursor within the value in the quotation marks.
- Press Alt+Enter, select Inject by Type, and then select XML (XML files).
- In the dialog that opens, in the Type pattern field, specify
(?i).*DATA
. (The type patterns are specified using regular expressions. In this example,(?i)
turns the case-insensitive mode on;.*
stands for any number of any characters.) - Check the light bulb menu (Alt+Enter) to see that XML has been injected for the MYDATA value.
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
A language fragment may be combined with a prefix and a suffix which together act as a wrapper, thereby turning the fragment into a syntactically complete language unit. When editing your code, you can see the prefix and the suffix only in the fragment editor. They are not shown in the main editor.
The prefix and the suffix can be included in the injection comment as follows:
-- language=<language_ID> prefix=<prefix> suffix=<suffix>
The prefix
and suffix
attributes 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
CREATE TABLE test (
, 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.