Skip to content

Commit

Permalink
Refactor custom templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom1729 committed Feb 26, 2022
1 parent 33de7a5 commit c1d86ce
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 67 deletions.
104 changes: 73 additions & 31 deletions extensions/custom_templates/custom_templates.syntax-extension
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,66 @@
%TAG ! tag:yaml-macros:yamlmacros.lib.arguments,yamlmacros.lib.include:
---
contexts:
literal-string-template:
expression-begin:
- meta_prepend: true
- include: literal-string-template-custom-comments
- include: literal-string-template-custom-tags
- include: styled-components

expression-begin:
- meta_prepend: true
# Keep the slow backreferences out of this frequently-used context.
- match: (?=`)
set:
# Keep the slow lookbehinds out of this frequently-used context.
- include: literal-string-template-custom-comments
- include: literal-string-template-custom-lookahead
- include: custom-templates-default
- include: else-pop

custom-templates-default: !if
- !argument default
- !with
- include: !argument default
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
- - include: literal-string-template
custom-templates-default:
- match: '`'
scope: string.quoted.other.js punctuation.definition.string.begin.js
push:
- - include: immediately-pop
- custom-templates-closing-quote
- custom-templates-clear-scopes
- - meta_include_prototype: false
- include: literal-string-template-custom-lookahead
- !if
- !argument default
- match: (?=\s*\S)
set: !argument default
with_prototype:
- include: custom-templates-prototype
- match: (?=\s*\S)
set:
- meta_scope: string.quoted.other.js
- meta_include_prototype: false
- include: custom-templates-prototype

literal-string-template-custom-lookahead: !foreach
in: !argument [lookaheads, {}]
as: [lookahead, include]
value:
match: !format '(?=\s*(?:{lookahead}))'
set: !argument include
with_prototype:
- include: custom-templates-prototype

literal-string-template-custom-tags: !foreach
in: !argument [tags, {}]
as: [tag, include]
value:
match: !format '(?:{tag})(?=\s*`)'
scope: variable.function.tagged-template.js
set: !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
set:
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
- include: else-pop

literal-string-template-custom-comments: !foreach
in: !argument [comments, {}]
as: [tag, include]
value:
match: !format '(?<=/\*(?:{tag})\*/|/\* (?:{tag}) \*/)'
scope: variable.function.tagged-template.js
set: !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml

literal-string-template-custom-lookahead: !foreach
in: !argument [lookaheads, {}]
as: [lookahead, include]
value:
match: !format '(?=`(?:{lookahead}))'
scope: variable.function.tagged-template.js
set: !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
set:
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
- include: else-pop

styled-components: !if
- !argument styled_components
Expand All @@ -65,18 +80,16 @@ contexts:
scope: punctuation.accessor.dot.js
push: styled-component-begin

- match: (?=`)
set: !with
- include: scope:source.js.css
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
- !with
- include: scope:source.js.css
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml

- include: expression-end

styled-component-keyframes-end:
- match: (?=`)
push: !with
- include: scope:source.js.css#at-keyframe-block-content
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
- !with
- include: scope:source.js.css#at-keyframe-block-content
- !include_resource Packages/JSCustom/extensions/custom_templates/template.yaml
- include: else-pop

styled-component-begin:
Expand All @@ -95,3 +108,32 @@ contexts:
pop: true

- include: else-pop

custom-templates-closing-quote:
- meta_include_prototype: false
- meta_scope: meta.string.js string.quoted.other.js
- match: "`"
scope: punctuation.definition.string.end.js
pop: true
- include: immediately-pop

custom-templates-clear-scopes:
- meta_include_prototype: false
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
- include: immediately-pop

custom-templates-prototype:
- match: (?=`)
pop: true
- match: '\$\{'
scope: punctuation.section.interpolation.begin.js
push:
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
- meta_scope: meta.interpolation.js
- meta_content_scope: source.js.embedded.expression
- match: '\}'
scope: punctuation.section.interpolation.end.js
pop: true
- match: (?=\S)
push: expression
- include: string-content
42 changes: 11 additions & 31 deletions extensions/custom_templates/template.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
%YAML 1.2
%TAG ! tag:yaml-macros:yamlmacros.lib.arguments:
---
- match: '`'
scope: string.quoted.other.js punctuation.definition.string.begin.js
push:
- - meta_include_prototype: false
- meta_scope: meta.string.js string.quoted.other.js
- match: "`"
scope: punctuation.definition.string.end.js
pop: true
- include: immediately-pop
- - meta_include_prototype: false
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
- include: immediately-pop
- - meta_include_prototype: false
- match: ''
set: !argument include
with_prototype:
- match: (?=`)
pop: true
- match: '\$\{'
scope: punctuation.section.interpolation.begin.js
push:
- clear_scopes: !if [!argument clear_all_scopes, true, 1]
- meta_scope: meta.interpolation.js
- meta_content_scope: source.js.embedded.expression
- match: '\}'
scope: punctuation.section.interpolation.end.js
pop: true
- match: (?=\S)
push: expression
- include: string-content
- include: else-pop
match: '`'
scope: string.quoted.other.js punctuation.definition.string.begin.js
push:
- - include: immediately-pop
- custom-templates-closing-quote
- custom-templates-clear-scopes
- - meta_include_prototype: false
- match: ''
set: !argument include
with_prototype:
- include: custom-templates-prototype
4 changes: 4 additions & 0 deletions tests/syntax_test_suites/templates/template_default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

`<foo></foo>`;
// ^^^^^^^^^^^^^ meta.string
// ^^^^^^^^^^^ text.html.basic - string
9 changes: 6 additions & 3 deletions tests/syntax_test_suites/templates/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
// ^^^^^^^^^^^^^^^^^^^^ meta.string
// ^ string.quoted.other punctuation.definition.string.begin
// ^^^^^^^^^^^^^^^^^^ source.sql - string
// ^ string.quoted.other punctuation.definition.string.end

`
select * from dual`;
//^^^^^^^^^^^^^^^^^^^^^^ meta.string
//^^^^^^^^^^^^^^^^^^^^^ source.sql - string
// ^ string.quoted.other punctuation.definition.string.end

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

`<foo></foo>`;
// ^^^^^^^^^^^^^ meta.string
// ^^^^^^^^^^^ text.html.basic - string
27 changes: 25 additions & 2 deletions tests/test_syntaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_flow(self):
tests=["js", "flow"],
)

def test_templates(self):
def test_templates_default(self):
yield from self._test_syntaxes(
name="templates",
configuration={
Expand All @@ -111,7 +111,30 @@ def test_templates(self):
}
},
tests=["js", "templates"],
exclude=["js.js"]
exclude=["js.js"],
)

def test_templates_no_default(self):
yield from self._test_syntaxes(
name="templates_no_default",
configuration={
"file_extensions": [],
"hidden": True,
"custom_templates": {
'tags': {
'css': 'scope:source.css',
},
'comments': {
'css': 'scope:source.css',
},
'lookaheads': {
r'select\b': 'scope:source.sql',
},
'styled_components': True,
}
},
tests=["js", "templates"],
exclude=["template_default.js"],
)

def test_string_object_keys(self):
Expand Down

0 comments on commit c1d86ce

Please sign in to comment.