Skip to content

Commit

Permalink
(GH-824) More strict block comment folding detection
Browse files Browse the repository at this point in the history
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 .
  • Loading branch information
glennsarti committed Dec 23, 2018
1 parent 5ffd49f commit 0983866
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/PowerShellEditorServices/Language/TokenOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ internal static class TokenOperations
// These regular expressions are used to match lines which mark the start and end of region comment in a PowerShell
// script. They are based on the defaults in the VS Code Language Configuration at;
// https://github.com/Microsoft/vscode/blob/64186b0a26/extensions/powershell/language-configuration.json#L26-L31
// https://github.com/Microsoft/vscode/issues/49070
static private readonly Regex s_startRegionTextRegex = new Regex(
@"^\s*#region\b",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
@"^\s*#[rR]egion\b", RegexOptions.Compiled);
static private readonly Regex s_endRegionTextRegex = new Regex(
@"^\s*#endregion\b",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
@"^\s*#[eE]nd[rR]egion\b", RegexOptions.Compiled);

/// <summary>
/// Extracts all of the unique foldable regions in a script given the list tokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ private static FoldingReference CreateFoldingReference(int startLine, int startC
// folding regions and regions which should not be
// detected. Due to file encoding this could be CLRF or LF line endings
private const string allInOneScript =
@"#RegIon This should fold
@"#Region This should fold
<#
Nested different comment types. This should fold
#>
#EnDReGion
#EndRegion
# region This should not fold due to whitespace
$shouldFold = $false
Expand Down Expand Up @@ -124,6 +124,10 @@ double quoted herestrings should also fold
${this
is
valid} = 5
#RegIon This should fold due to casing
$foo = 'bar'
#EnDReGion
";
private FoldingReference[] expectedAllInOneScriptFolds = {
CreateFoldingReference(0, 0, 4, 10, "region"),
Expand Down

0 comments on commit 0983866

Please sign in to comment.