diff --git a/src/services/services.ts b/src/services/services.ts index c0d386291a5..0ad6ea996e7 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -293,6 +293,9 @@ namespace ts { // symbol has no doc comment, then the empty array will be returned. documentationComment?: SymbolDisplayPart[]; + contextualGetAccessorDocumentationComment?: SymbolDisplayPart[]; + contextualSetAccessorDocumentationComment?: SymbolDisplayPart[]; + // Undefined is used to indicate the value has not been computed. If, after computing, the // symbol has no JSDoc tags, then the empty array will be returned. tags?: JSDocTagInfo[]; @@ -330,6 +333,25 @@ namespace ts { return this.documentationComment; } + getContextualDocumentationComment(context: Node | undefined, checker: TypeChecker | undefined): SymbolDisplayPart[] { + switch (context?.kind) { + case SyntaxKind.GetAccessor: + if (!this.contextualGetAccessorDocumentationComment) { + this.contextualGetAccessorDocumentationComment = emptyArray; + this.contextualGetAccessorDocumentationComment = getDocumentationComment(filter(this.declarations, isGetAccessor), checker); + } + return this.contextualGetAccessorDocumentationComment; + case SyntaxKind.SetAccessor: + if (!this.contextualSetAccessorDocumentationComment) { + this.contextualSetAccessorDocumentationComment = emptyArray; + this.contextualSetAccessorDocumentationComment = getDocumentationComment(filter(this.declarations, isSetAccessor), checker); + } + return this.contextualSetAccessorDocumentationComment; + default: + return this.getDocumentationComment(checker); + } + } + getJsDocTags(): JSDocTagInfo[] { if (this.tags === undefined) { this.tags = JsDoc.getJsDocTagsFromDeclarations(this.declarations); diff --git a/src/services/symbolDisplay.ts b/src/services/symbolDisplay.ts index b8122428e41..c9af01faef8 100644 --- a/src/services/symbolDisplay.ts +++ b/src/services/symbolDisplay.ts @@ -502,7 +502,7 @@ namespace ts.SymbolDisplay { } if (documentation.length === 0 && !hasMultipleSignatures) { - documentation = symbol.getDocumentationComment(typeChecker); + documentation = symbol.getContextualDocumentationComment(enclosingDeclaration, typeChecker); } if (documentation.length === 0 && symbolFlags & SymbolFlags.Property) { diff --git a/src/services/types.ts b/src/services/types.ts index 24d04fb438e..13c68364664 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -41,6 +41,8 @@ namespace ts { getName(): string; getDeclarations(): Declaration[] | undefined; getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[]; + /* @internal */ + getContextualDocumentationComment(context: Node | undefined, checker: TypeChecker | undefined): SymbolDisplayPart[] getJsDocTags(): JSDocTagInfo[]; } diff --git a/tests/cases/fourslash/commentsClassMembers.ts b/tests/cases/fourslash/commentsClassMembers.ts index 4457dc617cd..718b9e7816e 100644 --- a/tests/cases/fourslash/commentsClassMembers.ts +++ b/tests/cases/fourslash/commentsClassMembers.ts @@ -248,22 +248,22 @@ verify.quickInfos({ 1: ["class c1", "This is comment for c1"], 2: ["(property) c1.p1: number", "p1 is property of c1"], 3: ["(method) c1.p2(b: number): number", "sum with property"], - 6: ["(property) c1.p3: number", "getter property 1\nsetter property 1"], + 6: ["(property) c1.p3: number", "getter property 1"], "8q": ["(method) c1.p2(b: number): number", "sum with property"], - 10: ["(property) c1.p3: number", "getter property 1\nsetter property 1"], + 10: ["(property) c1.p3: number", "setter property 1"], "13q": ["(method) c1.p2(b: number): number", "sum with property"], 14: ["(property) c1.pp1: number", "pp1 is property of c1"], 15: ["(method) c1.pp2(b: number): number", "sum with property"], - 18: ["(property) c1.pp3: number", "getter property 2\nsetter property 2"], + 18: ["(property) c1.pp3: number", "getter property 2"], "20q": ["(method) c1.pp2(b: number): number", "sum with property"], - 22: ["(property) c1.pp3: number", "getter property 2\nsetter property 2"], + 22: ["(property) c1.pp3: number", "setter property 2"], "25q": ["(method) c1.pp2(b: number): number", "sum with property"], 26: ["constructor c1(): c1", "Constructor method"], 27: ["(property) c1.s1: number", "s1 is static property of c1"], 28: ["(method) c1.s2(b: number): number", "static sum with property"], - 32: ["(property) c1.s3: number", "static getter property\nsetter property 3"], + 32: ["(property) c1.s3: number", "static getter property"], "35q": ["(method) c1.s2(b: number): number", "static sum with property"], - 37: ["(property) c1.s3: number", "static getter property\nsetter property 3"], + 37: ["(property) c1.s3: number", "setter property 3"], "42q": ["(method) c1.s2(b: number): number", "static sum with property"], 43: "(property) c1.nc_p1: number", 44: "(method) c1.nc_p2(b: number): number", diff --git a/tests/cases/fourslash/quickInfoForGetterAndSetter.ts b/tests/cases/fourslash/quickInfoForGetterAndSetter.ts new file mode 100644 index 00000000000..723836488c2 --- /dev/null +++ b/tests/cases/fourslash/quickInfoForGetterAndSetter.ts @@ -0,0 +1,23 @@ +/// + +//// class Test { +//// constructor() { +//// this.value; +//// } +//// +//// /** Getter text */ +//// get val/*1*/ue() { +//// return this.value; +//// } +//// +//// /** Setter text */ +//// set val/*2*/ue(value) { +//// this.value = value; +//// } +//// } + +goTo.marker("1"); +verify.quickInfoIs("(property) Test.value: any", "Getter text"); + +goTo.marker("2"); +verify.quickInfoIs("(property) Test.value: any", "Setter text");