Skip to content

Commit

Permalink
Add support for syntax-based folding (#191)
Browse files Browse the repository at this point in the history
Regions, comment blocks, and heredocs can now be folded.
Additionally, region names are now searchable using ctrl-r.
  • Loading branch information
matteocoder authored Jan 12, 2025
1 parent 7b9767e commit a0ceb7f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Fold.tmPreferences
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>scope</key>
<string>source.powershell</string>
<key>settings</key>
<dict>
<key>foldScopes</key>
<array>
<dict>
<key>begin</key>
<string>meta.fold.begin</string>
<key>end</key>
<string>meta.fold.end</string>
</dict>
<dict>
<key>begin</key>
<string>punctuation.definition.comment.block.begin</string>
<key>end</key>
<string>punctuation.definition.comment.block.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<key>begin</key>
<string>string.quoted.double.heredoc punctuation.definition.string.begin</string>
<key>end</key>
<string>string.quoted.double.heredoc punctuation.definition.string.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<key>begin</key>
<string>string.quoted.single.heredoc punctuation.definition.string.begin</string>
<key>end</key>
<string>string.quoted.single.heredoc punctuation.definition.string.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<key>begin</key>
<string>punctuation.section.group.begin</string>
<key>end</key>
<string>punctuation.section.group.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
<dict>
<key>begin</key>
<string>punctuation.section.braces.begin</string>
<key>end</key>
<string>punctuation.section.braces.end</string>
<key>excludeTrailingNewlines</key>
<false/>
</dict>
</array>
</dict>
</dict>
</plist>
16 changes: 16 additions & 0 deletions PowerShell.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contexts:
pop: true

main:
- include: regions
- include: comments
- include: expressions

Expand Down Expand Up @@ -808,3 +809,18 @@ contexts:
- match: (?=\()
pop: true
- include: comment-line

regions:
- match: ^\s*((#)\s*(region\b)(?:\s*(\S.*))?(\n?))
captures:
1: comment.line.powershell
2: punctuation.definition.comment.powershell
3: keyword.other.region.begin.powershell
4: meta.toc-list.powershell entity.name.section.powershell
5: meta.fold.begin.powershell
- match: ^\s*((#)\s*(endregion\b).*(\n?))
captures:
1: comment.line.powershell
2: punctuation.definition.comment.powershell
3: keyword.other.region.end.powershell
4: meta.fold.end.powershell
29 changes: 29 additions & 0 deletions Tests/syntax_test_PowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ There is no @platting here!
"@
# <- string.quoted.double.heredoc
# <- string.quoted.double.heredoc
@'
#<- meta.string string.quoted.single.heredoc punctuation.definition.string.begin
A 'single quoted' "heredoc"
'@
#<- meta.string string.quoted.single.heredoc punctuation.definition.string.end

# Numeric constants

Expand Down Expand Up @@ -1805,3 +1810,27 @@ function get-number {}
#<- comment.block comment.documentation.embedded punctuation.definition.keyword.documentation
#^^^^^^^^^^^^ keyword.other.documentation
#>

#region Test
#<- punctuation.definition.comment
#^^^^^^ keyword.other.region.begin
#<- comment.line.powershell
# ^^^^ meta.toc-list entity.name.section
# @@@@ local-definition
# ^ meta.fold.begin
#endregion (More comments)
#<- punctuation.definition.comment.powershell
#^^^^^^^^^ keyword.other.region.end.powershell
#<- comment.line
# ^ meta.fold.end

#region
#<- punctuation.definition.comment
#^^^^^^ keyword.other.region.begin
#<- comment.line.powershell
# ^ meta.fold.begin
#endregion (More comments)
#<- punctuation.definition.comment.powershell
#^^^^^^^^^ keyword.other.region.end.powershell
#<- comment.line
# ^ meta.fold.end

0 comments on commit a0ceb7f

Please sign in to comment.