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:
JavaScript
HTML
CSS
JSON
XML
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
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:
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.
You can also use
prefix=
andpostfix=
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:
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.