Skip to content

Commit efbf539

Browse files
committed
Add support of contextual quick info
1 parent c8e43d8 commit efbf539

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

src/services/services.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ namespace ts {
283283
}
284284
}
285285

286+
enum ContextualDocumentationType {
287+
None = "None",
288+
GetAccessor = "GetAccessor",
289+
SetAccessor = "SetAccessor"
290+
}
291+
286292
class SymbolObject implements Symbol {
287293
flags: SymbolFlags;
288294
escapedName: __String;
@@ -293,6 +299,8 @@ namespace ts {
293299
// symbol has no doc comment, then the empty array will be returned.
294300
documentationComment?: SymbolDisplayPart[];
295301

302+
contextualDocumentationComment?: Map<SymbolDisplayPart[]>;
303+
296304
// Undefined is used to indicate the value has not been computed. If, after computing, the
297305
// symbol has no JSDoc tags, then the empty array will be returned.
298306
tags?: JSDocTagInfo[];
@@ -330,6 +338,27 @@ namespace ts {
330338
return this.documentationComment;
331339
}
332340

341+
getContextualDocumentationComment(context: Node | undefined, checker: TypeChecker | undefined): SymbolDisplayPart[] {
342+
switch (context?.kind) {
343+
case SyntaxKind.GetAccessor:
344+
return this.getContextualDocumentationCommentCached(ContextualDocumentationType.GetAccessor, filter(this.declarations, isGetAccessor), checker);
345+
case SyntaxKind.SetAccessor:
346+
return this.getContextualDocumentationCommentCached(ContextualDocumentationType.SetAccessor, filter(this.declarations, isSetAccessor), checker);
347+
default:
348+
return this.getDocumentationComment(checker);
349+
}
350+
}
351+
352+
getContextualDocumentationCommentCached(type: ContextualDocumentationType, declarations: Declaration[], checker: TypeChecker | undefined): SymbolDisplayPart[] {
353+
if (!this.contextualDocumentationComment) {
354+
this.contextualDocumentationComment = createMultiMap<SymbolDisplayPart>();
355+
}
356+
if (!this.contextualDocumentationComment.has(type)) {
357+
this.contextualDocumentationComment.set(type, getDocumentationComment(declarations, checker));
358+
}
359+
return this.contextualDocumentationComment.get(type)!;
360+
}
361+
333362
getJsDocTags(): JSDocTagInfo[] {
334363
if (this.tags === undefined) {
335364
this.tags = JsDoc.getJsDocTagsFromDeclarations(this.declarations);

src/services/symbolDisplay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ namespace ts.SymbolDisplay {
500500
}
501501

502502
if (documentation.length === 0 && !hasMultipleSignatures) {
503-
documentation = symbol.getDocumentationComment(typeChecker);
503+
documentation = symbol.getContextualDocumentationComment(enclosingDeclaration, typeChecker);
504504
}
505505

506506
if (documentation.length === 0 && symbolFlags & SymbolFlags.Property) {

src/services/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ namespace ts {
4141
getName(): string;
4242
getDeclarations(): Declaration[] | undefined;
4343
getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[];
44+
/* @internal */
45+
getContextualDocumentationComment(context: Node | undefined, checker: TypeChecker | undefined): SymbolDisplayPart[]
4446
getJsDocTags(): JSDocTagInfo[];
4547
}
4648

tests/cases/fourslash/commentsClassMembers.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,22 @@ verify.quickInfos({
248248
1: ["class c1", "This is comment for c1"],
249249
2: ["(property) c1.p1: number", "p1 is property of c1"],
250250
3: ["(method) c1.p2(b: number): number", "sum with property"],
251-
6: ["(property) c1.p3: number", "getter property 1\nsetter property 1"],
251+
6: ["(property) c1.p3: number", "getter property 1"],
252252
"8q": ["(method) c1.p2(b: number): number", "sum with property"],
253-
10: ["(property) c1.p3: number", "getter property 1\nsetter property 1"],
253+
10: ["(property) c1.p3: number", "setter property 1"],
254254
"13q": ["(method) c1.p2(b: number): number", "sum with property"],
255255
14: ["(property) c1.pp1: number", "pp1 is property of c1"],
256256
15: ["(method) c1.pp2(b: number): number", "sum with property"],
257-
18: ["(property) c1.pp3: number", "getter property 2\nsetter property 2"],
257+
18: ["(property) c1.pp3: number", "getter property 2"],
258258
"20q": ["(method) c1.pp2(b: number): number", "sum with property"],
259-
22: ["(property) c1.pp3: number", "getter property 2\nsetter property 2"],
259+
22: ["(property) c1.pp3: number", "setter property 2"],
260260
"25q": ["(method) c1.pp2(b: number): number", "sum with property"],
261261
26: ["constructor c1(): c1", "Constructor method"],
262262
27: ["(property) c1.s1: number", "s1 is static property of c1"],
263263
28: ["(method) c1.s2(b: number): number", "static sum with property"],
264-
32: ["(property) c1.s3: number", "static getter property\nsetter property 3"],
264+
32: ["(property) c1.s3: number", "static getter property"],
265265
"35q": ["(method) c1.s2(b: number): number", "static sum with property"],
266-
37: ["(property) c1.s3: number", "static getter property\nsetter property 3"],
266+
37: ["(property) c1.s3: number", "setter property 3"],
267267
"42q": ["(method) c1.s2(b: number): number", "static sum with property"],
268268
43: "(property) c1.nc_p1: number",
269269
44: "(method) c1.nc_p2(b: number): number",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
//// class Test {
4+
//// constructor() {
5+
//// this.value;
6+
//// }
7+
////
8+
//// /** Getter text */
9+
//// get val/*1*/ue() {
10+
//// return this.value;
11+
//// }
12+
////
13+
//// /** Setter text */
14+
//// set val/*2*/ue(value) {
15+
//// this.value = value;
16+
//// }
17+
//// }
18+
19+
goTo.marker("1");
20+
verify.quickInfoIs("(property) Test.value: any", "Getter text");
21+
22+
goTo.marker("2");
23+
verify.quickInfoIs("(property) Test.value: any", "Setter text");

0 commit comments

Comments
 (0)