Skip to content

Commit 57bd4c7

Browse files
committed
(GH-1336) Add syntax folding for here strings
This commit adds detection of text regions composed of single and double quoted here strings; @' '@ and @" "@.
1 parent 47640ea commit 57bd4c7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/features/Folding.ts

+37
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,31 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
182182
return result.reverse();
183183
}
184184

185+
// Given a textmate scope name, find a series of contiguous tokens which contain
186+
// that scope name and pair them together.
187+
private matchContiguousScopeElements(tokens,
188+
scopeName: string,
189+
matchType: MatchType,
190+
document: vscode.TextDocument) {
191+
const result = [];
192+
let startToken;
193+
194+
tokens.forEach((token, index) => {
195+
if (token.scopes.includes(scopeName)) {
196+
if (startToken === undefined) { startToken = token; }
197+
198+
// If we're at the end of the token list, or the next token does not include the scopeName
199+
// we've reached the end of the contiguous block.
200+
if (((index + 1) >= tokens.length) || (!tokens[index + 1].scopes.includes(scopeName))) {
201+
result.push(new MatchedToken(startToken, token, null, null, matchType, document));
202+
startToken = undefined;
203+
}
204+
}
205+
});
206+
207+
return result;
208+
}
209+
185210
// Given a list of tokens, return a list of matched tokens/line numbers regions
186211
private matchGrammarTokens(tokens, document: vscode.TextDocument): IMatchedTokenList {
187212
const matchedTokens = [];
@@ -200,6 +225,18 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
200225
MatchType.Region, document)
201226
.forEach((x) => { matchedTokens.push(x); });
202227

228+
// Find contiguous here strings @' -> '@
229+
this.matchContiguousScopeElements(tokens,
230+
"string.quoted.single.heredoc.powershell",
231+
MatchType.Region, document)
232+
.forEach((x) => { matchedTokens.push(x); });
233+
234+
// Find contiguous here strings @" -> "@
235+
this.matchContiguousScopeElements(tokens,
236+
"string.quoted.double.heredoc.powershell",
237+
MatchType.Region, document)
238+
.forEach((x) => { matchedTokens.push(x); });
239+
203240
return matchedTokens;
204241
}
205242

0 commit comments

Comments
 (0)