ReSharper 2017.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., ReSharper 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, ReSharper supports the following embedded languages:

ReSharper 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, ReSharper detects embedded languages automatically.

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

Manual language injections

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

  • Use the context action, which actually tells ReSharper 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*/ 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 ReSharper will get the same features in the marked strings.
    By the way, the format of comments is compatible with 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 ReSharper resolve the string as valid CSS.

This feature is supported in the following languages/technologies:

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 12 October 2017

See Also