@@ -410,54 +410,41 @@ export default class Analyzer {
410
410
// start from the line above
411
411
let commentBlockIndex = line - 1
412
412
413
- // check if that line starts with a comment
414
- // its possible that some lines
415
- // might have whitespace before the comment
416
- // begins, so check by iterating from
417
- // the start of the line
418
- const startsWithAComment = ( line : string ) : boolean => {
419
- let charindex = 0
420
- let char = line . charAt ( charindex )
421
- while ( char === ' ' ) {
422
- charindex += 1
423
- char = line . charAt ( charindex )
424
- }
425
- // once we reach a character that is not whitespace
426
- // return true if its a comment, false otherwise
427
- return char === '#'
413
+ // will return the comment string without the comment '#'
414
+ // and without leading whitespace, or null if the line 'l'
415
+ // is not a comment line
416
+ const getComment = ( l : string ) : null | string => {
417
+ // this regexp has to be defined within the function
418
+ const commentRegExp = / ^ \s * \# \s * ( .* ) / g
419
+ const matches = commentRegExp . exec ( l )
420
+ return matches ? matches [ 1 ] . trim ( ) : null
428
421
}
429
422
430
- const existsAndIsComment = ( elm : string ) : boolean =>
431
- elm !== undefined && startsWithAComment ( elm )
432
-
433
423
let currentLine = doc . getText ( {
434
424
start : { line : commentBlockIndex , character : 0 } ,
435
425
end : { line : commentBlockIndex + 1 , character : 0 } ,
436
426
} )
437
427
438
- while ( existsAndIsComment ( currentLine ) ) {
439
- // TODO: vscode must have some API for detecting comments
440
- // should figure that out instead of hardcoding the # symbol...
441
- currentLine = currentLine . replace ( '#' , '' )
442
- if ( currentLine . charAt ( currentLine . length - 1 ) === '\n' ) {
443
- // TODO: should preserve comment line endings?
444
- // I think not because sometimes comment strings wrap a line
445
- // to prevent the line from being too long, and it'd be nice
446
- // to see it displayed as a sentence.
447
- currentLine = currentLine . substr ( 0 , currentLine . length - 1 )
448
- }
449
- commentBlock . push ( currentLine )
428
+ // iterate on every line above and including
429
+ // the current line until getComment returns null
430
+ let currentComment : string | null = ''
431
+ while ( currentComment = getComment ( currentLine ) ) {
432
+ commentBlock . push ( currentComment )
450
433
commentBlockIndex -= 1
451
434
currentLine = doc . getText ( {
452
435
start : { line : commentBlockIndex , character : 0 } ,
453
436
end : { line : commentBlockIndex + 1 , character : 0 } ,
454
437
} )
455
438
}
456
439
457
- // since we searched from bottom up, we then reverse
458
- // the lines so that it reads top down.
459
- const commentStr = commentBlock . reverse ( ) . join ( '\n' )
460
- return commentStr
440
+ if ( commentBlock . length ) {
441
+ // since we searched from bottom up, we then reverse
442
+ // the lines so that it reads top down.
443
+ return commentBlock . reverse ( ) . join ( '\n' )
444
+ }
445
+
446
+ // no comments found above line:
447
+ return null
461
448
}
462
449
463
450
private getAllSymbols ( ) : LSP . SymbolInformation [ ] {
0 commit comments