Skip to content

(GH-1459) Folding provider should not crash with mismatched region tokens #1461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/features/Folding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
if (token.scopes.indexOf(startScopeName) !== -1) {
tokenStack.push(token);
}
if (token.scopes.indexOf(endScopeName) !== -1) {
if ((tokenStack.length > 0) && (token.scopes.indexOf(endScopeName) !== -1)) {
result.unshift((new LineNumberRange(matchType)).fromTokenPair(tokenStack.pop(), token, document));
}
});
Expand Down
14 changes: 14 additions & 0 deletions test/features/folding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ suite("Features", () => {

assertFoldingRegions(result, expectedFoldingRegions);
});

test("Can detect all of the foldable regions in a document with mismatched regions", async () => {
const expectedMismatchedFoldingRegions = [
{ start: 2, end: 4, kind: 3 },
];

// Integration test against the test fixture 'folding-mismatch.ps1' that contains
// comment regions with mismatched beginning and end
const uri = vscode.Uri.file(path.join(fixturePath, "folding-mismatch.ps1"));
const document = await vscode.workspace.openTextDocument(uri);
const result = await provider.provideFoldingRanges(document, null, null);

assertFoldingRegions(result, expectedMismatchedFoldingRegions);
});
});
});
});
7 changes: 7 additions & 0 deletions test/fixtures/folding-mismatch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#endregion should not fold - mismatched

#region This should fold
$something = 'foldable'
#endregion

#region should not fold - mismatched