Skip to content

Commit 5acfa91

Browse files
Michael Lyonsmichaelblyons
authored andcommitted
WIP Razor
1 parent d9693e8 commit 5acfa91

5 files changed

Lines changed: 610 additions & 15 deletions

File tree

Razor/C# (Razor).sublime-syntax

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
%YAML 1.2
2+
---
3+
scope: source.cs.embedded.html
4+
hidden: true
5+
# version: 2
6+
7+
extends: Packages/C#/C#.sublime-syntax
8+
9+
##############################################################################
10+
11+
contexts:
12+
# code_block_in:
13+
# - meta_prepend: true
14+
# - match: (%>)(\s*\n)?
15+
# scope: meta.embedded.cs
16+
# captures:
17+
# 1: punctuation.section.embedded.end.cs-razor
18+
# 2: meta.html-newline-after-csharp.cs-razor # used by indentation rules
19+
# push: embedding-syntax
20+
21+
embedding-syntax:
22+
- clear_scopes: true
23+
- meta_scope: embedding.cs text.html.cs.razor
24+
- meta_include_prototype: false
25+
- match: <%(?![=#:$@-])
26+
scope: meta.embedded.cs punctuation.section.embedded.begin.cs-razor
27+
pop: 1
28+
- include: Packages/HTML (C#)/Razor/Razor.sublime-syntax#comments
29+
- include: Packages/HTML (C#)/Razor/Razor.sublime-syntax#html
30+
31+
stray-braces:
32+
- match: (\})(\s*\n)?
33+
scope: meta.embedded.cs
34+
captures:
35+
1: punctuation.section.embedded.end.cs-razor
36+
2: meta.html-newline-after-csharp.cs-razor # used by indentation rules
37+
# - match: \}
38+
# scope: invalid.illegal.stray.cs
39+
40+
stray-brackets:
41+
- match: \]
42+
scope: invalid.illegal.stray.cs
43+
44+
stray-parens:
45+
- match: \)
46+
scope: invalid.illegal.stray.cs

Razor/Razor.sublime-syntax

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
%YAML 1.2
2+
---
3+
# http://www.sublimetext.com/docs/syntax.html
4+
name: HTML (C# Razor)
5+
scope: text.html.cs.razor
6+
version: 2
7+
8+
extends: Packages/HTML/HTML.sublime-syntax
9+
10+
file_extensions:
11+
- cshtml
12+
13+
variables:
14+
csharp_tag: '@{'
15+
csharp_tag_end: '}'
16+
csharp_tag_interpolation_paren: '@\('
17+
csharp_tag_interpolation_bare: '@(?=\w)'
18+
19+
contexts:
20+
prototype:
21+
- meta_prepend: true
22+
- include: comments
23+
- include: csharp-embedded
24+
25+
main:
26+
- meta_include_prototype: false
27+
- match: ''
28+
push: [razor, directives]
29+
30+
razor:
31+
# This is the main Razor context.
32+
# It includes `csharp-tags` which are allowed to be closed by } tags at any
33+
# level of code-block on stack resuming with the next top-level @{ tag.
34+
- meta_include_prototype: false
35+
- include: csharp-tags
36+
- include: html
37+
38+
comments:
39+
- match: '@\*'
40+
scope: punctuation.definition.comment.block.begin.cs-razor
41+
push:
42+
- meta_scope: comment.block.cs-razor
43+
- match: '\*@'
44+
scope: punctuation.definition.comment.block.end.cs-razor
45+
pop: 1
46+
47+
###[ DIRECTIVES ]################################################################
48+
49+
directives:
50+
- meta_include_prototype: false
51+
- include: comments
52+
- include: directive
53+
- include: Packages/HTML/HTML (Plain).sublime-syntax#comment
54+
- match: (?=\S)
55+
pop: 1
56+
57+
directive:
58+
- match: (@)(model)\b
59+
captures:
60+
1: punctuation.section.embedded.line.cs-razor
61+
2: source.cs.embedded.html keyword.other.cs-razor
62+
embed: scope:source.cs#type
63+
embed_scope: source.cs.embedded.html
64+
escape: $
65+
- match: (@)(using)\b(?!\s*\()
66+
captures:
67+
1: punctuation.section.embedded.line.cs-razor
68+
2: source.cs.embedded.html keyword.other.cs-razor
69+
embed: scope:source.cs#line_of_code_in
70+
embed_scope: source.cs.embedded.html
71+
escape: $
72+
73+
###[ RAZOR CONSTRUCTS ]#######################################################
74+
75+
razor-constructs:
76+
- match: (@)(section)\b(?:\s+(\w+))?
77+
captures:
78+
1: punctuation.section.embedded.line.cs-razor
79+
2: source.cs.embedded.html keyword.other.cs-razor
80+
3: entity.name.section.cs-razor
81+
push:
82+
- match: \{
83+
scope: punctuation.section.block.begin.cs-razor
84+
set:
85+
- match: \}
86+
scope: punctuation.section.block.end.cs-razor
87+
pop: true
88+
- include: razor
89+
- match: (@)(functions)\b
90+
captures:
91+
1: punctuation.section.embedded.line.cs-razor
92+
2: source.cs.embedded.html keyword.other.cs-razor
93+
push:
94+
- match: \{
95+
scope: punctuation.section.block.begin.cs-razor
96+
set:
97+
- clear_scopes: 1
98+
- meta_scope: source.cs.embedded.functions.cs-razor
99+
- match: \}
100+
scope: punctuation.section.block.end.cs-razor
101+
pop: true
102+
- include: scope:source.cs#code_block_in
103+
- match: \s*(?!\{)(?=\S)
104+
scope: invalid.illegal.expected-block.cs-razor
105+
pop: true
106+
- match: '@@'
107+
scope: constant.character.escape.cs-razor
108+
- match: (@)(?=(?:if|switch|for|foreach|using|try|lock)\b)
109+
captures:
110+
1: punctuation.section.embedded.cs-razor
111+
push:
112+
- clear_scopes: 1
113+
- meta_content_scope: source.cs.embedded.html
114+
- include: scope:source.cs#line_of_code
115+
- match: ''
116+
pop: true
117+
with_prototype:
118+
- include: razor-block-prototype
119+
- match: '[\w-+]+@\w+' # ignore email addresses
120+
- match: '@(?=(?:\w+\.)*\w+(?:[<\s]|$))'
121+
scope: punctuation.section.embedded.line.cs-razor
122+
embed: scope:source.cs#line_of_code_in
123+
embed_scope: source.cs.embedded.html
124+
escape: $|(?=[<\s])
125+
- match: '@(?=(?:\w+\.)*\w+\()'
126+
scope: punctuation.section.embedded.line.cs-razor
127+
push:
128+
- meta_content_scope: source.cs.embedded.html
129+
- match: (?=[<\s])
130+
pop: true
131+
- include: scope:source.cs#line_of_code_in
132+
- match: '@\('
133+
scope: punctuation.section.embedded.begin.cs-razor
134+
push:
135+
- clear_scopes: 1
136+
- meta_content_scope: source.cs.embedded.html
137+
- match: \)
138+
scope: punctuation.section.embedded.end.cs-razor
139+
pop: true
140+
- include: scope:source.cs#line_of_code_in
141+
#- match: '@(?=\w+(?:\.\w+)*)(?!\s*[+=|(])' # TODO: support a.b[2] etc.
142+
# comment: property/variable access
143+
# scope: punctuation.section.embedded.line.cs-razor
144+
# #push: razor-block # TODO: stop at closing " etc.
145+
- match: '@'
146+
scope: punctuation.section.embedded.line.cs-razor
147+
push: razor-block
148+
149+
razor-block-prototype:
150+
- match: (?=[<&])
151+
embed: scope:text.html.basic
152+
escape: (?=[@}]|$)
153+
- match: '@:'
154+
scope: punctuation.section.embedded.html.cs-razor
155+
embed: scope:text.html.basic
156+
escape: $
157+
158+
eat-whitespace:
159+
- match: (?=\S)
160+
pop: true
161+
162+
razor-block:
163+
- clear_scopes: 1
164+
- meta_content_scope: source.cs.embedded.html
165+
- match: (?=[<\s])
166+
pop: true
167+
- match: (?=\S)(?!@)
168+
push:
169+
- include: scope:source.cs#line_of_code
170+
with_prototype:
171+
- include: razor-block-prototype
172+
173+
###[ HTML CONTENT ]###########################################################
174+
175+
tag-html:
176+
- meta_prepend: true
177+
- include: razor-constructs
178+
- include: csharp-interpolations
179+
180+
cdata-content:
181+
- meta_prepend: true
182+
- meta_include_prototype: false
183+
- include: csharp-interpolations
184+
185+
script-javascript-content:
186+
- meta_include_prototype: false
187+
- match: \s*((<!\[)(CDATA)(\[))
188+
captures:
189+
1: meta.tag.sgml.cdata.html
190+
2: punctuation.definition.tag.begin.html
191+
3: keyword.declaration.cdata.html
192+
4: punctuation.definition.tag.begin.html
193+
pop: 1 # make sure to match only once
194+
embed: scope:source.js.cs.razor
195+
embed_scope: meta.tag.sgml.cdata.html source.js.embedded.html
196+
escape: \]\]>
197+
escape_captures:
198+
0: meta.tag.sgml.cdata.html punctuation.definition.tag.end.html
199+
- match: '{{script_content_begin}}'
200+
captures:
201+
1: comment.block.html punctuation.definition.comment.begin.html
202+
pop: 1 # make sure to match only once
203+
embed: scope:source.js.cs.razor
204+
embed_scope: source.js.embedded.html
205+
escape: '{{script_content_end}}'
206+
escape_captures:
207+
1: source.js.embedded.html
208+
2: comment.block.html punctuation.definition.comment.end.html
209+
3: source.js.embedded.html
210+
4: comment.block.html punctuation.definition.comment.end.html
211+
212+
style-css-content:
213+
- meta_include_prototype: false
214+
- match: \s*((<!\[)(CDATA)(\[))
215+
captures:
216+
1: meta.tag.sgml.cdata.html
217+
2: punctuation.definition.tag.begin.html
218+
3: keyword.declaration.cdata.html
219+
4: punctuation.definition.tag.begin.html
220+
pop: 1 # make sure to match only once
221+
embed: scope:source.css.cs.razor
222+
embed_scope: meta.tag.sgml.cdata.html source.css.embedded.html
223+
escape: \]\]>
224+
escape_captures:
225+
0: meta.tag.sgml.cdata.html punctuation.definition.tag.end.html
226+
- match: '{{style_content_begin}}'
227+
captures:
228+
1: comment.block.html punctuation.definition.comment.begin.html
229+
pop: 1 # make sure to match only once
230+
embed: scope:source.css.cs.razor
231+
embed_scope: source.css.embedded.html
232+
escape: '{{style_content_end}}'
233+
escape_captures:
234+
1: source.css.embedded.html
235+
2: comment.block.html punctuation.definition.comment.end.html
236+
3: source.css.embedded.html
237+
4: comment.block.html punctuation.definition.comment.end.html
238+
239+
tag-event-attribute-value:
240+
- match: \"
241+
scope: meta.string.html string.quoted.double.html punctuation.definition.string.begin.html
242+
embed: scope:source.js.cs.razor
243+
embed_scope: meta.string.html meta.interpolation.html source.js.embedded.html
244+
escape: \"
245+
escape_captures:
246+
0: meta.string.html string.quoted.double.html punctuation.definition.string.end.html
247+
- match: \'
248+
scope: meta.string.html string.quoted.single.html punctuation.definition.string.begin.html
249+
embed: scope:source.js.cs.razor
250+
embed_scope: meta.string.html meta.interpolation.html source.js.embedded.html
251+
escape: \'
252+
escape_captures:
253+
0: meta.string.html string.quoted.single.html punctuation.definition.string.end.html
254+
- include: else-pop
255+
256+
tag-style-attribute-value:
257+
- match: \"
258+
scope: meta.string.html string.quoted.double.html punctuation.definition.string.begin.html
259+
embed: scope:source.css.cs.razor#rule-list-body
260+
embed_scope: meta.string.html meta.interpolation.html source.css.embedded.html
261+
escape: \"
262+
escape_captures:
263+
0: meta.string.html string.quoted.double.html punctuation.definition.string.end.html
264+
- match: \'
265+
scope: meta.string.html string.quoted.single.html punctuation.definition.string.begin.html
266+
embed: scope:source.css.cs.razor#rule-list-body
267+
embed_scope: meta.string.html meta.interpolation.html source.css.embedded.html
268+
escape: \'
269+
escape_captures:
270+
0: meta.string.html string.quoted.single.html punctuation.definition.string.end.html
271+
- include: else-pop
272+
273+
tag-attribute-value-content:
274+
- meta_prepend: true
275+
- include: csharp-interpolations
276+
277+
strings-common-content:
278+
- meta_prepend: true
279+
- include: csharp-interpolations
280+
281+
###[ C# TAGS ]###############################################################
282+
283+
csharp-tags:
284+
# - include: php-tag-incomplete
285+
- include: comments
286+
- match: '{{csharp_tag}}'
287+
scope: punctuation.section.embedded.begin.cs-razor
288+
push: csharp-tag-content
289+
290+
csharp-tag-content:
291+
- meta_include_prototype: false
292+
- meta_scope: meta.embedded.cs
293+
- meta_content_scope: source.cs.embedded.html
294+
- match: ({{csharp_tag_end}})(\s*\n)?
295+
captures:
296+
1: punctuation.section.embedded.end.cs-razor
297+
2: meta.html-newline-after-csharp.cs-razor # used by indentation rules
298+
pop: 1
299+
- include: Packages/HTML (C#)/Razor/C# (Razor).sublime-syntax
300+
apply_prototype: true
301+
302+
csharp-interpolations:
303+
- meta_include_prototype: false
304+
- match: '@@'
305+
scope: constant.character.escape.cs-razor
306+
- match: (?={{csharp_tag_interpolation_bare}})
307+
push: csharp-interpolation-body
308+
309+
csharp-interpolation-body:
310+
# - clear_scopes: 1
311+
- meta_include_prototype: false
312+
- include: csharp-embedded
313+
- include: immediately-pop
314+
315+
csharp-embedded:
316+
- meta_include_prototype: false
317+
- match: '{{csharp_tag_interpolation_bare}}'
318+
scope: meta.embedded.cs punctuation.section.embedded.begin.cs-razor
319+
embed: Packages/C#/C#.sublime-syntax
320+
embed_scope: meta.embedded.cs source.cs.embedded.html
321+
escape: ((?=")|\s*\n)?
322+
escape_captures:
323+
0: meta.embedded.cs
324+
# 1: punctuation.section.embedded.end.cs-razor
325+
1: meta.html-newline-after-csharp.cs-razor
326+
- match: '{{csharp_tag_interpolation_paren}}'
327+
scope: meta.embedded.cs punctuation.section.embedded.begin.cs-razor
328+
embed: Packages/C#/C#.sublime-syntax
329+
embed_scope: meta.embedded.cs source.cs.embedded.html
330+
escape: (\))(\s*\n)?
331+
escape_captures:
332+
0: meta.embedded.cs
333+
1: punctuation.section.embedded.end.cs-razor
334+
2: meta.html-newline-after-csharp.cs-razor

Razor/Razor.x.sublime-syntax

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%YAML 1.2
22
---
33
# http://www.sublimetext.com/docs/3/syntax.html
4-
name: HTML Razor (C#)
4+
name: HTML (C# Razor) OLD
55
file_extensions:
66
- cshtml
77
scope: embedding.cshtml.razor

0 commit comments

Comments
 (0)