Skip to content

Commit c1d86ce

Browse files
committed
Refactor custom templates.
1 parent 33de7a5 commit c1d86ce

File tree

5 files changed

+119
-67
lines changed

5 files changed

+119
-67
lines changed

extensions/custom_templates/custom_templates.syntax-extension

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,66 @@
22
%TAG ! tag:yaml-macros:yamlmacros.lib.arguments,yamlmacros.lib.include:
33
---
44
contexts:
5-
literal-string-template:
5+
expression-begin:
66
- meta_prepend: true
7-
- include: literal-string-template-custom-comments
87
- include: literal-string-template-custom-tags
98
- include: styled-components
109

11-
expression-begin:
12-
- meta_prepend: true
13-
# Keep the slow backreferences out of this frequently-used context.
1410
- match: (?=`)
1511
set:
12+
# Keep the slow lookbehinds out of this frequently-used context.
1613
- include: literal-string-template-custom-comments
17-
- include: literal-string-template-custom-lookahead
1814
- include: custom-templates-default
15+
- include: else-pop
1916

20-
custom-templates-default: !if
21-
- !argument default
22-
- !with
23-
- include: !argument default
24-
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
25-
- - include: literal-string-template
17+
custom-templates-default:
18+
- match: '`'
19+
scope: string.quoted.other.js punctuation.definition.string.begin.js
20+
push:
21+
- - include: immediately-pop
22+
- custom-templates-closing-quote
23+
- custom-templates-clear-scopes
24+
- - meta_include_prototype: false
25+
- include: literal-string-template-custom-lookahead
26+
- !if
27+
- !argument default
28+
- match: (?=\s*\S)
29+
set: !argument default
30+
with_prototype:
31+
- include: custom-templates-prototype
32+
- match: (?=\s*\S)
33+
set:
34+
- meta_scope: string.quoted.other.js
35+
- meta_include_prototype: false
36+
- include: custom-templates-prototype
37+
38+
literal-string-template-custom-lookahead: !foreach
39+
in: !argument [lookaheads, {}]
40+
as: [lookahead, include]
41+
value:
42+
match: !format '(?=\s*(?:{lookahead}))'
43+
set: !argument include
44+
with_prototype:
45+
- include: custom-templates-prototype
2646

2747
literal-string-template-custom-tags: !foreach
2848
in: !argument [tags, {}]
2949
as: [tag, include]
3050
value:
3151
match: !format '(?:{tag})(?=\s*`)'
3252
scope: variable.function.tagged-template.js
33-
set: !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
53+
set:
54+
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
55+
- include: else-pop
3456

3557
literal-string-template-custom-comments: !foreach
3658
in: !argument [comments, {}]
3759
as: [tag, include]
3860
value:
3961
match: !format '(?<=/\*(?:{tag})\*/|/\* (?:{tag}) \*/)'
40-
scope: variable.function.tagged-template.js
41-
set: !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
42-
43-
literal-string-template-custom-lookahead: !foreach
44-
in: !argument [lookaheads, {}]
45-
as: [lookahead, include]
46-
value:
47-
match: !format '(?=`(?:{lookahead}))'
48-
scope: variable.function.tagged-template.js
49-
set: !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
62+
set:
63+
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
64+
- include: else-pop
5065

5166
styled-components: !if
5267
- !argument styled_components
@@ -65,18 +80,16 @@ contexts:
6580
scope: punctuation.accessor.dot.js
6681
push: styled-component-begin
6782

68-
- match: (?=`)
69-
set: !with
70-
- include: scope:source.js.css
71-
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
83+
- !with
84+
- include: scope:source.js.css
85+
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
7286

7387
- include: expression-end
7488

7589
styled-component-keyframes-end:
76-
- match: (?=`)
77-
push: !with
78-
- include: scope:source.js.css#at-keyframe-block-content
79-
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
90+
- !with
91+
- include: scope:source.js.css#at-keyframe-block-content
92+
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
8093
- include: else-pop
8194

8295
styled-component-begin:
@@ -95,3 +108,32 @@ contexts:
95108
pop: true
96109

97110
- include: else-pop
111+
112+
custom-templates-closing-quote:
113+
- meta_include_prototype: false
114+
- meta_scope: meta.string.js string.quoted.other.js
115+
- match: "`"
116+
scope: punctuation.definition.string.end.js
117+
pop: true
118+
- include: immediately-pop
119+
120+
custom-templates-clear-scopes:
121+
- meta_include_prototype: false
122+
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
123+
- include: immediately-pop
124+
125+
custom-templates-prototype:
126+
- match: (?=`)
127+
pop: true
128+
- match: '\$\{'
129+
scope: punctuation.section.interpolation.begin.js
130+
push:
131+
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
132+
- meta_scope: meta.interpolation.js
133+
- meta_content_scope: source.js.embedded.expression
134+
- match: '\}'
135+
scope: punctuation.section.interpolation.end.js
136+
pop: true
137+
- match: (?=\S)
138+
push: expression
139+
- include: string-content
Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
11
%YAML 1.2
22
%TAG ! tag:yaml-macros:yamlmacros.lib.arguments:
33
---
4-
- match: '`'
5-
scope: string.quoted.other.js punctuation.definition.string.begin.js
6-
push:
7-
- - meta_include_prototype: false
8-
- meta_scope: meta.string.js string.quoted.other.js
9-
- match: "`"
10-
scope: punctuation.definition.string.end.js
11-
pop: true
12-
- include: immediately-pop
13-
- - meta_include_prototype: false
14-
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
15-
- include: immediately-pop
16-
- - meta_include_prototype: false
17-
- match: ''
18-
set: !argument include
19-
with_prototype:
20-
- match: (?=`)
21-
pop: true
22-
- match: '\$\{'
23-
scope: punctuation.section.interpolation.begin.js
24-
push:
25-
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
26-
- meta_scope: meta.interpolation.js
27-
- meta_content_scope: source.js.embedded.expression
28-
- match: '\}'
29-
scope: punctuation.section.interpolation.end.js
30-
pop: true
31-
- match: (?=\S)
32-
push: expression
33-
- include: string-content
34-
- include: else-pop
4+
match: '`'
5+
scope: string.quoted.other.js punctuation.definition.string.begin.js
6+
push:
7+
- - include: immediately-pop
8+
- custom-templates-closing-quote
9+
- custom-templates-clear-scopes
10+
- - meta_include_prototype: false
11+
- match: ''
12+
set: !argument include
13+
with_prototype:
14+
- include: custom-templates-prototype
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
`<foo></foo>`;
3+
// ^^^^^^^^^^^^^ meta.string
4+
// ^^^^^^^^^^^ text.html.basic - string

tests/syntax_test_suites/templates/templates.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
// ^^^^^^^^^^^^^^^^^^^^ meta.string
2222
// ^ string.quoted.other punctuation.definition.string.begin
2323
// ^^^^^^^^^^^^^^^^^^ source.sql - string
24+
// ^ string.quoted.other punctuation.definition.string.end
25+
26+
`
27+
select * from dual`;
28+
//^^^^^^^^^^^^^^^^^^^^^^ meta.string
29+
//^^^^^^^^^^^^^^^^^^^^^ source.sql - string
2430
// ^ string.quoted.other punctuation.definition.string.end
2531

2632
/*css*/`a { color: red; }`
@@ -36,6 +42,3 @@
3642
// ^^^^^^^^^^^^^^^^^^^^^^ meta.string
3743
// ^^^^^^^^^^^^^^^^^^^^ source.css - string
3844

39-
`<foo></foo>`;
40-
// ^^^^^^^^^^^^^ meta.string
41-
// ^^^^^^^^^^^ text.html.basic - string

tests/test_syntaxes.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_flow(self):
9090
tests=["js", "flow"],
9191
)
9292

93-
def test_templates(self):
93+
def test_templates_default(self):
9494
yield from self._test_syntaxes(
9595
name="templates",
9696
configuration={
@@ -111,7 +111,30 @@ def test_templates(self):
111111
}
112112
},
113113
tests=["js", "templates"],
114-
exclude=["js.js"]
114+
exclude=["js.js"],
115+
)
116+
117+
def test_templates_no_default(self):
118+
yield from self._test_syntaxes(
119+
name="templates_no_default",
120+
configuration={
121+
"file_extensions": [],
122+
"hidden": True,
123+
"custom_templates": {
124+
'tags': {
125+
'css': 'scope:source.css',
126+
},
127+
'comments': {
128+
'css': 'scope:source.css',
129+
},
130+
'lookaheads': {
131+
r'select\b': 'scope:source.sql',
132+
},
133+
'styled_components': True,
134+
}
135+
},
136+
tests=["js", "templates"],
137+
exclude=["template_default.js"],
115138
)
116139

117140
def test_string_object_keys(self):

0 commit comments

Comments
 (0)