Skip to content

Add Razor syntax highlighting for .razor and .cshtml - #113

Open
bancho4560 wants to merge 1 commit into
zed-extensions:mainfrom
bancho4560:razor-highlighting
Open

bancho4560 wants to merge 1 commit into
zed-extensions:mainfrom
bancho4560:razor-highlighting

Conversation

@bancho4560

Copy link
Copy Markdown

Summary

Adds syntax highlighting for .razor and .cshtml files, addressing #41.

This is highlighting only: no language server, completion, or diagnostics are attached to Razor buffers. Roslyn/Razor LSP support (as proposed in #110) is intentionally out of scope.

What's included

  • A Razor language with path_suffixes = ["razor", "cshtml"], Razor block comments (@* ... *@), and @ as a word character.
  • Highlighting for directives (@page, @model, @using, @inject, @section, ...), transitions (@if, @foreach, @try, @:, @( ... ), @code, ...), tag helpers (@onclick, @bind-Value, ...), Razor comments, and doctypes.
  • Embedded C# highlighting inside expressions and code blocks.
  • Markup highlighting through Zed's HTML language, including the HTML extension's CSS/JS injections for <style> and <script>.
  • Bracket matching and indentation consistent with the C# language.

Grammar

alfpricot/tree-sitter-razor-modern at fa9264bcc1af01c260a28749f180ac5577a40fea.

The grammar parses current Razor/Blazor syntax with zero ERROR nodes in my test corpus, including @page with and without a route, <!DOCTYPE>, @media inside <style>, ${...} inside <script>, tag helpers, @@, @:, and @* ... *@. In the same tests, the tris203/Meeksoft grammar lineage used in #103/#110 produced ERROR nodes on several of those constructs.

Implementation notes

  • Zed does not support ; inherits: c_sharp, so the C# highlight rules are ported into languages/razor/highlights.scm. The one deliberate difference is that <, >, = and other comparison/assignment operators are scoped to expression nodes; a bare token list would paint HTML tags and attributes.
  • The razor grammar exposes markup as a single opaque element node, so markup cannot be captured directly. injections.scm reparses top-level elements with Zed's bundled HTML language. injection.combined is intentionally not used: nested elements would produce overlapping included ranges and the layer would be rejected.
  • CSS/JS in <style>/<script> still highlight through the HTML extension's own injections.

Verification

  • All queries compile against the pinned grammar.
  • Test files (MVC view, Razor Page, Blazor component, directive edge cases) parse with zero ERROR nodes.
  • Markup is not painted by C# operator rules; =, >, => captures occur only inside Razor/C# expressions.
  • Installed as a dev extension in Zed 1.31; the grammar builds and highlighting works for .cshtml and .razor.
  • .cs files, their highlighting, and their language servers are unchanged.

Scope / limitations

  • No Razor LSP; that needs the Roslyn aspnetcorerazor registration and should be a separate change.
  • Razor expression punctuation inside attribute values can take the HTML string color; the expression tokens themselves keep their C# colors.
  • The grammar does not expose tag tokens, so indentation/outline are bracket-based (same as C#), not tag-aware.

Register the razor grammar and a Razor language, highlight Razor directives and transitions, and port the C# query set for embedded code. Markup is highlighted by injecting Zed's HTML language over top-level elements, which also supplies CSS/JS highlighting inside <style> and <script>.

This is syntax highlighting only; no language server is attached to Razor buffers.
@cla-bot cla-bot Bot added the cla-signed label Dec 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant