Language Injections Enhancements in PhpStorm 8

In many codebases, small islands of different programming languages are being used in code, for example chunks of JavaScript written in a PHP string, or portions of HTML in a JavaScript variable, or database queries inside a string. With PhpStorm 8, we’ve enhanced the Language Injections feature to make working with different language fragments a breeze.

Here’s an example. We’re generating an HTML e-mail body here in which we’re using several PHP variables and so forth. PhpStorm recognizes that the string holds HTML mixed with PHP and provides syntax highlighting, code completion and navigation. Yes, all inside a string.

Language Injections recognize HTML and PHP

But there’s more! Let’s look inside.

This functionality is available in IntelliJ Idea, PyCharm, WebStorm, PhpStorm, RubyMine, and AppCode.

In the above examples, PhpStorm was able to recognize the fragment’s language. In other cases, it will not. We can easily hint the language injection by using the Inject Language/Reference intention (Alt+Enter).

Inject Language

Once the IDE knows the language injected, we can use the Edit Fragment intention (again, Alt+Enter) to open up a separate editor in which we can edit the language fragment. The advantage is that we can focus on the contents of the string instead of things like escaping, or adding the text we’re typing to the correct concatenated string part. PhpStorm will do all that for us.

PhpStorm escapes code fragment

Language injections work on any type of string: regular strings, concatenated strings, HEREDOC, and so on. If a string contains a code fragment that does not belong to the language being edited, PhpStorm will simply gray it out so its original value is always preserved. The last thing we want to do is break your precious code!

Grayed out MySQL

Speaking of SQL… Depending on the context, PhpStorm can figure out the value of a PHP variable in a SQL language injection. For example, the $wpdb->posts variable below will be materialized into a known table name when editing the fragment.

$wpdb variable materialized

This also means that running this query in the console (using the intention) will only prompt for one variable, $id in this case, since the IDE already knows the table name.

Run Query in Console

Download PhpStorm 8 Public Preview and give language injections a try! Your feedback is welcome through the issue tracker, by posting in the comments below, or in our forums!

Develop with pleasure!
– JetBrains PhpStorm Team

This entry was posted in Cool Feature, Early Access Program and tagged , , , , , , , . Bookmark the permalink.