Language injections in IntelliJ IDEA


Sometimes you might need to edit code fragment located inside source file of another language. IntelliJ IDEA have a nice feature, allowing to inject custom language into given block.

Imagine we want to keep CSS in XML (don’t ask me why). Here how it will looks. Not nice at all.

plain xml

Just add a comment <!-- language=CSS --> before the tag, and see that now you have highlighting, autocompletion, inspections and much more!

comment

If you need to make this changes global, just call a context menu, and choose “inject language or reference menu”.

shortcut

You can achieve same results with a bit more control via settings.

menu

As you can see from previous screen, IDEA already have dozens of injections configured for you, use them as an inspiration.
Personally I use it when I’m editing BPMN files with Groovy code inside, and xml with XPath expressions in tags.

Enjoy and experiment!

P.S. If it doesn’t work, check that IntelliLang plugin is enabled!
More details: in documentation.