Skip to content

Commit b4a66c3

Browse files
glennsartiTylerLeonhardt
authored andcommitted
(GH-1428) Make region folding case insensitive and strict whitespace (#1430)
Previously the code folding feature would fail to find region blocks starting with `Region` or ending with `EndRegion`. This was due to the regex only looking for lower case. This commit updates the start and end region detection to be case insensitive, similar to that defined in microsoft/vscode@64186b0 This commit adds has strict whitespace rules for the region/endregion keyword which means only `#region` will match whereas `# region` will not. Previously the code folding had trouble detecting the beginning of comment blocks and regions which were not indented. This was due to the empty line regex expecting at least one whitespace character to be considered "an empty line". This commit modifies the empty line regex to also allow an empty string which is indeed, an empty line.
1 parent 65245a1 commit b4a66c3

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

src/features/Folding.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
354354
): ILineNumberRangeList {
355355
const result = [];
356356

357-
const emptyLine = /^[\s]+$/;
357+
const emptyLine = /^\s*$/;
358358

359359
let startLine: number = -1;
360360
let nextLine: number = -1;
@@ -421,9 +421,9 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
421421
): ITokenList {
422422
const result = [];
423423

424-
const emptyLine = /^[\s]+$/;
425-
const startRegionText = /^#\s*region\b/;
426-
const endRegionText = /^#\s*endregion\b/;
424+
const emptyLine = /^\s*$/;
425+
const startRegionText = /^#region\b/i;
426+
const endRegionText = /^#endregion\b/i;
427427

428428
tokens.forEach((token) => {
429429
if (token.scopes.indexOf("punctuation.definition.comment.powershell") !== -1) {

test/features/folding.test.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ suite("Features", () => {
3838

3939
suite("For a single document", async () => {
4040
const expectedFoldingRegions = [
41-
{ start: 1, end: 6, kind: 1 },
42-
{ start: 7, end: 51, kind: 3 },
43-
{ start: 8, end: 13, kind: 1 },
44-
{ start: 14, end: 17, kind: 3 },
45-
{ start: 19, end: 22, kind: 3 },
46-
{ start: 26, end: 28, kind: 1 },
47-
{ start: 30, end: 40, kind: 3 },
48-
{ start: 32, end: 36, kind: 3 },
49-
{ start: 42, end: 44, kind: 3 },
50-
{ start: 47, end: 50, kind: 3 },
41+
{ start: 0, end: 4, kind: 3 },
42+
{ start: 1, end: 3, kind: 1 },
43+
{ start: 10, end: 15, kind: 1 },
44+
{ start: 16, end: 60, kind: 3 },
45+
{ start: 17, end: 22, kind: 1 },
46+
{ start: 23, end: 26, kind: 3 },
47+
{ start: 28, end: 31, kind: 3 },
48+
{ start: 35, end: 37, kind: 1 },
49+
{ start: 39, end: 49, kind: 3 },
50+
{ start: 41, end: 45, kind: 3 },
51+
{ start: 51, end: 53, kind: 3 },
52+
{ start: 56, end: 59, kind: 3 },
5153
];
5254

5355
test("Can detect all of the foldable regions in a document with CRLF line endings", async () => {

test/fixtures/folding-crlf.ps1

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#RegIon This should fold
2+
<#
3+
Nested different comment types. This should fold
4+
#>
5+
#EnDReGion
6+
7+
# region This should not fold due to whitespace
8+
$shouldFold = $false
9+
# endRegion
110
function short-func-not-fold {};
211
<#
312
.SYNOPSIS
@@ -30,15 +39,15 @@ double quoted herestrings should also fold
3039

3140
#region This fools the indentation folding.
3241
Write-Host "Hello"
33-
# region Nested regions should be foldable
42+
#region Nested regions should be foldable
3443
Write-Host "Hello"
3544
# comment1
3645
Write-Host "Hello"
3746
#endregion
3847
Write-Host "Hello"
3948
# comment2
4049
Write-Host "Hello"
41-
# endregion
50+
#endregion
4251

4352
$c = {
4453
Write-Host "Script blocks should be foldable"

test/fixtures/folding-lf.ps1

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#RegIon This should fold
2+
<#
3+
Nested different comment types. This should fold
4+
#>
5+
#EnDReGion
6+
7+
# region This should not fold due to whitespace
8+
$shouldFold = $false
9+
# endRegion
110
function short-func-not-fold {};
211
<#
312
.SYNOPSIS
@@ -30,15 +39,15 @@ double quoted herestrings should also fold
3039

3140
#region This fools the indentation folding.
3241
Write-Host "Hello"
33-
# region Nested regions should be foldable
42+
#region Nested regions should be foldable
3443
Write-Host "Hello"
3544
# comment1
3645
Write-Host "Hello"
3746
#endregion
3847
Write-Host "Hello"
3948
# comment2
4049
Write-Host "Hello"
41-
# endregion
50+
#endregion
4251

4352
$c = {
4453
Write-Host "Script blocks should be foldable"

0 commit comments

Comments
 (0)