- 1. Recognizing a String as Language Injection
- 2. Editing a Language Injection
- (optional) 3. Working with Databases and SQL Language Injections
1. Recognizing a String as Language Injection
In many situations, the editor will automatically recognize a string as a language injection. In some cases, we may have to inject them manually.
1.1. Automatically Injecting a Language Reference
1.2. Manually Injecting a Language Reference
For situations where the IDE can not automatically determine the language we're injecting in a string, we can tell the IDE which language we're intending to use. Using Alt+Enter on the string contents, we can invoke the Inject Language/Reference intention and select the language contained in the string.
2. Editing a Language Injection
Fragments of different languages can be edited inline, within the string literal, or in a separate editor tab.
2.1. Editing a Language Injection within a String Literal
Once the editor knows about the language injection, we can start working with it. Syntax and error highlighting and coding assistance are extended to this string.
2.2. Editing a Language Injection in a Separate Editor Tab
We can also use the Edit <language> Fragment intention to open a separate editor tab in which we can work with the fragment.
In this separate editor tab, we can work with the source code in the corresponding language. PhpStorm will update the original string literal with the modifications we make.
(optional) 3. Working with Databases and SQL Language Injections
A very nice thing about language injections is how they behave when a database is connected to the project we are working in (see Databases and SQL Editor in PhpStorm). Not only will PhpStorm recognize SQL syntax and provide syntax highlighting and code completion on the injected language itself: it will also know about tables and columns.
We can edit these queries in a separate editor by using the Edit MySQL Fragent intention. Since a database connection is configured, PhpStorm also lets us run this query immediately in the database console.