Skip to content

Commit d3ce263

Browse files
glennsartiTylerLeonhardt
authored andcommitted
(PowerShellGH-1459) Folding provider should not crash with mismatched region tokens (PowerShell#1461)
Previously the folding provider would crash with an error if the document contained mismatched begin and end region comments e.g. If the document started with `# endregion`. This was due to the token stack code always assuming there was at least one element in the stack. This commit modifies the end region detection to only trigger if there was a previous begin region.
1 parent a555ba5 commit d3ce263

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/features/Folding.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
283283
if (token.scopes.indexOf(startScopeName) !== -1) {
284284
tokenStack.push(token);
285285
}
286-
if (token.scopes.indexOf(endScopeName) !== -1) {
286+
if ((tokenStack.length > 0) && (token.scopes.indexOf(endScopeName) !== -1)) {
287287
result.unshift((new LineNumberRange(matchType)).fromTokenPair(tokenStack.pop(), token, document));
288288
}
289289
});

test/features/folding.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ suite("Features", () => {
8282

8383
assertFoldingRegions(result, expectedFoldingRegions);
8484
});
85+
86+
test("Can detect all of the foldable regions in a document with mismatched regions", async () => {
87+
const expectedMismatchedFoldingRegions = [
88+
{ start: 2, end: 4, kind: 3 },
89+
];
90+
91+
// Integration test against the test fixture 'folding-mismatch.ps1' that contains
92+
// comment regions with mismatched beginning and end
93+
const uri = vscode.Uri.file(path.join(fixturePath, "folding-mismatch.ps1"));
94+
const document = await vscode.workspace.openTextDocument(uri);
95+
const result = await provider.provideFoldingRanges(document, null, null);
96+
97+
assertFoldingRegions(result, expectedMismatchedFoldingRegions);
98+
});
8599
});
86100
});
87101
});

test/fixtures/folding-mismatch.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#endregion should not fold - mismatched
2+
3+
#region This should fold
4+
$something = 'foldable'
5+
#endregion
6+
7+
#region should not fold - mismatched

0 commit comments

Comments
 (0)