Android Studio is a powerful IDE with a multitude of features that I’ll probably never discover. However, when I do discover a feature of the IDE that I’ve been dreaming about, I get irrationally excited about it (and often times write a blog post such as this one). One feature that really piqued my interest lately is the “Language Injection” feature.
What is Language Injection?
Language injection allows you to work with different languages in the same file, for example having SQL statements stored inside a Kotlin string literal. Language injection improves the syntax highlighting and editing of that snippet.
Have you ever used the AndroidX Room library and wondered how it achieves the SQL syntax highlighting? It is using Language Injection to ensure that the syntax of string literals are highlighted and even validates whilst you type.
When you type in a Room
@Query field, it does validation for correct SQL and gives suggestions for table names / column names that you have defined in your app.
How do I use it in other scenarios?
Here is an example of a string that stores SQL, but doesn’t show any syntax highlighting:
But that is no fun! We want highlighting. Using Language Injection can be as simple as annotating a String with
// language=sql . This needs to be placed above the String literal and it will magically transform into a nicely highlighted String.
Syntax highlighting is not the only interesting part about Language Injection, you can even edit the snippet in a separate editor to make your life a bit easier. Just open the context actions for the line (yellow lightbulb icon), and then click “Edit … Fragment” .
This will open up a separate editor that you can type in, making it a bit easier to edit long Strings.
Where did I find Language Injections to be useful?
We use a lot of OpenGL in the Over app and we have GLSL code that sometimes lives inside a Kotlin file. I was getting a bit frustrated without having proper syntax highlighting on these snippets. I didn’t want to go down the road of having to create separate GLSL files that we load up on demand, as this would require us to pass an Android
Context object everywhere (as well as the overhead of file reading).
Enabling GLSL syntax highlighting in Strings
In order to get GLSL code snippets to have proper syntax highlighting in the IDE, I installed the GLSL Support Plugin in Android Studio.
Then all you need to do is place the
// language=glsl or the
@Language("glsl") on the line above the String literal and you will have beautiful syntax highlighting inline, with editing functionality. 🍾
Custom Class Syntax Highlighting
What if you wanted to do something like Room does where if anyone types in the
@Query field, it immediately starts syntax highlighting?
In our example, we could create a custom
Glsl annotation, and then set up a new Language Injection rule, to automatically highlight code with that annotation:
We then annotate the
codeSnippet String with the newly defined
Glsl annotation. Then inside the Editor Preferences -> Language Injections, click + and select Generic Kotlin:
We can create a new rule that looks for this annotation and applies GLSL highlighting whenever a parameter is annotated:
After this point, you probably want your teammates to also have this rule set up for them too, as currently it’ll only be in your IDE. So at the bottom of the Language Inspection settings, you can move the rule to “Project scope” instead of “IDE scope”:
Once you’ve done that, you will want to check the
.idea/IntelliLang.xml into version control as this contains the rule you’ve just created.
It is worth noting that you can also just use the
@Language("glsl") annotation on a String parameter in a class and the same outcome will be produced in this example, but a custom annotation will provide you with a bit more power if you wish to add custom suffixes and prefixes.
Language Injection is a powerful, underrated feature of Android Studio/IntelliJ. To learn more from the official docs, check out this link.
Enjoying my content? Find me on Twitter and subscribe to this blog below for email updates when a new post is out.