ReSharper Platform SDK Help

HTML-Like languages

ReSharper supports HTML files, but it also supports HTML-like languages, such as Razor (.cshtml) or WebForms (.aspx) files. These files contain HTML, but also contain other elements, such as custom tags, or new syntax. Rather than force each language to reimplement HTML parsing, ReSharper provides support that a language can build on, reusing existing and augmenting the existing HTML parsing infrastructure.

For example, a WebForms .aspx file can look like this, with custom <% and %> delimiters marking the ASP.NET specific syntax, and the asp: prefixed tags extending the HTML:

<%@ Page Title="Home Page" Langauge="C#" MasterPageFile="~/Site.Master" %> <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <h2><%: Title %></h2> <div> <!-- ... --> </div> </asp:Content>

And a Razor .cshtml file uses the @ symbol to delimit C# card that will be executed as the page runs. This C# can even contain HTML blocks.

<h2>@ViewBag.Header</h2> <table> @foreach (var person in Model.People) { <tr> <td><%: person.Name %></td> <td><%: person.Country %></td> </tr> } </table>

More languages and language features are also supported. For example, there is also support for the MVC flavour of Razor, which includes @Model markup. There is also support for Angular 2+ (including Angular 4) which augments standard HTML files to add tokens for Angular interpolation expressions. And there is also support for parsing HTML and HTML-like languages inside C# and JavaScript string literals.

ReSharper provides the following:

  • Language registration - HtmlLanguageService to act as a base class for the PSI Language Service.

  • Lexing - HtmlCompoundLexer and HtmlCompoundIncrementalLexer which interleaves lexing the custom parts of the language with standard HTML.

  • Parsing - IWebTreeBuilder and the WebTreeBuilder base class to parse the mix of custom and traditional HTML syntax. HTML-like languages use custom written parsers rather than parsers generated with PsiGen and .psi files. This also means that the PSI element types are also generated by hand - the syntax of HTML and its derived languages is small and regular enough that this is possible.

Last modified: 04 July 2023