Skip to content

Commit c5d3b59

Browse files
committed
(PowerShellGH-1428) Make region folding case insensitive and parse whitespace correctly
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 use a similar regex to that of the VS Code default, defined in microsoft/vscode@64186b0 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 c5d3b59

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
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 = /^#\s*region\b/i;
426+
const endRegionText = /^#\s*endregion\b/i;
427427

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

test/features/folding.test.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ 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: 6, end: 8, kind: 3 },
44+
{ start: 10, end: 15, kind: 1 },
45+
{ start: 16, end: 60, kind: 3 },
46+
{ start: 17, end: 22, kind: 1 },
47+
{ start: 23, end: 26, kind: 3 },
48+
{ start: 28, end: 31, kind: 3 },
49+
{ start: 35, end: 37, kind: 1 },
50+
{ start: 39, end: 49, kind: 3 },
51+
{ start: 41, end: 45, kind: 3 },
52+
{ start: 51, end: 53, kind: 3 },
53+
{ start: 56, end: 59, kind: 3 },
5154
];
5255

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

test/fixtures/folding-crlf.ps1

+9
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 also fold
8+
$shouldFold = $true
9+
# endRegion
110
function short-func-not-fold {};
211
<#
312
.SYNOPSIS

test/fixtures/folding-lf.ps1

+9
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 also fold
8+
$shouldFold = $true
9+
# endRegion
110
function short-func-not-fold {};
211
<#
312
.SYNOPSIS

0 commit comments

Comments
 (0)