mirror of
https://github.com/dart-lang/sdk
synced 2024-10-03 01:25:41 +00:00
[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:
parent
fd2c27ff4e
commit
bdf4528871
|
@ -62,8 +62,10 @@ class DartUnitHoverComputer {
|
||||||
// element
|
// element
|
||||||
var element = ElementLocator.locate(node);
|
var element = ElementLocator.locate(node);
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
// variable, if synthetic accessor
|
// use the non-synthetic element to get things like dartdoc from the
|
||||||
if (element is PropertyAccessorElement) {
|
// underlying field (and resolved type args), except for `enum.values`
|
||||||
|
// because that will resolve to the enum itself.
|
||||||
|
if (_useNonSyntheticElement(element)) {
|
||||||
element = element.nonSynthetic;
|
element = element.nonSynthetic;
|
||||||
}
|
}
|
||||||
// description
|
// description
|
||||||
|
@ -255,6 +257,20 @@ class DartUnitHoverComputer {
|
||||||
return staticType?.getDisplayString(withNullability: true);
|
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(
|
static Documentation? computeDocumentation(
|
||||||
DartdocDirectiveInfo dartdocInfo, Element elementBeingDocumented,
|
DartdocDirectiveInfo dartdocInfo, Element elementBeingDocumented,
|
||||||
{bool includeSummary = false}) {
|
{bool includeSummary = false}) {
|
||||||
|
|
|
@ -152,6 +152,42 @@ class HoverTest extends AbstractLspAnalysisServerTest {
|
||||||
Future<void> test_dartDocPreference_unset() =>
|
Future<void> test_dartDocPreference_unset() =>
|
||||||
assertDocumentation(null, includesSummary: true, includesFull: true);
|
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 {
|
Future<void> test_forLoop_declaredVariable() async {
|
||||||
final content = '''
|
final content = '''
|
||||||
void f() {
|
void f() {
|
||||||
|
|
Loading…
Reference in a new issue