Skip to content

Commit dae023f

Browse files
committed
Updates commentsAbove to strip leading whitespace
1 parent 35b49bf commit dae023f

File tree

2 files changed

+23
-36
lines changed

2 files changed

+23
-36
lines changed

server/src/__tests__/analyzer.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ describe('findSymbolCompletions', () => {
195195
describe('commentsAbove', () => {
196196
it('returns a string of a comment block above a line', () => {
197197
analyzer.analyze(CURRENT_URI, FIXTURES.COMMENT_DOC)
198-
expect(analyzer.commentsAbove(CURRENT_URI, 22)).toEqual(' doc for func_one')
198+
expect(analyzer.commentsAbove(CURRENT_URI, 22)).toEqual('doc for func_one')
199199

200200
expect(analyzer.commentsAbove(CURRENT_URI, 28)).toEqual(
201-
' doc for func_two\n has two lines',
201+
'doc for func_two\nhas two lines',
202202
)
203203

204204
// if there is a line break in the comments

server/src/analyser.ts

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -410,54 +410,41 @@ export default class Analyzer {
410410
// start from the line above
411411
let commentBlockIndex = line - 1
412412

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
428421
}
429422

430-
const existsAndIsComment = (elm: string): boolean =>
431-
elm !== undefined && startsWithAComment(elm)
432-
433423
let currentLine = doc.getText({
434424
start: { line: commentBlockIndex, character: 0 },
435425
end: { line: commentBlockIndex + 1, character: 0 },
436426
})
437427

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)
450433
commentBlockIndex -= 1
451434
currentLine = doc.getText({
452435
start: { line: commentBlockIndex, character: 0 },
453436
end: { line: commentBlockIndex + 1, character: 0 },
454437
})
455438
}
456439

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
461448
}
462449

463450
private getAllSymbols(): LSP.SymbolInformation[] {

0 commit comments

Comments
 (0)