Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XML] Add scope based folding #3783

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.localname</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>
36 changes: 24 additions & 12 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 @@ -294,18 +298,18 @@ contexts:
scope: constant.other.xml
pop: 1
- match: \(
scope: punctuation.definition.group.begin.xml
scope: punctuation.section.group.begin.xml
set: dtd-element-parens
- include: dtd-constants
- include: dtd-content-quoted

dtd-element-parens:
- meta_scope: meta.group.xml
- match: \)
scope: punctuation.definition.group.end.xml
scope: punctuation.section.group.end.xml
set: dtd-element-operator
- match: \(
scope: punctuation.definition.group.begin.xml
scope: punctuation.section.group.begin.xml
push: dtd-element-parens
- match: '[*?+]'
scope: keyword.operator.xml
Expand Down Expand Up @@ -353,16 +357,16 @@ contexts:

dtd-attlist-parens:
- match: \(
scope: punctuation.definition.group.begin.xml
scope: punctuation.section.group.begin.xml
push: dtd-attlist-parens-content

dtd-attlist-parens-content:
- meta_scope: meta.group.enumerated.xml
- match: \)
scope: punctuation.definition.group.end.xml
scope: punctuation.section.group.end.xml
pop: 1
- match: \|
scope: punctuation.separator.logical.xml
scope: punctuation.separator.xml
- include: dtd-constants
- include: entity
- include: should-be-entity
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
8 changes: 4 additions & 4 deletions XML/syntax_test_xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@
## ^^^^^^^ keyword.declaration.element
## ^ - keyword - variable
## ^^^^^^^^^ variable.other.element
## ^ punctuation.definition.group.begin
## ^ punctuation.section.group.begin
## ^^^^^^^ constant.other.placeholder
## ^ punctuation.separator
## ^^^ meta.string string.unquoted
Expand All @@ -753,7 +753,7 @@
## ^^^^^ variable.parameter
## ^ punctuation.terminator.parameter
## ^ punctuation.separator
## ^ punctuation.definition.group.begin
## ^ punctuation.section.group.begin
## ^ punctuation.definition.entity
## ^^^^^ constant.character.entity.named
## ^ punctuation.terminator.entity
Expand All @@ -765,9 +765,9 @@
## ^^^ punctuation.definition.entity
## ^^^^^^ constant.character.entity.hexadecimal
## ^ punctuation.terminator.entity
## ^ punctuation.definition.group.end
## ^ punctuation.section.group.end
## ^ keyword.operator
## ^ punctuation.definition.group.end
## ^ punctuation.section.group.end
## ^ keyword.operator
## ^ punctuation.definition.tag.end

Expand Down