From 1213df6062579910da0eae3ead79679a95a027f4 Mon Sep 17 00:00:00 2001 From: matteocoder <22897138+matteocoder@users.noreply.github.com> Date: Sat, 11 Jan 2025 21:11:10 +0100 Subject: [PATCH 1/5] Add support for syntax-based folding Regions, comment blocks, and heredocs can now be folded. Additionally, region names are now searchable using ctrl-r. --- Fold.tmPreferences | 31 +++++++++++++++++++++++++++++++ PowerShell.sublime-syntax | 15 +++++++++++++++ Tests/syntax_test_PowerShell.ps1 | 13 +++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 Fold.tmPreferences diff --git a/Fold.tmPreferences b/Fold.tmPreferences new file mode 100644 index 0000000..63272f1 --- /dev/null +++ b/Fold.tmPreferences @@ -0,0 +1,31 @@ + + + + scope + source.powershell + settings + + foldScopes + + + begin + punctuation.section.block.begin + end + punctuation.section.block.end + + + begin + punctuation.definition.comment.block.begin + end + punctuation.definition.comment.block.end + + + begin + punctuation.definition.string.begin + end + punctuation.definition.string.end + + + + + diff --git a/PowerShell.sublime-syntax b/PowerShell.sublime-syntax index 64fa213..d1c64d1 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,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\b + scope: punctuation.section.block.end.powershell comment.line.powershell + push: + - meta_content_scope: comment.line.powershell + - include: pop-at-newline + - include: comment-embedded-docs diff --git a/Tests/syntax_test_PowerShell.ps1 b/Tests/syntax_test_PowerShell.ps1 index 5ffc74a..d81f605 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,11 @@ function get-number {} #<- comment.block comment.documentation.embedded punctuation.definition.keyword.documentation #^^^^^^^^^^^^ keyword.other.documentation #> + +#region Test +#<- punctuation.section.block.begin comment.line +# ^^^^ meta.toc-list +# @@@@ local-definition +#endregion (Text after #endregion is optional, but the ISE marks it as a comment as well) +#<- punctuation.section.block.end comment.line +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line From 9ecdc80b1b90f9088075ca185a7dbb1ba3346fb7 Mon Sep 17 00:00:00 2001 From: matteocoder <22897138+matteocoder@users.noreply.github.com> Date: Sun, 12 Jan 2025 14:34:27 +0100 Subject: [PATCH 2/5] Revise region-matching regexes and add new fold scopes --- Fold.tmPreferences | 24 ++++++++++++++++++++++-- PowerShell.sublime-syntax | 26 ++++++++++++++------------ Tests/syntax_test_PowerShell.ps1 | 7 ++++--- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Fold.tmPreferences b/Fold.tmPreferences index 63272f1..0d1e71b 100644 --- a/Fold.tmPreferences +++ b/Fold.tmPreferences @@ -9,21 +9,41 @@ begin - punctuation.section.block.begin + meta.fold.begin end - punctuation.section.block.end + meta.fold.end begin punctuation.definition.comment.block.begin end punctuation.definition.comment.block.end + excludeTrailingNewlines + begin punctuation.definition.string.begin end 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 d1c64d1..451a98d 100644 --- a/PowerShell.sublime-syntax +++ b/PowerShell.sublime-syntax @@ -811,15 +811,17 @@ contexts: - 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\b - scope: punctuation.section.block.end.powershell comment.line.powershell - push: - - meta_content_scope: comment.line.powershell - - include: pop-at-newline - - include: comment-embedded-docs + - match: ^\s*(#)\s*(region)\b\s*(\S.*) + captures: + 1: punctuation.definition.comment.powershell + 2: comment.line.powershell + 3: meta.toc-list.powershell meta.fold.begin.powershell entity.name.section.powershell + - match: '^\s*(#)\s*(region)\b\s*' + captures: + 1: punctuation.definition.comment.powershell + 2: comment.line.powershell meta.fold.begin.powershell + - match: '^\s*(#)\s*(endregion\b[^\S\n]*.*)($\n?)' + captures: + 1: punctuation.definition.comment.powershell + 2: comment.line.powershell + 3: meta.fold.end.powershell diff --git a/Tests/syntax_test_PowerShell.ps1 b/Tests/syntax_test_PowerShell.ps1 index d81f605..ca670fe 100644 --- a/Tests/syntax_test_PowerShell.ps1 +++ b/Tests/syntax_test_PowerShell.ps1 @@ -1812,9 +1812,10 @@ function get-number {} #> #region Test -#<- punctuation.section.block.begin comment.line -# ^^^^ meta.toc-list +#<- punctuation.definition.comment +#^^^^^^ comment.line.powershell +# ^^^^ meta.toc-list meta.fold.begin entity.name.section # @@@@ local-definition #endregion (Text after #endregion is optional, but the ISE marks it as a comment as well) -#<- punctuation.section.block.end comment.line +#<- punctuation.definition.comment.powershell # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line From 76acf11d993bc20661dd2c0d541d91f29dba5db4 Mon Sep 17 00:00:00 2001 From: matteocoder <22897138+matteocoder@users.noreply.github.com> Date: Sun, 12 Jan 2025 16:39:53 +0100 Subject: [PATCH 3/5] Use a more specific scope for heredoc folding This prevents strings that are not heredocs from being foldable. --- Fold.tmPreferences | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Fold.tmPreferences b/Fold.tmPreferences index 0d1e71b..ce6f43a 100644 --- a/Fold.tmPreferences +++ b/Fold.tmPreferences @@ -23,9 +23,17 @@ begin - punctuation.definition.string.begin + string.quoted.double.heredoc punctuation.definition.string.begin end - punctuation.definition.string.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 From d97415f3c64540f8e477b66d6ec215e63cf7b482 Mon Sep 17 00:00:00 2001 From: matteocoder <22897138+matteocoder@users.noreply.github.com> Date: Sun, 12 Jan 2025 18:01:18 +0200 Subject: [PATCH 4/5] Revise matching regexes and scoping for regions Co-authored-by: deathaxe --- PowerShell.sublime-syntax | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/PowerShell.sublime-syntax b/PowerShell.sublime-syntax index 451a98d..7d3e927 100644 --- a/PowerShell.sublime-syntax +++ b/PowerShell.sublime-syntax @@ -811,17 +811,16 @@ contexts: - include: comment-line regions: - - match: ^\s*(#)\s*(region)\b\s*(\S.*) - captures: - 1: punctuation.definition.comment.powershell - 2: comment.line.powershell - 3: meta.toc-list.powershell meta.fold.begin.powershell entity.name.section.powershell - - match: '^\s*(#)\s*(region)\b\s*' - captures: - 1: punctuation.definition.comment.powershell - 2: comment.line.powershell meta.fold.begin.powershell - - match: '^\s*(#)\s*(endregion\b[^\S\n]*.*)($\n?)' - captures: - 1: punctuation.definition.comment.powershell - 2: comment.line.powershell - 3: meta.fold.end.powershell + - 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 From 0d1a3a2770526d72d27f60635e4581d76d44db53 Mon Sep 17 00:00:00 2001 From: matteocoder <22897138+matteocoder@users.noreply.github.com> Date: Sun, 12 Jan 2025 17:11:05 +0100 Subject: [PATCH 5/5] Update region syntax highlighting tests --- Tests/syntax_test_PowerShell.ps1 | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Tests/syntax_test_PowerShell.ps1 b/Tests/syntax_test_PowerShell.ps1 index ca670fe..b67309c 100644 --- a/Tests/syntax_test_PowerShell.ps1 +++ b/Tests/syntax_test_PowerShell.ps1 @@ -1813,9 +1813,24 @@ function get-number {} #region Test #<- punctuation.definition.comment -#^^^^^^ comment.line.powershell -# ^^^^ meta.toc-list meta.fold.begin entity.name.section +#^^^^^^ keyword.other.region.begin +#<- comment.line.powershell +# ^^^^ meta.toc-list entity.name.section # @@@@ local-definition -#endregion (Text after #endregion is optional, but the ISE marks it as a comment as well) +# ^ meta.fold.begin +#endregion (More comments) #<- punctuation.definition.comment.powershell -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line +#^^^^^^^^^ 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