Skip to content

Commit fd1e342

Browse files
deathaxeFichteFoll
authored andcommitted
Snippets: Highlight CDATA as self closing tag (#188)
Only the encapsulated content of a <![CDATA[ content ]]> tag should be scoped as string. By scoping the beginning and the end as ordinary xml tags the readability can be improved. The content can better be distinguished from the enclosing tag. The <![CDATA[ is parsed by the xml-parser and marks the start of text to exclude from parsing up to the ]]> tag. This commit implements the required changes to do so and fixes a bug which causes the </content> tag after an illegal second CDATA tag to be highlighted as illegal as well.
1 parent a3c8066 commit fd1e342

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

Package/Sublime Text Snippet/Sublime Text Snippet.sublime-syntax

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@ contexts:
1717
3: punctuation.definition.tag.end.xml
1818
push:
1919
- include: comments
20-
- match: '<!\[CDATA\['
21-
scope: punctuation.definition.string.begin.xml
20+
- match: '(<!\[)(CDATA)(\[)'
21+
captures:
22+
1: punctuation.definition.tag.begin.xml
23+
2: entity.name.tag.cdata.xml
24+
3: punctuation.definition.tag.begin.xml
2225
set:
23-
- meta_scope: string.unquoted.cdata.xml
24-
- match: '\]\]>'
25-
scope: punctuation.definition.string.end.xml
26+
- meta_scope: meta.tag.cdata.xml
27+
- meta_content_scope: string.unquoted.cdata.xml
28+
- match: (?=\]\]>)
2629
set:
27-
- include: comments
28-
- match: '<!\[CDATA\['
30+
- match: '\]\]>'
31+
scope: meta.tag.cdata.xml punctuation.definition.tag.end.xml
2932
set:
30-
- meta_scope: invalid.illegal.multiple-cdata-not-allowed.sublime-snippet
31-
- match: '\]\]>'
32-
pop: true
33-
- include: inside_content
33+
- include: comments
34+
- match: '<!\[CDATA\['
35+
push:
36+
- meta_scope: invalid.illegal.multiple-cdata-not-allowed.sublime-snippet
37+
- match: '\]\]>'
38+
pop: true
39+
- include: inside_content
3440
- match: ''
3541
push: scope:source.sublime.snippet
3642
with_prototype:

Package/Sublime Text Snippet/syntax_test_snippet.xml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
# ^ punctuation.definition.tag.begin
1111
# ^ punctuation.definition.tag.end
1212
# ^^^^^^^^^^^^^^^^ comment.block.xml
13-
# ^^^^^^^^^^ string.unquoted.cdata - meta.tag
14-
# ^^^^^^^^^ punctuation.definition.string.begin
13+
# ^^^^^^^^^^^ meta.tag.cdata
14+
# ^^^ punctuation.definition.tag.begin
15+
# ^^^^^ entity.name.tag.cdata
16+
# ^ punctuation.definition.tag.begin
1517
# ^ source.sublime.snippet
1618
Original: ${1:Hey, Joe!}
1719
# ^^^^^^^^^^^^^^ meta.text-substitution
@@ -205,7 +207,7 @@ It is possible to include a literal newline in the replacement: ${1/test/_
205207
# ^ keyword.other.block.end
206208
207209
]]>snippet<</content>
208-
#^^^ string.unquoted.cdata punctuation.definition.string.end
210+
#^^^ meta.tag.cdata punctuation.definition.tag.end
209211
# ^^^^^^^ source.sublime.snippet
210212
# ^ invalid.illegal.missing-entity.xml - source.sublime.snippet
211213
# ^^^^^^^^^^ meta.tag - string
@@ -246,7 +248,7 @@ It is possible to include a literal newline in the replacement: ${1/test/_
246248

247249
<content>
248250
<![CDATA[ example ${1} ]]>
249-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ string.unquoted.cdata.xml - string.unquoted.cdata.xml string.unquoted.cdata.xml
251+
# ^^^^^^^^^^^^^^ string.unquoted.cdata.xml - string.unquoted.cdata.xml string.unquoted.cdata.xml
250252
# ^^ keyword.other.block
251253
# ^ constant.numeric
252254
# ^ keyword.other.block
@@ -256,17 +258,23 @@ It is possible to include a literal newline in the replacement: ${1/test/_
256258
# ^ - invalid
257259
# ^ - invalid
258260
</content>
259-
#^^^^^^^^^ meta.tag
261+
#^^^^^^^^^ meta.tag.xml
262+
# ^^^^^^^ entity.name.tag.localname.xml
260263

261264
<content>
262265
<![CDATA[ example </content> ${1} ]]>
263-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string
264-
# ^ - string
265-
# ^^^^^^^^^^ - meta.tag
266+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.tag.cdata
267+
# ^^^ punctuation.definition.tag.begin
268+
# ^^^^^ entity.name.tag.cdata.xml
269+
# ^ punctuation.definition.tag.begin.xml - string
270+
# ^^^^^^^^^^^^^^^^^^^^^^^^^ string
271+
# ^ - string
272+
# ^^^^^^^^^^ - entity.name.tag
266273
# ^^^^ meta.text-substitution
267274
# ^^ keyword.other.block
268275
# ^ constant.numeric
269276
# ^ keyword.other.block
277+
# ^^^ punctuation.definition.tag.end
270278
</content>
271279
#^^^^^^^^^ meta.tag
272280

0 commit comments

Comments
 (0)