Skip to content

Commit 8ca4ca1

Browse files
author
Michael Lyons
committed
WIP Razor
1 parent 40468be commit 8ca4ca1

File tree

4 files changed

+613
-14
lines changed

4 files changed

+613
-14
lines changed

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

0 commit comments

Comments
 (0)