Skip to content

Commit abecefd

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Forward compatible portion of CL that does not set types for identifiers that are not expressions.
The full CL: https://dart-review.googlesource.com/c/sdk/+/119761 Change-Id: I9c909a4e1eec5d05f5718849ea45da79f01c5291 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121643 Reviewed-by: Paul Berry <paulberry@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 37a0c1e commit abecefd

26 files changed

+509
-342
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,15 @@ class DartUnitHighlightsComputer {
141141
}
142142

143143
bool _addIdentifierRegion_dynamicType(SimpleIdentifier node) {
144-
// should be variable
145144
Element element = node.staticElement;
146-
if (element is! VariableElement) {
147-
return false;
148-
}
149-
// has dynamic static type
150-
DartType staticType = node.staticType;
151-
if (staticType == null || !staticType.isDynamic) {
152-
return false;
145+
if (element is VariableElement) {
146+
DartType staticType = element.type;
147+
if (staticType == null || !staticType.isDynamic) {
148+
return false;
149+
}
150+
return _addRegion_node(node, HighlightRegionType.DYNAMIC_TYPE);
153151
}
154-
// OK
155-
return _addRegion_node(node, HighlightRegionType.DYNAMIC_TYPE);
152+
return false;
156153
}
157154

158155
bool _addIdentifierRegion_field(SimpleIdentifier node) {

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,24 @@ class DartUnitHighlightsComputer2 {
144144
}
145145

146146
bool _addIdentifierRegion_dynamicLocal(SimpleIdentifier node) {
147-
// has dynamic static type
148-
DartType staticType = node.staticType;
149-
if (staticType == null || !staticType.isDynamic) {
150-
return false;
151-
}
152-
// OK
153147
Element element = node.staticElement;
154148
if (element is LocalVariableElement) {
155-
HighlightRegionType type = node.inDeclarationContext()
156-
? HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_DECLARATION
157-
: HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_REFERENCE;
158-
return _addRegion_node(node, type);
149+
var elementType = element.type;
150+
if (elementType?.isDynamic == true) {
151+
HighlightRegionType type = node.inDeclarationContext()
152+
? HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_DECLARATION
153+
: HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_REFERENCE;
154+
return _addRegion_node(node, type);
155+
}
159156
}
160157
if (element is ParameterElement) {
161-
HighlightRegionType type = node.inDeclarationContext()
162-
? HighlightRegionType.DYNAMIC_PARAMETER_DECLARATION
163-
: HighlightRegionType.DYNAMIC_PARAMETER_REFERENCE;
164-
return _addRegion_node(node, type);
158+
var elementType = element.type;
159+
if (elementType?.isDynamic == true) {
160+
HighlightRegionType type = node.inDeclarationContext()
161+
? HighlightRegionType.DYNAMIC_PARAMETER_DECLARATION
162+
: HighlightRegionType.DYNAMIC_PARAMETER_REFERENCE;
163+
return _addRegion_node(node, type);
164+
}
165165
}
166166
return false;
167167
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,9 @@ class DartUnitHoverComputer {
111111
// types
112112
{
113113
AstNode parent = expression.parent;
114-
DartType staticType = null;
115-
if (element is ParameterElement) {
116-
staticType = element.type;
117-
} else if (element == null || element is VariableElement) {
118-
staticType = expression.staticType;
114+
DartType staticType;
115+
if (element == null || element is VariableElement) {
116+
staticType = _getTypeOfDeclarationOrReference(node);
119117
}
120118
if (parent is MethodInvocation && parent.methodName == expression) {
121119
staticType = parent.staticInvokeType;
@@ -166,5 +164,21 @@ class DartUnitHoverComputer {
166164
return null;
167165
}
168166

167+
static DartType _getTypeOfDeclarationOrReference(Expression node) {
168+
if (node is SimpleIdentifier) {
169+
var element = node.staticElement;
170+
if (element is VariableElement) {
171+
if (node.inDeclarationContext()) {
172+
return element.type;
173+
}
174+
var parent2 = node.parent.parent;
175+
if (parent2 is NamedExpression && parent2.name.label == node) {
176+
return element.type;
177+
}
178+
}
179+
}
180+
return node.staticType;
181+
}
182+
169183
static String _safeToString(obj) => obj?.toString();
170184
}

pkg/analysis_server/lib/src/domains/analysis/navigation_dart.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor {
184184
if (node.type == null) {
185185
Token token = node.keyword;
186186
if (token?.keyword == Keyword.VAR) {
187-
DartType inferredType = node.identifier?.staticType;
187+
DartType inferredType = node.declaredElement?.type;
188188
Element element = inferredType?.element;
189189
if (element != null) {
190190
computer._addRegionForToken(token, element);
@@ -294,12 +294,12 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor {
294294
* inferred type.
295295
*/
296296
Element getCommonElement(List<VariableDeclaration> variables) {
297-
Element firstElement = variables[0].name?.staticType?.element;
297+
Element firstElement = variables[0].declaredElement.type?.element;
298298
if (firstElement == null) {
299299
return null;
300300
}
301301
for (int i = 1; i < variables.length; i++) {
302-
Element element = variables[1].name?.staticType?.element;
302+
Element element = variables[1].declaredElement.type?.element;
303303
if (element != firstElement) {
304304
return null;
305305
}

pkg/analysis_server/lib/src/services/completion/dart/common_usage_sorter.dart

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,11 @@ class CommonUsageSorter implements DartContributionSorter {
5757
var target = _getCompletionTarget(request);
5858
if (target != null) {
5959
var visitor = new _BestTypeVisitor(target.entity);
60-
DartType type = target.containingNode.accept(visitor);
61-
if (type != null) {
62-
Element typeElem = type.element;
63-
if (typeElem != null) {
64-
LibraryElement libElem = typeElem.library;
65-
if (libElem != null) {
66-
_updateInvocationRelevance(type, libElem, suggestions);
67-
}
60+
var typeElem = target.containingNode.accept(visitor);
61+
if (typeElem != null) {
62+
LibraryElement libElem = typeElem.library;
63+
if (libElem != null) {
64+
_updateInvocationRelevance(typeElem.name, libElem, suggestions);
6865
}
6966
}
7067
}
@@ -74,9 +71,8 @@ class CommonUsageSorter implements DartContributionSorter {
7471
* Adjusts the relevance of all method suggestions based upon the given
7572
* target type and library.
7673
*/
77-
void _updateInvocationRelevance(DartType type, LibraryElement libElem,
74+
void _updateInvocationRelevance(String typeName, LibraryElement libElem,
7875
Iterable<CompletionSuggestion> suggestions) {
79-
String typeName = type.name;
8076
List<String> selectors = selectorRelevance['${libElem.name}.$typeName'];
8177
if (selectors != null) {
8278
for (CompletionSuggestion suggestion in suggestions) {
@@ -102,7 +98,7 @@ class CommonUsageSorter implements DartContributionSorter {
10298
/**
10399
* An [AstVisitor] used to determine the best defining type of a node.
104100
*/
105-
class _BestTypeVisitor extends UnifyingAstVisitor<DartType> {
101+
class _BestTypeVisitor extends UnifyingAstVisitor<Element> {
106102
/**
107103
* The entity which the completed text will replace (or which will be
108104
* displaced once the completed text is inserted). This may be an AstNode or
@@ -114,32 +110,43 @@ class _BestTypeVisitor extends UnifyingAstVisitor<DartType> {
114110
_BestTypeVisitor(this.entity);
115111

116112
@override
117-
DartType visitConstructorName(ConstructorName node) =>
118-
node.period != null && node.name == entity ? node.type?.type : null;
113+
Element visitConstructorName(ConstructorName node) {
114+
return node.period != null && node.name == entity
115+
? node.type?.type?.element
116+
: null;
117+
}
119118

120119
@override
121-
DartType visitNamedExpression(NamedExpression node) {
120+
Element visitNamedExpression(NamedExpression node) {
122121
AstNode parent = node.parent;
123122
if (parent is ArgumentListImpl) {
124123
List<ParameterElement> params = parent.correspondingStaticParameters;
125124
if (params != null) {
126125
int index = parent.arguments.indexOf(node);
127-
return params[index]?.type;
126+
return params[index]?.type?.element;
128127
}
129128
}
130129
return super.visitNamedExpression(node);
131130
}
132131

133132
@override
134-
DartType visitNode(AstNode node) {
133+
Element visitNode(AstNode node) {
135134
return null;
136135
}
137136

138137
@override
139-
DartType visitPrefixedIdentifier(PrefixedIdentifier node) =>
140-
node.identifier == entity ? node.prefix?.staticType : null;
138+
Element visitPrefixedIdentifier(PrefixedIdentifier node) {
139+
if (node.identifier == entity) {
140+
var type = node.prefix.staticType;
141+
if (type is InterfaceType) {
142+
return type.element;
143+
}
144+
return node.prefix.staticElement;
145+
}
146+
return null;
147+
}
141148

142149
@override
143-
DartType visitPropertyAccess(PropertyAccess node) =>
144-
node.propertyName == entity ? node.realTarget?.staticType : null;
150+
Element visitPropertyAccess(PropertyAccess node) =>
151+
node.propertyName == entity ? node.realTarget?.staticType?.element : null;
145152
}

0 commit comments

Comments
 (0)