JetBrains Rider 2018.1 Help

Language Injections

If a string literal (as well as a tag or an attribute in an XML-like language) contains some other formal language, such as regular expression, HTML, etc., JetBrains Rider can provide code inspection, quick-fixes, code completion, context actions, and many other features specific to this language right inside this excerpt.

In C#, JavaScript, and TypeScript strings, JetBrains Rider supports the following embedded languages:

JetBrains Rider can handle two types of language injections:

Automatic language injections

There are cases when language excerpts in another language file can be detected unambiguously, e.g. JavaScript inside <script></script> tags or CSS in the style attribute in HTML. In these cases, JetBrains Rider detects embedded languages automatically.

If necessary, you can disable automatic language injections in specific cases on the Code Editing | Language Injections page of JetBrains Rider options.

Manual language injections

When a formal language inside a string literal cannot be detected automatically, JetBrains Rider allows you to manually mark the literal as containing specific language in one of the following ways:

  • Use the context action, which actually tells JetBrains Rider to mark the symbol range corresponding to the string, save this range in its internal database and keep track of it as the containing file changes. This way is very quick and straightforward, but there are two downsides: the range can be lost after an external file change, such as a VCS merge, and the injection marked this way will only be tracked locally:
  • Mark method parameters accepting regular expressions with the [RegexPatternAttribute] from JetBrains.Annotations. This is the recommended way for regular expressions in parameters.
  • Put a comment /*language=javascript|html|regexp|jsregexp|json|css|xml*/ before the string literal. Granted, these comments require some typing and you can even think of them as contaminating your code. However, they make your intention clear to everyone who reads your code, they will not get lost, and anyone opening your code with JetBrains Rider will get the same features in the marked strings.
    By the way, the format of comments is compatible with JetBrains ReSharper and IntelliJ Platform-based IDEs.
    Language injections in C# strings with comments
    You can also use prefix= and postfix= parameters in the comments. For example, if a string only contains a list of CSS properties, you can add the following comment before it: //language=css prefix=body{ postfix=}. This will make JetBrains Rider resolve the string as valid CSS.
Last modified: 20 August 2018

See Also