Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bf7e9d1

Browse files
parloughcommit-bot@chromium.org
authored andcommitted
Allow analyzer to fallback to getter documentation for setters
The analyzer server firsts looks for the setter's dartdoc, then the first parent setter's dartdoc, then the closest getter's dartdoc. Fixes #38963 Bug: dart-lang/sdk#38963 Change-Id: I09f7ae0e5aa49d2536ebefd44dcfd3d6101bb0a6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/148566 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Lasse R.H. Nielsen <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent f0450f3 commit bf7e9d1

File tree

2 files changed

+138
-12
lines changed

2 files changed

+138
-12
lines changed

pkg/analysis_server/lib/src/computer/computer_hover.dart

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,46 @@ class DartUnitHoverComputer {
155155
// parameter for a field that does not exist.
156156
return null;
157157
}
158-
// The documentation of the element itself.
159-
if (element.documentationComment != null) {
160-
return dartdocInfo.processDartdoc(element.documentationComment);
161-
}
162-
// Look for documentation comments of overridden members.
158+
159+
Element documentedElement;
160+
Element documentedGetter;
161+
162+
// Look for documentation comments of overridden members
163163
var overridden = findOverriddenElements(element);
164-
for (var superElement in [
164+
for (var candidate in [
165+
element,
165166
...overridden.superElements,
166167
...overridden.interfaceElements
167168
]) {
168-
var rawDoc = superElement.documentationComment;
169-
if (rawDoc != null) {
170-
var interfaceClass = superElement.enclosingElement;
171-
return dartdocInfo.processDartdoc(rawDoc) +
172-
'\n\nCopied from `${interfaceClass.displayName}`.';
169+
if (candidate.documentationComment != null) {
170+
documentedElement = candidate;
171+
break;
172+
}
173+
if (documentedGetter == null &&
174+
candidate is PropertyAccessorElement &&
175+
candidate.isSetter) {
176+
var getter = candidate.correspondingGetter;
177+
if (getter != null && getter.documentationComment != null) {
178+
documentedGetter = getter;
179+
}
173180
}
174181
}
175-
return null;
182+
183+
// Use documentation of a corresponding getter if setters don't have it
184+
documentedElement ??= documentedGetter;
185+
if (documentedElement == null) {
186+
return null;
187+
}
188+
189+
var rawDoc = documentedElement.documentationComment;
190+
var result = dartdocInfo.processDartdoc(rawDoc);
191+
192+
var documentedElementClass = documentedElement.enclosingElement;
193+
if (documentedElementClass != element.enclosingElement) {
194+
result += '\n\nCopied from `${documentedElementClass.displayName}`.';
195+
}
196+
197+
return result;
176198
}
177199

178200
static DartType _getTypeOfDeclarationOrReference(Expression node) {

pkg/analysis_server/test/analysis/get_hover_test.dart

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,108 @@ main() {
723723
expect(hover.elementKind, 'parameter');
724724
expect(hover.staticType, 'int');
725725
}
726+
727+
Future<void> test_setter_hasDocumentation() async {
728+
addTestFile('''
729+
class A {
730+
/// getting
731+
int get foo => 42;
732+
/// setting
733+
set foo(int x) {}
734+
}
735+
main(A a) {
736+
a.foo = 123;
737+
}
738+
''');
739+
var hover = await prepareHover('foo = ');
740+
expect(hover.containingClassDescription, 'A');
741+
expect(hover.dartdoc, '''setting''');
742+
expect(hover.elementDescription, 'void set foo(int x)');
743+
expect(hover.elementKind, 'setter');
744+
}
745+
746+
Future<void> test_setter_noDocumentation() async {
747+
addTestFile('''
748+
class A {
749+
/// getting
750+
int get foo => 42;
751+
set foo(int x) {}
752+
}
753+
main(A a) {
754+
a.foo = 123;
755+
}
756+
''');
757+
var hover = await prepareHover('foo = ');
758+
expect(hover.containingClassDescription, 'A');
759+
expect(hover.dartdoc, '''getting''');
760+
expect(hover.elementDescription, 'void set foo(int x)');
761+
expect(hover.elementKind, 'setter');
762+
}
763+
764+
Future<void> test_setter_super_hasDocumentation() async {
765+
addTestFile('''
766+
class A {
767+
/// pgetting
768+
int get foo => 42;
769+
/// psetting
770+
set foo(int x) {}
771+
}
772+
class B extends A {
773+
/// getting
774+
int get foo => 42;
775+
set foo(int x) {}
776+
}
777+
main(B b) {
778+
b.foo = 123;
779+
}
780+
''');
781+
var hover = await prepareHover('foo = ');
782+
expect(hover.containingClassDescription, 'B');
783+
expect(hover.dartdoc, '''psetting\n\nCopied from `A`.''');
784+
expect(hover.elementDescription, 'void set foo(int x)');
785+
expect(hover.elementKind, 'setter');
786+
}
787+
788+
Future<void> test_setter_super_noDocumentation() async {
789+
addTestFile('''
790+
class A {
791+
/// pgetting
792+
int get foo => 42;
793+
set foo(int x) {}
794+
}
795+
class B extends A {
796+
int get foo => 42;
797+
set foo(int x) {}
798+
}
799+
main(B b) {
800+
b.foo = 123;
801+
}
802+
''');
803+
var hover = await prepareHover('foo = ');
804+
expect(hover.containingClassDescription, 'B');
805+
expect(hover.dartdoc, '''pgetting\n\nCopied from `A`.''');
806+
expect(hover.elementDescription, 'void set foo(int x)');
807+
expect(hover.elementKind, 'setter');
808+
}
809+
810+
@failingTest
811+
Future<void> test_setter_super_noSetter() async {
812+
addTestFile('''
813+
class A {
814+
/// pgetting
815+
int get foo => 42;
816+
}
817+
class B extends A {
818+
set foo(int x) {}
819+
}
820+
main(B b) {
821+
b.foo = 123;
822+
}
823+
''');
824+
var hover = await prepareHover('foo = ');
825+
expect(hover.containingClassDescription, 'B');
826+
expect(hover.dartdoc, '''pgetting''');
827+
expect(hover.elementDescription, 'void set foo(int x)');
828+
expect(hover.elementKind, 'setter');
829+
}
726830
}

0 commit comments

Comments
 (0)