ReSharper 2021.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, and so on, ReSharper can provide code inspection, quick-fixes, code completion, Context Actions, and many other features specific to this language right inside this excerpt.

ReSharper supports the following languages inside C#, JavaScript, and TypeScript string literals:

ReSharper can handle two types of language injections: automatic and manual.

There are cases when language excerpts in another language file can be detected unambiguously, for example, 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.

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:

    ReSharper: Analyzing CSS code inside a C# string literal

  • Mark method parameters, properties, and fields accepting regular expressions with the [RegexPatternAttribute] from JetBrains.Annotations. This is the recommended way for regular expressions.

  • 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 ReSharper will get the same features in the marked strings.
    By the way, the format of comments is compatible with JetBrains Rider 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 ReSharper resolve the string as valid CSS.

This feature is supported in the following languages and technologies:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C# Feature is not available in Visual Basic .NET Feature is not available in C++ Feature is not available in HTML Feature is not available in ASP.NET Feature is not available in RazorFeature is available in JavaScriptFeature is available in TypeScript Feature is not available in CSS Feature is not available in XML Feature is not available in XAML Feature is not available in Resource files Feature is not available in build script files Feature is not available in Protobuf Feature is not available in JSON

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: 08 March 2021