Skip to content

Commit 09ae743

Browse files
committed
(PowerShellGH-1437) Fix detecting contiguous comment blocks and regions
Previously the syntax folding feature would not correctly identify comment blocks and comment regions if they appeared all together. This commit changes the comment block detection to ignore line comments that start with region and endregion, i.e. region block start/end directives. This commit also adds test for this scenario.
1 parent 15b6dd1 commit 09ae743

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

src/features/Folding.ts

+12-23
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
180180
*/
181181
private readonly startRegionText = /^\s*#region\b/i;
182182
private readonly endRegionText = /^\s*#endregion\b/i;
183+
/**
184+
* This regular expressions is used to detect a line comment (as opposed to an inline comment), that is not a region
185+
* block directive i.e.
186+
* - No text between the beginning of the line and `#`
187+
* - Comment does start with region
188+
* - Comment does start with endregion
189+
*/
190+
private readonly lineCommentText = /\s*#(?!region\b|endregion\b)/i;
183191

184192
constructor(
185193
powershellGrammar: IGrammar,
@@ -317,22 +325,6 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
317325
return result;
318326
}
319327

320-
/**
321-
* Given a zero based offset, find the line text preceeding it in the document
322-
* @param offset Zero based offset in the document
323-
* @param document The source text document
324-
* @returns The line text preceeding the offset, not including the preceeding Line Feed
325-
*/
326-
private preceedingText(
327-
offset: number,
328-
document: vscode.TextDocument,
329-
): string {
330-
const endPos = document.positionAt(offset);
331-
const startPos = endPos.translate(0, -endPos.character);
332-
333-
return document.getText(new vscode.Range(startPos, endPos));
334-
}
335-
336328
/**
337329
* Given a zero based offset, find the line in the document
338330
* @param offset Zero based offset in the document
@@ -359,19 +351,16 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
359351
document: vscode.TextDocument,
360352
): ILineNumberRangeList {
361353
const result = [];
362-
363-
const emptyLine = /^\s*$/;
364-
365354
let startLine: number = -1;
366355
let nextLine: number = -1;
367356

368357
tokens.forEach((token) => {
369358
if (token.scopes.indexOf("punctuation.definition.comment.powershell") !== -1) {
359+
const line: vscode.TextLine = this.lineAtOffset(token.startIndex, document);
370360
// The punctuation.definition.comment.powershell token matches new-line comments
371-
// and inline comments e.g. `$x = 'foo' # inline comment`. We are only interested
372-
// in comments which begin the line i.e. no preceeding text
373-
if (emptyLine.test(this.preceedingText(token.startIndex, document))) {
374-
const lineNum = document.positionAt(token.startIndex).line;
361+
// and inline comments e.g. `$x = 'foo' # inline comment`.
362+
if (this.lineCommentText.test(line.text)) {
363+
const lineNum = line.lineNumber;
375364
// A simple pattern for keeping track of contiguous numbers in a known sorted array
376365
if (startLine === -1) {
377366
startLine = lineNum;

test/features/folding.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ suite("Features", () => {
5050
{ start: 41, end: 45, kind: 3 },
5151
{ start: 51, end: 53, kind: 3 },
5252
{ start: 56, end: 59, kind: 3 },
53+
{ start: 64, end: 66, kind: 1 },
54+
{ start: 67, end: 72, kind: 3 },
55+
{ start: 68, end: 70, kind: 1 },
5356
];
5457

5558
test("Can detect all of the foldable regions in a document with CRLF line endings", async () => {

test/fixtures/folding-crlf.ps1

+12
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,15 @@ double quoted herestrings should also fold
5959
'should fold2'
6060
)
6161
}
62+
63+
# Make sure contiguous comment blocks can be folded properly
64+
65+
# Comment Block 1
66+
# Comment Block 1
67+
# Comment Block 1
68+
#region Comment Block 3
69+
# Comment Block 2
70+
# Comment Block 2
71+
# Comment Block 2
72+
$something = $true
73+
#endregion Comment Block 3

test/fixtures/folding-lf.ps1

+12
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,15 @@ double quoted herestrings should also fold
5959
'should fold2'
6060
)
6161
}
62+
63+
# Make sure contiguous comment blocks can be folded properly
64+
65+
# Comment Block 1
66+
# Comment Block 1
67+
# Comment Block 1
68+
#region Comment Block 3
69+
# Comment Block 2
70+
# Comment Block 2
71+
# Comment Block 2
72+
$something = $true
73+
#endregion Comment Block 3

0 commit comments

Comments
 (0)