Note: In this blog post, we’ll look at some of the basics of language injections. They work similar in both Rider and ReSharper – check this deep-dive post on language injections in ReSharper to learn more.
Mark as injected language
Like all good things in Rider, Alt+Enter is where it starts. In the editor, we can place the caret on a string literal and mark it as an injected language.
Rider will remember this string is an injected language, but sometimes it’s impossible to keep track. For example after a large refactoring or moving classes or methods around, Rider may lose track and we may have to mark the string as injected language again.
We can make use of injection comments – a way of telling Rider the string literal following the comment should be treated as injected language. Here are some examples of using injection comments:
We can also add a prefix and postfix, essentially telling Rider to treat the string literal as being “inside” the structure provided by prefix and postfix. In the above screenshot we are using this technique to get highlighting for CSS colors (see
myColor) or on the attributes of an
<a /> element without having the entire HTML element inside the string literal (see
More examples of using language injection comments are available in the IntelliJ IDEA web help.
Right now it’s only possible to mark a single string literal as injected language (so no concatenations, nor a C# interpolated string). Also, injected language fragments are always scoped to the local string instead of the entire document. We’ll be working to address these limitations!
Give the latest Rider EAP build a try! We’d love to hear your feedback!