@@ -180,6 +180,14 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
180
180
*/
181
181
private readonly startRegionText = / ^ \s * # r e g i o n \b / i;
182
182
private readonly endRegionText = / ^ \s * # e n d r e g i o n \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 * # (? ! r e g i o n \b | e n d r e g i o n \b ) / i;
183
191
184
192
constructor (
185
193
powershellGrammar : IGrammar ,
@@ -317,22 +325,6 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
317
325
return result ;
318
326
}
319
327
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
-
336
328
/**
337
329
* Given a zero based offset, find the line in the document
338
330
* @param offset Zero based offset in the document
@@ -359,19 +351,16 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
359
351
document : vscode . TextDocument ,
360
352
) : ILineNumberRangeList {
361
353
const result = [ ] ;
362
-
363
- const emptyLine = / ^ \s * $ / ;
364
-
365
354
let startLine : number = - 1 ;
366
355
let nextLine : number = - 1 ;
367
356
368
357
tokens . forEach ( ( token ) => {
369
358
if ( token . scopes . indexOf ( "punctuation.definition.comment.powershell" ) !== - 1 ) {
359
+ const line : vscode . TextLine = this . lineAtOffset ( token . startIndex , document ) ;
370
360
// 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 ;
375
364
// A simple pattern for keeping track of contiguous numbers in a known sorted array
376
365
if ( startLine === - 1 ) {
377
366
startLine = lineNum ;
0 commit comments