-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JavaScript] Basic support for syntax highlighting in tagged templates (
#4019) * [JavaScript] Syntax highlighting in tagged templates This commit adds support for syntax highlighting of CSS/JS/JSON/HTML within tagged template strings. * [JavaScript] Add fallback for unknown tags * [JavaScript] Fix unknown tagged templates This commit... 1. adds a `literal-string-templates` context to include templates into contexts without popping it off stack. 2. adjusts tag scopes to already existing `variable.function.tagged-template`. 3. adds support for whitespace between template tag and punctuation 4. drops supportt for block quotes between template tag and punctuation for simplicity reasons. It's jugdged unlikely enough to appear in real world code. * [JavaScript] Auto-indent tagged template string content This commit adjusts syntax definition and indentation rules to ensure content of tagged templates is indented, automatically. It includes consuming all trailing/leading whitespace after opening and in front of closing string punctuation, to ensure JavaScript indentation rules being applied, when hitting enter after an opening template punctuation. * [JavaScript] Improve embedded scope source boundaries. This commit strips embedded source scope after opening and before closing string punctuation only, if those are located on a separate line. In single line tagged template strings, embedded scope is applied to leading and trailing whitespace as well.
- Loading branch information
Showing
9 changed files
with
587 additions
and
20 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
JavaScript/Embeddings/CSS (for JS template).sublime-syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
%YAML 1.2 | ||
--- | ||
# http://www.sublimetext.com/docs/syntax.html | ||
# highlight tagged template strings | ||
scope: source.css.js-template | ||
version: 2 | ||
hidden: true | ||
|
||
extends: Packages/CSS/CSS.sublime-syntax | ||
|
||
variables: | ||
|
||
ident_start: (?:{{nmstart}}|\${) | ||
|
||
contexts: | ||
|
||
prototype: | ||
- meta_prepend: true | ||
- include: scope:source.js#text-interpolations | ||
|
||
strings-content: | ||
- meta_prepend: true | ||
- include: scope:source.js#string-interpolations |
122 changes: 122 additions & 0 deletions
122
JavaScript/Embeddings/HTML (for JS template).sublime-syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
%YAML 1.2 | ||
--- | ||
# http://www.sublimetext.com/docs/syntax.html | ||
# highlight tagged template strings | ||
scope: text.html.js-template | ||
version: 2 | ||
hidden: true | ||
|
||
extends: Packages/HTML/HTML.sublime-syntax | ||
|
||
variables: | ||
|
||
tag_name_start: (?:[A-Za-z]|\${) | ||
|
||
contexts: | ||
|
||
prototype: | ||
- meta_prepend: true | ||
- include: scope:source.js#text-interpolations | ||
|
||
cdata-content: | ||
- meta_prepend: true | ||
- include: scope:source.js#string-interpolations | ||
|
||
script-javascript-content: | ||
- meta_include_prototype: false | ||
- match: '{{script_content_begin}}' | ||
captures: | ||
1: comment.block.html punctuation.definition.comment.begin.html | ||
pop: 1 # make sure to match only once | ||
embed: scope:source.js.js-template | ||
embed_scope: source.js.embedded.html | ||
escape: '{{script_content_end}}' | ||
escape_captures: | ||
1: source.js.embedded.html | ||
2: comment.block.html punctuation.definition.comment.end.html | ||
3: source.js.embedded.html | ||
4: comment.block.html punctuation.definition.comment.end.html | ||
|
||
script-json-content: | ||
- meta_include_prototype: false | ||
- match: '{{script_content_begin}}' | ||
captures: | ||
1: comment.block.html punctuation.definition.comment.begin.html | ||
pop: 1 # make sure to match only once | ||
embed: scope:source.json.js-template | ||
embed_scope: source.json.embedded.html | ||
escape: '{{script_content_end}}' | ||
escape_captures: | ||
1: source.json.embedded.html | ||
2: comment.block.html punctuation.definition.comment.end.html | ||
3: source.json.embedded.html | ||
4: comment.block.html punctuation.definition.comment.end.html | ||
|
||
style-css-content: | ||
- meta_include_prototype: false | ||
- match: '{{style_content_begin}}' | ||
captures: | ||
1: comment.block.html punctuation.definition.comment.begin.html | ||
pop: 1 # make sure to match only once | ||
embed: scope:source.css.js-template | ||
embed_scope: source.css.embedded.html | ||
escape: '{{style_content_end}}' | ||
escape_captures: | ||
1: source.css.embedded.html | ||
2: comment.block.html punctuation.definition.comment.end.html | ||
3: source.css.embedded.html | ||
4: comment.block.html punctuation.definition.comment.end.html | ||
|
||
tag-event-attribute-value: | ||
- match: \" | ||
scope: | ||
meta.string.html string.quoted.double.html | ||
punctuation.definition.string.begin.html | ||
embed: scope:source.js.js-template | ||
embed_scope: meta.string.html meta.embedded.html source.js.embedded.html | ||
escape: \" | ||
escape_captures: | ||
0: meta.string.html string.quoted.double.html | ||
punctuation.definition.string.end.html | ||
- match: \' | ||
scope: | ||
meta.string.html string.quoted.single.html | ||
punctuation.definition.string.begin.html | ||
embed: scope:source.js.js-template | ||
embed_scope: meta.string.html meta.embedded.html source.js.embedded.html | ||
escape: \' | ||
escape_captures: | ||
0: meta.string.html string.quoted.single.html | ||
punctuation.definition.string.end.html | ||
- include: else-pop | ||
|
||
tag-style-attribute-value: | ||
- match: \" | ||
scope: | ||
meta.string.html string.quoted.double.html | ||
punctuation.definition.string.begin.html | ||
embed: scope:source.css.js-template#rule-list-body | ||
embed_scope: meta.string.html meta.embedded.html source.css.embedded.html | ||
escape: \" | ||
escape_captures: | ||
0: meta.string.html string.quoted.double.html | ||
punctuation.definition.string.end.html | ||
- match: \' | ||
scope: | ||
meta.string.html string.quoted.single.html | ||
punctuation.definition.string.begin.html | ||
embed: scope:source.css.js-template#rule-list-body | ||
embed_scope: meta.string.html meta.embedded.html source.css.embedded.html | ||
escape: \' | ||
escape_captures: | ||
0: meta.string.html string.quoted.single.html | ||
punctuation.definition.string.end.html | ||
- include: else-pop | ||
|
||
tag-attribute-value-content: | ||
- meta_prepend: true | ||
- include: scope:source.js#string-interpolations | ||
|
||
strings-common-content: | ||
- meta_prepend: true | ||
- include: scope:source.js#string-interpolations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
%YAML 1.2 | ||
--- | ||
# http://www.sublimetext.com/docs/syntax.html | ||
# highlight tagged template strings | ||
scope: source.json.js-template | ||
version: 2 | ||
hidden: true | ||
|
||
extends: Packages/JSON/JSON.sublime-syntax | ||
|
||
contexts: | ||
prototype: | ||
- meta_prepend: true | ||
- include: scope:source.js#text-interpolations | ||
|
||
string-prototype: | ||
- meta_prepend: true | ||
- include: scope:source.js#string-interpolations |
19 changes: 19 additions & 0 deletions
19
JavaScript/Embeddings/JavaScript (JS template).sublime-syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
%YAML 1.2 | ||
--- | ||
# http://www.sublimetext.com/docs/syntax.html | ||
# highlight tagged template strings | ||
scope: source.js.js-template | ||
version: 2 | ||
hidden: true | ||
|
||
extends: Packages/JavaScript/JavaScript.sublime-syntax | ||
|
||
contexts: | ||
|
||
prototype: | ||
- meta_prepend: true | ||
- include: scope:source.js#text-interpolations | ||
|
||
string-content: | ||
- meta_prepend: true | ||
- include: scope:source.js#string-interpolations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.