Skip to content

Commit 15b6dd1

Browse files
committed
(maint) Refactor region folding detection
Previously the region comment detection used a little convoluted method to detect regions in a document. This commit simplifies the detection by extracting the line from the document and using regex's similar to that used by the PowerShell language configuration. This also removes the need for the emptyline and subsequentText method calls. While the performance of the folder is pretty quick, this should in theory make it faster on larger documents by doing less calls to the VSCode Document API.
1 parent b4a66c3 commit 15b6dd1

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/features/Folding.ts

+19-20
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ interface ILineNumberRangeList extends Array<LineNumberRange> { }
173173
export class FoldingProvider implements vscode.FoldingRangeProvider {
174174
private powershellGrammar: IGrammar;
175175

176+
/**
177+
* These regular expressions are used to match lines which mark the start and end of region comment in a PowerShell
178+
* script. They are based on the defaults in the VS Code Language Configuration at;
179+
* https://github.com/Microsoft/vscode/blob/64186b0a26/extensions/powershell/language-configuration.json#L26-L31
180+
*/
181+
private readonly startRegionText = /^\s*#region\b/i;
182+
private readonly endRegionText = /^\s*#endregion\b/i;
183+
176184
constructor(
177185
powershellGrammar: IGrammar,
178186
) {
@@ -326,18 +334,16 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
326334
}
327335

328336
/**
329-
* Given a zero based offset, find the line text after it in the document
337+
* Given a zero based offset, find the line in the document
330338
* @param offset Zero based offset in the document
331339
* @param document The source text document
332-
* @returns The line text after the offset, not including the subsequent Line Feed
340+
* @returns The line at the offset
333341
*/
334-
private subsequentText(
342+
private lineAtOffset(
335343
offset: number,
336344
document: vscode.TextDocument,
337-
): string {
338-
const startPos: vscode.Position = document.positionAt(offset);
339-
const endPos: vscode.Position = document.lineAt(document.positionAt(offset)).range.end;
340-
return document.getText(new vscode.Range(startPos, endPos));
345+
): vscode.TextLine {
346+
return document.lineAt(document.positionAt(offset));
341347
}
342348

343349
/**
@@ -420,21 +426,14 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
420426
document: vscode.TextDocument,
421427
): ITokenList {
422428
const result = [];
423-
424-
const emptyLine = /^\s*$/;
425-
const startRegionText = /^#region\b/i;
426-
const endRegionText = /^#endregion\b/i;
427-
428429
tokens.forEach((token) => {
429430
if (token.scopes.indexOf("punctuation.definition.comment.powershell") !== -1) {
430-
if (emptyLine.test(this.preceedingText(token.startIndex, document))) {
431-
const commentText = this.subsequentText(token.startIndex, document);
432-
if (startRegionText.test(commentText)) {
433-
result.push(this.addTokenScope(token, "custom.start.region"));
434-
}
435-
if (endRegionText.test(commentText)) {
436-
result.push(this.addTokenScope(token, "custom.end.region"));
437-
}
431+
const line: string = this.lineAtOffset(token.startIndex, document).text;
432+
if (this.startRegionText.test(line)) {
433+
result.push(this.addTokenScope(token, "custom.start.region"));
434+
}
435+
if (this.endRegionText.test(line)) {
436+
result.push(this.addTokenScope(token, "custom.end.region"));
438437
}
439438
}
440439
});

0 commit comments

Comments
 (0)