Skip to content

Commit f5ee6d2

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 09c8ade commit f5ee6d2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/features/Folding.ts

+48
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,40 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
252252
return result;
253253
}
254254

255+
/**
256+
* Given a textmate scope name, find a series of contiguous tokens which contain
257+
* that scope name and pair them together.
258+
* @param tokens List of grammar tokens to parse
259+
* @param scopeName The name of the scope to match
260+
* @param matchType The type of range this region represents e.g. A comment
261+
* @param document The source text document
262+
* @returns A list of LineNumberRange objects of the contiguous token scopes
263+
*/
264+
private matchContiguousScopeElements(
265+
tokens: ITokenList,
266+
scopeName: string,
267+
matchType: vscode.FoldingRangeKind,
268+
document: vscode.TextDocument,
269+
): ILineNumberRangeList {
270+
const result = [];
271+
let startToken;
272+
273+
tokens.forEach((token, index) => {
274+
if (token.scopes.indexOf(scopeName) !== -1) {
275+
if (startToken === undefined) { startToken = token; }
276+
277+
// If we're at the end of the token list, or the next token does not include the scopeName
278+
// we've reached the end of the contiguous block.
279+
if (((index + 1) >= tokens.length) || (tokens[index + 1].scopes.indexOf(scopeName) === -1)) {
280+
result.push((new LineNumberRange(matchType)).fromTokenPair(startToken, token, document));
281+
startToken = undefined;
282+
}
283+
}
284+
});
285+
286+
return result;
287+
}
288+
255289
/**
256290
* Given a list of tokens, return a list of line number ranges which could be folding regions in the document
257291
* @param tokens List of grammar tokens to parse
@@ -280,6 +314,20 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
280314
vscode.FoldingRangeKind.Region, document)
281315
.forEach((match) => { matchedTokens.push(match); });
282316

317+
// Find contiguous here strings @' -> '@
318+
this.matchContiguousScopeElements(
319+
tokens,
320+
"string.quoted.single.heredoc.powershell",
321+
vscode.FoldingRangeKind.Region, document)
322+
.forEach((match) => { matchedTokens.push(match); });
323+
324+
// Find contiguous here strings @" -> "@
325+
this.matchContiguousScopeElements(
326+
tokens,
327+
"string.quoted.double.heredoc.powershell",
328+
vscode.FoldingRangeKind.Region, document)
329+
.forEach((match) => { matchedTokens.push(match); });
330+
283331
return matchedTokens;
284332
}
285333
}

0 commit comments

Comments
 (0)