Skip to content

Commit accac07

Browse files
committed
(PowerShellGH-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 33c5240 commit accac07

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/features/Folding.ts

+39
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 = [];
@@ -202,6 +227,20 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
202227
MatchType.Region, document)
203228
.forEach((match) => { matchedTokens.push(match); });
204229

230+
// Find contiguous here strings @' -> '@
231+
this.matchContiguousScopeElements(
232+
tokens,
233+
"string.quoted.single.heredoc.powershell",
234+
MatchType.Region, document)
235+
.forEach((match) => { matchedTokens.push(match); });
236+
237+
// Find contiguous here strings @" -> "@
238+
this.matchContiguousScopeElements(
239+
tokens,
240+
"string.quoted.double.heredoc.powershell",
241+
MatchType.Region, document)
242+
.forEach((match) => { matchedTokens.push(match); });
243+
205244
return matchedTokens;
206245
}
207246

0 commit comments

Comments
 (0)