Skip to content

Commit cfac0be

Browse files
Show documentation comments in quick info.
1 parent f4cdbfa commit cfac0be

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/services/services.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,13 @@ module ts {
264264

265265
private processDocumentationCommentDeclaration(lines: string[], declaration: Node) {
266266
var commentRanges = getLeadingCommentRangesOfNode(declaration);
267-
var sourceFile = declaration.getSourceFile();
267+
if (commentRanges) {
268+
var sourceFile = declaration.getSourceFile();
268269

269-
for (var i = 0, n = commentRanges.length; i < n; i++) {
270-
this.processDocumentationCommentRange(
271-
lines, sourceFile, commentRanges[0]);
270+
for (var i = 0, n = commentRanges.length; i < n; i++) {
271+
this.processDocumentationCommentRange(
272+
lines, sourceFile, commentRanges[0]);
273+
}
272274
}
273275
}
274276

@@ -320,16 +322,21 @@ module ts {
320322
// Baz
321323
var trimLength: number = undefined;
322324
for (var iLine = startLineAndChar.line + 1; iLine <= endLineAndChar.line; iLine++) {
323-
var lineStart = sourceFile.getPositionFromLineAndCharacter(iLine, /*character:*/ 0);
324-
var docCommentTriviaLength = this.skipDocumentationCommentTrivia(sourceFile.text, lineStart);
325+
var lineStart = sourceFile.getPositionFromLineAndCharacter(iLine, /*character:*/ 1);
326+
var lineEnd = iLine === endLineAndChar.line
327+
? commentRange.end - "*/".length
328+
: sourceFile.getPositionFromLineAndCharacter(iLine + 1, 1);
329+
var docCommentTriviaLength = this.skipDocumentationCommentTrivia(sourceFile.text, lineStart, lineEnd);
325330

326331
if (trimLength === undefined || (docCommentTriviaLength && docCommentTriviaLength < trimLength)) {
327332
trimLength = docCommentTriviaLength;
328333
}
329334
}
330335

331336
// Add the first line in.
332-
var firstLine = this.trimRight(sourceFile.text.substr(commentRange.pos + "/**".length));
337+
var firstLine = sourceFile.text.substring(
338+
commentRange.pos + "/**".length,
339+
sourceFile.getPositionFromLineAndCharacter(startLineAndChar.line + 1, /*character:*/ 1)).trim();
333340
if (firstLine !== "") {
334341
lines.push(firstLine);
335342
}
@@ -338,16 +345,16 @@ module ts {
338345
// of the line (with the length up to the
339346
for (var iLine = startLineAndChar.line + 1; iLine < endLineAndChar.line; iLine++) {
340347
var line = this.trimRight(sourceFile.text.substring(
341-
sourceFile.getPositionFromLineAndCharacter(iLine, /*character*/0),
342-
sourceFile.getPositionFromLineAndCharacter(iLine + 1, /*character*/0))).substr(trimLength);
348+
sourceFile.getPositionFromLineAndCharacter(iLine, /*character*/ 1),
349+
sourceFile.getPositionFromLineAndCharacter(iLine + 1, /*character*/ 1))).substr(trimLength);
343350

344351
lines.push(line);
345352
}
346353

347354
// Add the last line if there is any actual text before the */
348-
var lastLine = sourceFile.text.substring(
349-
sourceFile.getPositionFromLineAndCharacter(endLineAndChar.line, /*character:*/ 0),
350-
commentRange.end - "*/".length).substr(trimLength);
355+
var lastLine = this.trimRight(sourceFile.text.substring(
356+
sourceFile.getPositionFromLineAndCharacter(endLineAndChar.line, /*character:*/ 1),
357+
commentRange.end - "*/".length)).substr(trimLength);
351358

352359
if (lastLine !== "") {
353360
lines.push(lastLine);
@@ -358,13 +365,25 @@ module ts {
358365
return val.replace(/(\n|\r|\s)+$/, '');
359366
}
360367

361-
private skipDocumentationCommentTrivia(text: string, lineStart: number): number {
362-
for (var i = 0; i < text.length; i++) {
363-
var char = text.charCodeAt(i);
364-
if (char === CharacterCodes.asterisk) {
365-
return i + 1;
368+
private skipDocumentationCommentTrivia(text: string, lineStart: number, lineEnd: number): number {
369+
var seenAsterisk = false;
370+
var lineLength = lineEnd - lineStart;
371+
for (var i = 0; i < lineLength; i++) {
372+
var char = text.charCodeAt(i + lineStart);
373+
if (char === CharacterCodes.asterisk && !seenAsterisk) {
374+
// Ignore the first asterisk we see. We want to trim out the line of *'s
375+
// commonly seen at the start of a doc comment.
376+
seenAsterisk = true;
377+
continue;
378+
}
379+
else if (isLineBreak(char)) {
380+
// This was a blank line. Just ignore it wrt computing the leading whitespace to
381+
// trim.
382+
break;
366383
}
367384
else if (!isWhiteSpace(char)) {
385+
// Found a real doc comment character. Keep track of it so we can determine how
386+
// much of the doc comment leading trivia to trim off.
368387
return i;
369388
}
370389
}
@@ -2362,10 +2381,9 @@ module ts {
23622381
return result.length > 0 ? result.join(',') : ScriptElementKindModifier.none;
23632382
}
23642383

2365-
/// QuickInfo
23662384
function getQuickInfoAtPosition(fileName: string, position: number): QuickInfo {
23672385
synchronizeHostData();
2368-
2386+
23692387
fileName = TypeScript.switchToForwardSlashes(fileName);
23702388
var sourceFile = getSourceFile(fileName);
23712389
var node = getNodeAtPosition(sourceFile, position);
@@ -2378,6 +2396,9 @@ module ts {
23782396
return undefined;
23792397
}
23802398

2399+
var documentation = symbol.getDocumentationComment();
2400+
var documentationParts = documentation === "" ? [] : [new SymbolDisplayPart(documentation, SymbolDisplayPartKind.text, /*symbol:*/ null)];
2401+
23812402
// Having all this logic here is pretty unclean. Consider moving to the roslyn model
23822403
// where all symbol display logic is encapsulated into visitors and options.
23832404
var totalParts: SymbolDisplayPart[] = [];
@@ -2465,7 +2486,7 @@ module ts {
24652486
getSymbolModifiers(symbol),
24662487
new TypeScript.TextSpan(node.getStart(), node.getWidth()),
24672488
totalParts,
2468-
[new SymbolDisplayPart(symbol.getDocumentationComment(), SymbolDisplayPartKind.text, /*symbol:*/ null)]);
2489+
documentationParts);
24692490
}
24702491

24712492
function getTypeAtPosition(fileName: string, position: number): TypeInfo {

0 commit comments

Comments
 (0)