Skip to content

Commit

Permalink
[XML] Add syntax based folding rules
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe committed Jun 3, 2023
1 parent debf44b commit 73b7892
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 6 deletions.
108 changes: 108 additions & 0 deletions XML/Fold.tmPreferences
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>scope</key>
<string>text.xml, text.xml.dtd, text.xml.xsl</string>
<key>settings</key>
<dict>
<key>indentationFoldingEnabled</key>
<true/>
<key>foldScopes</key>
<array>
<dict>
<!-- Comments -->
<key>begin</key>
<string>comment.block punctuation.definition.comment.begin</string>
<key>end</key>
<string>comment.block punctuation.definition.comment.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<!-- Strings -->
<key>begin</key>
<string>punctuation.definition.string.begin</string>
<key>end</key>
<string>punctuation.definition.string.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<!-- Brackets -->
<key>begin</key>
<string>punctuation.section.brackets.begin</string>
<key>end</key>
<string>punctuation.section.brackets.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<!-- Groups -->
<key>begin</key>
<string>punctuation.section.group.begin</string>
<key>end</key>
<string>punctuation.section.group.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<!-- CDATA -->
<key>begin</key>
<string>meta.tag.sgml.cdata punctuation.definition.tag.begin.content</string>
<key>end</key>
<string>meta.tag.sgml.cdata punctuation.definition.tag</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<!-- DTD tag attributes -->
<key>begin</key>
<string>meta.tag.sgml keyword.declaration</string>
<key>end</key>
<string>meta.tag.sgml punctuation.definition.tag</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<!--
XML preprocessor tag attributes
Preprocessor tags are always self-closing. Thus trailing newlines are
included to avoid closing punctuation to keep on a single line when folded.
-->
<key>begin</key>
<string>meta.tag.preprocessor entity.name.tag</string>
<key>end</key>
<string>meta.tag.preprocessor punctuation.definition.tag</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<!--
XML tag attributes
Exclude trailing newlines, so attributes and child tags
can always be folded/unfolded via fold buttons individually.
Note: Works only if closing punctuation is already on a separate line.
-->
<key>begin</key>
<string>meta.tag entity.name.tag</string>
<key>end</key>
<string>meta.tag punctuation.definition.tag</string>
<key>excludeTrailingNewlines</key>
<true/>
</dict>
<dict>
<!-- Content between XML opening and closing tags -->
<key>begin</key>
<string>meta.tag punctuation.definition.tag.end.opening</string>
<key>end</key>
<string>meta.tag punctuation.definition.tag.begin.closing</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
</array>
</dict>
</dict>
</plist>
24 changes: 18 additions & 6 deletions XML/XML.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,15 @@ contexts:
###[ CDATA ]##################################################################

cdata:
# Notes:
#
# `content` sub-scope is used for folding.
# Changing it might cause folding to break!
- match: (<!\[)(CDATA)(\[)
captures:
1: punctuation.definition.tag.begin.xml
2: keyword.declaration.cdata.xml
3: punctuation.definition.tag.begin.xml
3: punctuation.definition.tag.begin.content.xml
push: cdata-content

cdata-content:
Expand Down Expand Up @@ -552,10 +556,15 @@ contexts:
###[ XML TAGS ]###############################################################

tag:
# Notes:
#
# `opening`, `closing` and `self-closing` sub-scopes are used for folding.
# Changing them might cause folding to break!

# end-tag without attribute support
- match: (</){{qualified_tag_name}}
captures:
1: punctuation.definition.tag.begin.xml
1: punctuation.definition.tag.begin.closing.xml
2: entity.name.tag.namespace.xml
3: invalid.illegal.bad-tag-name.xml
4: entity.name.tag.xml punctuation.separator.namespace.xml
Expand All @@ -565,7 +574,7 @@ contexts:
# opening maybe self-closing tag with optional attributes
- match: (<){{qualified_tag_name}}
captures:
1: punctuation.definition.tag.begin.xml
1: punctuation.definition.tag.begin.opening.xml
2: entity.name.tag.namespace.xml
3: invalid.illegal.bad-tag-name.xml
4: entity.name.tag.xml punctuation.separator.namespace.xml
Expand All @@ -575,8 +584,11 @@ contexts:

begin-tag-content:
- meta_scope: meta.tag.xml
- match: /?>
scope: punctuation.definition.tag.end.xml
- match: />
scope: punctuation.definition.tag.end.self-closing.xml
pop: 1
- match: '>'
scope: punctuation.definition.tag.end.opening.xml
pop: 1
- match: \?>
scope: invalid.illegal.bad-tag-end.xml
Expand All @@ -587,7 +599,7 @@ contexts:
end-tag-content:
- meta_scope: meta.tag.xml
- match: '>'
scope: punctuation.definition.tag.end.xml
scope: punctuation.definition.tag.end.closing.xml
pop: 1
- include: tag-end-missing-pop
- match: '[/\?]>'
Expand Down

0 comments on commit 73b7892

Please sign in to comment.