Skip to content

Commit 47640ea

Browse files
committed
(GH-1336) Add syntax folding for braces and brackets
This commit adds detection of text regions bounded by braces { } and brackets ( ). This provides syntax aware folding for functions, arrays and hash tables.
1 parent 545e3f2 commit 47640ea

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/features/Folding.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,47 @@ 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(tokens,
191+
"punctuation.section.braces.begin.powershell",
192+
"punctuation.section.braces.end.powershell",
193+
MatchType.Region, document)
194+
.forEach((x) => { matchedTokens.push(x); });
195+
196+
// Find matching brackets ( -> )
197+
this.matchScopeElements(tokens,
198+
"punctuation.section.group.begin.powershell",
199+
"punctuation.section.group.end.powershell",
200+
MatchType.Region, document)
201+
.forEach((x) => { matchedTokens.push(x); });
202+
166203
return matchedTokens;
167204
}
168205

0 commit comments

Comments
 (0)