[analysis_server] Show correct type for enum.values

Fixes https://github.com/Dart-Code/Dart-Code/issues/4877

Change-Id: Ie9b7f223f71c4c79c141ca69356340ef8b15aceb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350720
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2024-02-06 22:58:09 +00:00 committed by Commit Queue
parent fd2c27ff4e
commit bdf4528871
2 changed files with 54 additions and 2 deletions

View file

@ -62,8 +62,10 @@ class DartUnitHoverComputer {
// element
var element = ElementLocator.locate(node);
if (element != null) {
// variable, if synthetic accessor
if (element is PropertyAccessorElement) {
// use the non-synthetic element to get things like dartdoc from the
// underlying field (and resolved type args), except for `enum.values`
// because that will resolve to the enum itself.
if (_useNonSyntheticElement(element)) {
element = element.nonSynthetic;
}
// description
@ -255,6 +257,20 @@ class DartUnitHoverComputer {
return staticType?.getDisplayString(withNullability: true);
}
/// Whether to use the non-synthetic element for hover information.
///
/// Usually we want this because the non-synthetic element will include the
/// users DartDoc and show any type arguments as declared.
///
/// For enum.values, nonSynthetic returns the enum itself which causes
/// incorrect types to be shown and so we stick with the synthetic getter.
bool _useNonSyntheticElement(Element element) {
return element is PropertyAccessorElement &&
!(element.enclosingElement is EnumElement &&
element.name == 'values' &&
element.isSynthetic);
}
static Documentation? computeDocumentation(
DartdocDirectiveInfo dartdocInfo, Element elementBeingDocumented,
{bool includeSummary = false}) {

View file

@ -152,6 +152,42 @@ class HoverTest extends AbstractLspAnalysisServerTest {
Future<void> test_dartDocPreference_unset() =>
assertDocumentation(null, includesSummary: true, includesFull: true);
Future<void> test_enum_member() async {
final content = '''
enum MyEnum { one }
void f() {
MyEnum.[!o^ne!];
}
''';
final expected = '''
```dart
MyEnum one
```
Type: `MyEnum`
*package:test/main.dart*''';
await assertStringContents(content, equals(expected));
}
Future<void> test_enum_values() async {
final content = '''
enum MyEnum { one }
void f() {
MyEnum.[!va^lues!];
}
''';
final expected = '''
```dart
List<MyEnum> get values
```
Type: `List<MyEnum>`
*package:test/main.dart*''';
await assertStringContents(content, equals(expected));
}
Future<void> test_forLoop_declaredVariable() async {
final content = '''
void f() {