Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ: custom language: combine fragments in other languages

I am creating a custom language plugin for IntelliJ.

I would like it to be possible for a file in the new language to contain fragments of text in other languages.

The specific languages I would like to support are HTML, JS, CSS, and SQL.

I would also like to support other custom languages (i.e. languages I would define the syntax for).

The main feature I want is syntax coloring, but if I can get stuff like "go to declaration" and refactoring out of the box then all the better.

My last requirement is that it would be possible to use my own code to tell IntelliJ which language a fragment contains; fragments containing different languages will not be distinguishable at the lexer / parser level.

In short, I would like to implement something similar to what PhpStorm does when it detects, say, SQL inside a string:

enter image description here

I looked at IntelliJ's source code and found the ILazyParseableElementType interface which seemed relevant, but I'm not sure if this is the way to go (and if so - how to use it in my code exactly...)

Any pointers would be highly appreciated...

like image 785
obe Avatar asked Mar 11 '16 21:03

obe


1 Answers

The Intellij feature/terminology you are looking for is Language Injections.

Here is a github PR which implements a very similar feature for JFLex files.

In short you need to implement a LanguageInjector and add it to your plugin.xml as a <languageInjector implementation="YourImplClass">.

like image 162
PiRocks Avatar answered Oct 06 '22 10:10

PiRocks