From a0ceb7f9122726df7d85df883e8d7aec55377aca Mon Sep 17 00:00:00 2001 From: matteocoder <22897138+matteocoder@users.noreply.github.com> Date: Sun, 12 Jan 2025 17:51:31 +0100 Subject: [PATCH] Add support for syntax-based folding (#191) Regions, comment blocks, and heredocs can now be folded. Additionally, region names are now searchable using ctrl-r. --- Fold.tmPreferences | 59 ++++++++++++++++++++++++++++++++ PowerShell.sublime-syntax | 16 +++++++++ Tests/syntax_test_PowerShell.ps1 | 29 ++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 Fold.tmPreferences diff --git a/Fold.tmPreferences b/Fold.tmPreferences new file mode 100644 index 0000000..ce6f43a --- /dev/null +++ b/Fold.tmPreferences @@ -0,0 +1,59 @@ + + + + scope + source.powershell + settings + + foldScopes + + + begin + meta.fold.begin + end + meta.fold.end + + + begin + punctuation.definition.comment.block.begin + end + punctuation.definition.comment.block.end + excludeTrailingNewlines + + + + begin + string.quoted.double.heredoc punctuation.definition.string.begin + end + string.quoted.double.heredoc punctuation.definition.string.end + excludeTrailingNewlines + + + + begin + string.quoted.single.heredoc punctuation.definition.string.begin + end + string.quoted.single.heredoc punctuation.definition.string.end + excludeTrailingNewlines + + + + begin + punctuation.section.group.begin + end + punctuation.section.group.end + excludeTrailingNewlines + + + + begin + punctuation.section.braces.begin + end + punctuation.section.braces.end + excludeTrailingNewlines + + + + + + diff --git a/PowerShell.sublime-syntax b/PowerShell.sublime-syntax index 64fa213..7d3e927 100644 --- a/PowerShell.sublime-syntax +++ b/PowerShell.sublime-syntax @@ -35,6 +35,7 @@ contexts: pop: true main: + - include: regions - include: comments - include: expressions @@ -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 diff --git a/Tests/syntax_test_PowerShell.ps1 b/Tests/syntax_test_PowerShell.ps1 index 5ffc74a..b67309c 100644 --- a/Tests/syntax_test_PowerShell.ps1 +++ b/Tests/syntax_test_PowerShell.ps1 @@ -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 @@ -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