From a143796b72e046044506d62fd9f03282443a89dc Mon Sep 17 00:00:00 2001 From: Thomas Smith Date: Tue, 20 Aug 2019 10:59:31 -0400 Subject: [PATCH 1/2] Add lookahead templates. --- .../custom_templates/custom_templates.syntax-extension | 9 +++++++++ .../templates/syntax_test_templates.js | 6 ++++++ tests/test_syntaxes.py | 3 +++ 3 files changed, 18 insertions(+) diff --git a/extensions/custom_templates/custom_templates.syntax-extension b/extensions/custom_templates/custom_templates.syntax-extension index f1bd444..c05b0ab 100644 --- a/extensions/custom_templates/custom_templates.syntax-extension +++ b/extensions/custom_templates/custom_templates.syntax-extension @@ -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 @@ -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}}) diff --git a/tests/syntax_test_suites/templates/syntax_test_templates.js b/tests/syntax_test_suites/templates/syntax_test_templates.js index 18c517a..4f56f32 100644 --- a/tests/syntax_test_suites/templates/syntax_test_templates.js +++ b/tests/syntax_test_suites/templates/syntax_test_templates.js @@ -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 diff --git a/tests/test_syntaxes.py b/tests/test_syntaxes.py index 8bbd6f0..7b84a4f 100644 --- a/tests/test_syntaxes.py +++ b/tests/test_syntaxes.py @@ -102,6 +102,9 @@ def test_templates(self): 'comments': { 'css': 'scope:source.css', }, + 'lookaheads': { + r'select\b': 'scope:source.sql', + }, 'styled_components': True, } }, From f24910cd1752d45b0edf3c719125dab8fdc7bb59 Mon Sep 17 00:00:00 2001 From: Thomas Smith Date: Tue, 20 Aug 2019 11:03:02 -0400 Subject: [PATCH 2/2] Add docs. --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 119f38e..391f18c 100644 --- a/README.md +++ b/README.md @@ -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 { @@ -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/).