Let me know if you want upgrades.
Note: This project made with AI. It does work. Use with caution. I quickly build this project (in 1 hour) just for using ZED with Blazor-Razor pages. It lacks structure, coding standarts, and every other professional aspect. Just an AI slop
A Tree-sitter grammar for ASP.NET Razor Pages (.razor, .cshtml).
This grammar provides robust parsing for the Razor syntax, including:
- Directives (
@page,@inject,@code, etc.) - Control structures (
@if,@foreach,@switch, etc.) - Explicit and implicit C# expressions (
@(...),@someVar) - HTML integration with attributes
- C# code block injection
- Accurate Parsing: Handles mixed C# and HTML content typical in Razor files.
- Syntax Highlighting: Includes queries for syntax highlighting.
- Injection Support: seamless C# injection inside
@codeblocks and control structures. - Zed Editor Support: Ready-to-use integration for the Zed editor.
- Node.js (v18+)
- C Compiler (MSVC on Windows, GCC/Clang on Linux/macOS)
- Tree-sitter CLI (
npm install -g tree-sitter-cli)
-
Clone the repository:
git clone https://github.com/yourusername/tree-sitter-razor.git cd tree-sitter-razor -
Install dependencies:
npm install
-
Build the grammar:
npm run build # or tree-sitter generate
Run the corpus tests to verify the grammar:
# Standard test run
tree-sitter test
# Windows specific helper (sets up VS environment)
.\run_test.batTo parse a specific file and see the syntax tree:
tree-sitter parse test/counter.razorThis repository includes a local extension setup for Zed.
- Navigate to the
zed-razordirectory. - Ensure you have a compiled WASM grammar:
tree-sitter build --wasm copy tree-sitter-razor.wasm zed-razor\grammars\razor.wasm
- Open Zed and install the extension as a "Dev Extension" pointing to the
zed-razorfolder.
zed-razor/extension.toml: Extension manifest.zed-razor/languages/razor/: Language configuration and queries.zed-razor/grammars/razor.wasm: Compiled Tree-sitter grammar.
grammar.js: The Tree-sitter grammar definition.src/: Generated parser source code (C).queries/: Tree-sitter queries for highlighting and injections.test/corpus/: Test cases for the grammar.zed-razor/: Zed extension configuration files.
MIT
