Skip to content

Commit 33c5240

Browse files
committed
(GH-1336) Add syntax folding for braces and parentheses
This commit adds detection of text regions bounded by braces { } and parentheses ( ). This provides syntax aware folding for functions, arrays and hash tables.
1 parent 9da9d66 commit 33c5240

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/features/Folding.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,49 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
159159
return foldingRanges;
160160
}
161161

162+
// Given a start and end textmate scope name, find matching grammar tokens
163+
// and pair them together. Uses a simple FIFO stack to take into account
164+
// nested regions.
165+
private matchScopeElements(tokens,
166+
startScopeName: string,
167+
endScopeName: string,
168+
matchType: MatchType,
169+
document: vscode.TextDocument): IMatchedTokenList {
170+
const result = [];
171+
const tokenStack = [];
172+
173+
tokens.forEach((token) => {
174+
if (token.scopes.includes(startScopeName)) {
175+
tokenStack.push(token);
176+
}
177+
if (token.scopes.includes(endScopeName)) {
178+
result.push(new MatchedToken(tokenStack.pop(), token, null, null, matchType, document));
179+
}
180+
});
181+
182+
return result.reverse();
183+
}
184+
162185
// Given a list of tokens, return a list of matched tokens/line numbers regions
163186
private matchGrammarTokens(tokens, document: vscode.TextDocument): IMatchedTokenList {
164187
const matchedTokens = [];
165188

189+
// Find matching braces { -> }
190+
this.matchScopeElements(
191+
tokens,
192+
"punctuation.section.braces.begin.powershell",
193+
"punctuation.section.braces.end.powershell",
194+
MatchType.Region, document)
195+
.forEach((match) => { matchedTokens.push(match); });
196+
197+
// Find matching parentheses ( -> )
198+
this.matchScopeElements(
199+
tokens,
200+
"punctuation.section.group.begin.powershell",
201+
"punctuation.section.group.end.powershell",
202+
MatchType.Region, document)
203+
.forEach((match) => { matchedTokens.push(match); });
204+
166205
return matchedTokens;
167206
}
168207

0 commit comments

Comments
 (0)