Skip to content

Commit 0983866

Browse files
committed
(GH-824) More strict block comment folding detection
Previously the folder would search for the region markers without case sensitivity. This commit modifies the regular expressions to be more strict on the what is a region marker and adds a negative test to ensure that regions that are not cased correctly are not folded .
1 parent 5ffd49f commit 0983866

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/PowerShellEditorServices/Language/TokenOperations.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ internal static class TokenOperations
2424
// These regular expressions are used to match lines which mark the start and end of region comment in a PowerShell
2525
// script. They are based on the defaults in the VS Code Language Configuration at;
2626
// https://github.com/Microsoft/vscode/blob/64186b0a26/extensions/powershell/language-configuration.json#L26-L31
27+
// https://github.com/Microsoft/vscode/issues/49070
2728
static private readonly Regex s_startRegionTextRegex = new Regex(
28-
@"^\s*#region\b",
29-
RegexOptions.IgnoreCase | RegexOptions.Compiled);
29+
@"^\s*#[rR]egion\b", RegexOptions.Compiled);
3030
static private readonly Regex s_endRegionTextRegex = new Regex(
31-
@"^\s*#endregion\b",
32-
RegexOptions.IgnoreCase | RegexOptions.Compiled);
31+
@"^\s*#[eE]nd[rR]egion\b", RegexOptions.Compiled);
3332

3433
/// <summary>
3534
/// Extracts all of the unique foldable regions in a script given the list tokens

test/PowerShellEditorServices.Test/Language/TokenOperationsTests.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ private static FoldingReference CreateFoldingReference(int startLine, int startC
4343
// folding regions and regions which should not be
4444
// detected. Due to file encoding this could be CLRF or LF line endings
4545
private const string allInOneScript =
46-
@"#RegIon This should fold
46+
@"#Region This should fold
4747
<#
4848
Nested different comment types. This should fold
4949
#>
50-
#EnDReGion
50+
#EndRegion
5151
5252
# region This should not fold due to whitespace
5353
$shouldFold = $false
@@ -124,6 +124,10 @@ double quoted herestrings should also fold
124124
${this
125125
is
126126
valid} = 5
127+
128+
#RegIon This should fold due to casing
129+
$foo = 'bar'
130+
#EnDReGion
127131
";
128132
private FoldingReference[] expectedAllInOneScriptFolds = {
129133
CreateFoldingReference(0, 0, 4, 10, "region"),

0 commit comments

Comments
 (0)