Skip to content

Commit

Permalink
Add support for syntax-based folding
Browse files Browse the repository at this point in the history
Regions, comment blocks, and heredocs can now be folded.
Additionally, Sublime Text now indexes region names as symbols.
  • Loading branch information
matteocoder committed Jan 12, 2025
1 parent 7b9767e commit 19dde50
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Fold.tmPreferences
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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>punctuation.section.block.begin</string>
<key>end</key>
<string>punctuation.section.block.end</string>
</dict>
<dict>
<key>begin</key>
<string>punctuation.definition.comment.block.begin</string>
<key>end</key>
<string>punctuation.definition.comment.block.end</string>
</dict>
<dict>
<key>begin</key>
<string>punctuation.definition.string.begin</string>
<key>end</key>
<string>punctuation.definition.string.end</string>
</dict>
</array>
</dict>
</dict>
</plist>
15 changes: 15 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,17 @@ contexts:
- match: (?=\()
pop: true
- include: comment-line

regions:
- match: ^(#region\s*(.*))$
captures:
# Applying the punctuation.section.block.begin scope to the entire pattern
# allows Sublime Text to show the region name when it is folded.
1: punctuation.section.block.begin.powershell comment.line.powershell
2: meta.toc-list.powershell
- match: ^#endregion
scope: punctuation.section.block.end.powershell comment.line.powershell
push:
- meta_content_scope: comment.line.powershell
- include: pop-at-newline
- include: comment-embedded-docs
12 changes: 12 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,10 @@ function get-number {}
#<- comment.block comment.documentation.embedded punctuation.definition.keyword.documentation
#^^^^^^^^^^^^ keyword.other.documentation
#>

#region Test

Check failure on line 1814 in Tests/syntax_test_PowerShell.ps1

View workflow job for this annotation

GitHub Actions / Syntax Tests (4107)

Unexpected symbol found ['Test' local-definition]
#<- punctuation.section.block.begin comment.line
# ^^^^ meta.toc-list
#endregion (Text after #endregion is optional, but the ISE marks it as a comment as well)
#<- punctuation.section.block.end comment.line
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line

0 comments on commit 19dde50

Please sign in to comment.