Skip to content

Commit eba38af

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Add DartType.getDisplayString()
[email protected], [email protected] Change-Id: I3849ccb4d2359fddf470b42280e3ea21e38f3664 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127344 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent f7aff73 commit eba38af

File tree

5 files changed

+57
-101
lines changed

5 files changed

+57
-101
lines changed

pkg/analysis_server/test/analysis/get_hover_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class B<T> extends A<T> with M1, M2<int> implements I1<int, String>, I2 {}
5454
expect(
5555
hover.elementDescription,
5656
'class B<T> extends A<T> with M1, M2<int> '
57-
'implements I1<int, String>, I2');
57+
'implements I1<int, String>, I2<dynamic>');
5858
expect(hover.staticType, isNull);
5959
expect(hover.propagatedType, isNull);
6060
}

pkg/analyzer/lib/dart/element/type.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ abstract class DartType {
115115
/// Return the nullability suffix of this type.
116116
NullabilitySuffix get nullabilitySuffix;
117117

118+
/// Return the presentation of this type as it should appear when presented
119+
/// to users in contexts such as error messages.
120+
///
121+
/// If [withNullability] is `true`, then [NullabilitySuffix.question] and
122+
/// [NullabilitySuffix.star] will be be represented as `?` and `*`.
123+
/// [NullabilitySuffix.none] does not have any explicit presentation.
124+
///
125+
/// If [withNullability] is `false`, nullability suffixes will not be
126+
/// included into the presentation.
127+
///
128+
/// Clients should not depend on the content of the returned value as it will
129+
/// be changed if doing so would improve the UX.
130+
String getDisplayString({bool withNullability = false});
131+
118132
/// If this type is a [TypeParameterType], returns its bound if it has one, or
119133
/// [objectType] otherwise.
120134
///

pkg/analyzer/lib/error/listener.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class ErrorReporter {
202202
if (name != null && name.isNotEmpty) {
203203
StringBuffer buffer = new StringBuffer();
204204
buffer.write(name);
205-
(type as TypeImpl).appendTo(buffer);
205+
(type as TypeImpl).appendTo(buffer, withNullability: false);
206206
return buffer.toString();
207207
}
208208
}

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4610,7 +4610,7 @@ class GenericFunctionTypeElementImpl extends ElementImpl
46104610
void appendTo(StringBuffer buffer) {
46114611
DartType type = returnType;
46124612
if (type is TypeImpl) {
4613-
type.appendTo(buffer);
4613+
type.appendTo(buffer, withNullability: false);
46144614
buffer.write(' Function');
46154615
} else {
46164616
buffer.write('Function');

pkg/analyzer/lib/src/dart/element/type.dart

Lines changed: 40 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class DynamicTypeImpl extends TypeImpl {
6464
@override
6565
bool operator ==(Object object) => identical(object, this);
6666

67+
@override
68+
void appendTo(StringBuffer buffer, {@required bool withNullability}) {
69+
buffer.write('dynamic');
70+
}
71+
6772
@override
6873
DartType replaceTopAndBottom(TypeProvider typeProvider,
6974
{bool isCovariant = true}) {
@@ -142,50 +147,6 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
142147
@override
143148
List<TypeParameterElement> get boundTypeParameters => typeFormals;
144149

145-
@override
146-
String get displayName {
147-
if (name == null || name.isEmpty) {
148-
// Function types have an empty name when they are defined implicitly by
149-
// either a closure or as part of a parameter declaration.
150-
StringBuffer buffer = new StringBuffer();
151-
appendTo(buffer);
152-
if (nullabilitySuffix == NullabilitySuffix.question) {
153-
buffer.write('?');
154-
}
155-
return buffer.toString();
156-
}
157-
158-
List<DartType> typeArguments = this.typeArguments;
159-
160-
bool allTypeArgumentsAreDynamic() {
161-
for (DartType type in typeArguments) {
162-
if (type != null && !type.isDynamic) {
163-
return false;
164-
}
165-
}
166-
return true;
167-
}
168-
169-
StringBuffer buffer = new StringBuffer();
170-
buffer.write(name);
171-
// If there is at least one non-dynamic type, then list them out.
172-
if (!allTypeArgumentsAreDynamic()) {
173-
buffer.write("<");
174-
for (int i = 0; i < typeArguments.length; i++) {
175-
if (i != 0) {
176-
buffer.write(", ");
177-
}
178-
DartType typeArg = typeArguments[i];
179-
buffer.write(typeArg.displayName);
180-
}
181-
buffer.write(">");
182-
}
183-
if (nullabilitySuffix == NullabilitySuffix.question) {
184-
buffer.write('?');
185-
}
186-
return buffer.toString();
187-
}
188-
189150
@override
190151
FunctionTypedElement get element {
191152
var element = super.element;
@@ -294,7 +255,7 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
294255
}
295256

296257
@override
297-
void appendTo(StringBuffer buffer, {bool withNullability = false}) {
258+
void appendTo(StringBuffer buffer, {@required bool withNullability}) {
298259
// TODO(paulberry): eliminate code duplication with
299260
// _ElementWriter.writeType. See issue #35818.
300261
if (typeFormals.isNotEmpty) {
@@ -944,39 +905,6 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
944905
return _constructors;
945906
}
946907

947-
@override
948-
String get displayName {
949-
List<DartType> typeArguments = this.typeArguments;
950-
951-
bool allTypeArgumentsAreDynamic() {
952-
for (DartType type in typeArguments) {
953-
if (type != null && !type.isDynamic) {
954-
return false;
955-
}
956-
}
957-
return true;
958-
}
959-
960-
StringBuffer buffer = new StringBuffer();
961-
buffer.write(name);
962-
// If there is at least one non-dynamic type, then list them out.
963-
if (!allTypeArgumentsAreDynamic()) {
964-
buffer.write("<");
965-
for (int i = 0; i < typeArguments.length; i++) {
966-
if (i != 0) {
967-
buffer.write(", ");
968-
}
969-
DartType typeArg = typeArguments[i];
970-
buffer.write(typeArg.displayName);
971-
}
972-
buffer.write(">");
973-
}
974-
if (nullabilitySuffix == NullabilitySuffix.question) {
975-
buffer.write('?');
976-
}
977-
return buffer.toString();
978-
}
979-
980908
@override
981909
ClassElement get element => super.element;
982910

@@ -1175,8 +1103,8 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
11751103
}
11761104

11771105
@override
1178-
void appendTo(StringBuffer buffer, {bool withNullability = false}) {
1179-
buffer.write(name);
1106+
void appendTo(StringBuffer buffer, {@required bool withNullability}) {
1107+
buffer.write(element.name);
11801108
int argumentCount = typeArguments.length;
11811109
if (argumentCount > 0) {
11821110
buffer.write("<");
@@ -1902,6 +1830,14 @@ class NeverTypeImpl extends TypeImpl {
19021830
@override
19031831
bool operator ==(Object object) => identical(object, this);
19041832

1833+
@override
1834+
void appendTo(StringBuffer buffer, {@required bool withNullability}) {
1835+
buffer.write('Never');
1836+
if (withNullability) {
1837+
_appendNullability(buffer);
1838+
}
1839+
}
1840+
19051841
@override
19061842
DartType replaceTopAndBottom(TypeProvider typeProvider,
19071843
{bool isCovariant = true}) {
@@ -1963,7 +1899,9 @@ abstract class TypeImpl implements DartType {
19631899
TypeImpl(this._element, this.name);
19641900

19651901
@override
1966-
String get displayName => name;
1902+
String get displayName {
1903+
return getDisplayString(withNullability: false);
1904+
}
19671905

19681906
@override
19691907
Element get element => _element;
@@ -2028,15 +1966,13 @@ abstract class TypeImpl implements DartType {
20281966
/**
20291967
* Append a textual representation of this type to the given [buffer].
20301968
*/
2031-
void appendTo(StringBuffer buffer, {bool withNullability = false}) {
2032-
if (name == null) {
2033-
buffer.write("<unnamed type>");
2034-
} else {
2035-
buffer.write(name);
2036-
}
2037-
if (withNullability) {
2038-
_appendNullability(buffer);
2039-
}
1969+
void appendTo(StringBuffer buffer, {@required bool withNullability});
1970+
1971+
@override
1972+
String getDisplayString({bool withNullability = false}) {
1973+
var buffer = StringBuffer();
1974+
appendTo(buffer, withNullability: withNullability);
1975+
return buffer.toString();
20401976
}
20411977

20421978
/// Replaces all covariant occurrences of `dynamic`, `Object`, and `void` with
@@ -2061,9 +1997,7 @@ abstract class TypeImpl implements DartType {
20611997

20621998
@override
20631999
String toString({bool withNullability = false}) {
2064-
StringBuffer buffer = new StringBuffer();
2065-
appendTo(buffer, withNullability: withNullability);
2066-
return buffer.toString();
2000+
return getDisplayString(withNullability: withNullability);
20672001
}
20682002

20692003
/**
@@ -2081,11 +2015,6 @@ abstract class TypeImpl implements DartType {
20812015
TypeImpl withNullability(NullabilitySuffix nullabilitySuffix);
20822016

20832017
void _appendNullability(StringBuffer buffer) {
2084-
if (isDynamic || isVoid) {
2085-
// These types don't have nullability variations, so don't append
2086-
// anything.
2087-
return;
2088-
}
20892018
switch (nullabilitySuffix) {
20902019
case NullabilitySuffix.question:
20912020
buffer.write('?');
@@ -2181,6 +2110,14 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
21812110
other.nullabilitySuffix == nullabilitySuffix;
21822111
}
21832112

2113+
@override
2114+
void appendTo(StringBuffer buffer, {@required bool withNullability}) {
2115+
buffer.write(element.name);
2116+
if (withNullability) {
2117+
_appendNullability(buffer);
2118+
}
2119+
}
2120+
21842121
@override
21852122
DartType replaceTopAndBottom(TypeProvider typeProvider,
21862123
{bool isCovariant = true}) {
@@ -2319,6 +2256,11 @@ class VoidTypeImpl extends TypeImpl implements VoidType {
23192256
@override
23202257
bool operator ==(Object object) => identical(object, this);
23212258

2259+
@override
2260+
void appendTo(StringBuffer buffer, {@required bool withNullability}) {
2261+
buffer.write('void');
2262+
}
2263+
23222264
@override
23232265
DartType replaceTopAndBottom(TypeProvider typeProvider,
23242266
{bool isCovariant = true}) {

0 commit comments

Comments
 (0)