Skip to content

Commit

Permalink
Merge pull request #78 from Thom1729/template-lookahead
Browse files Browse the repository at this point in the history
Add lookahead templates
  • Loading branch information
Thom1729 authored Aug 25, 2019
2 parents 40dca98 + f24910c commit b283f5c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const myStyle = style`div { color: red }`;

##### `comments`: object

Highlight tagged template literals based on a preceding block comment. Example configuration:
Highlight untagged template literals based on a preceding block comment. Example configuration:

```json
{
Expand All @@ -161,6 +161,30 @@ const myStyle = /*style*/`div { color: red }`;
const myStyle = /* style */`div { color: red }`;
```

##### `lookaheads`: object

Highlight untagged template literals based on the contents. Example configuration:

```json
{
"configurations": {
"My Config": {
"custom_templates": {
"lookaheads": {
"select\b": "scope:source.sql"
}
}
}
}
}
```

Example JavaScript:

```js
const myQuery = `select 1 from dual`;
```

##### `styled_components`: boolean

Highlight template string literals for [Styled Components](https://www.styled-components.com/).
Expand Down
9 changes: 9 additions & 0 deletions extensions/custom_templates/custom_templates.syntax-extension
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contexts: !merge
- match: (?=`)
set:
- include: literal-string-template-custom-comments
- include: literal-string-template-custom-lookahead
- include: literal-string-template

literal-string-template-custom-tags: !foreach
Expand All @@ -31,6 +32,14 @@ contexts: !merge
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

styled-components: !if
- !argument styled_components
- - match: (?=(?:styled|css|createGlobalStyle|injectGlobal|keyframes){{identifier_break}})
Expand Down
6 changes: 6 additions & 0 deletions tests/syntax_test_suites/templates/syntax_test_templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
// ^ punctuation.definition.string.begin
// ^ punctuation.definition.string.end

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

styled`color: red`
// ^^^^^^ variable.function.tagged-template
// ^ string.quoted.other punctuation.definition.string.begin
Expand Down
3 changes: 3 additions & 0 deletions tests/test_syntaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def test_templates(self):
'comments': {
'css': 'scope:source.css',
},
'lookaheads': {
r'select\b': 'scope:source.sql',
},
'styled_components': True,
}
},
Expand Down

0 comments on commit b283f5c

Please sign in to comment.