Skip to content

Commit 44a8f14

Browse files
keertipCommit Queue
authored andcommitted
[Elements.migrate] Migrate FeatureComputer
Change-Id: Iff5c9cffd676464454460ca2a51cdd7cd0e1d507 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397040 Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent d621ed9 commit 44a8f14

File tree

7 files changed

+72
-153
lines changed

7 files changed

+72
-153
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ lib/src/search/element_references.dart
2929
lib/src/search/type_hierarchy.dart
3030
lib/src/services/completion/dart/completion_manager.dart
3131
lib/src/services/completion/dart/declaration_helper.dart
32-
lib/src/services/completion/dart/feature_computer.dart
3332
lib/src/services/completion/dart/identifier_helper.dart
3433
lib/src/services/completion/dart/in_scope_completion_pass.dart
3534
lib/src/services/completion/dart/not_imported_completion_pass.dart

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ mixin MemberSuggestion implements ElementBasedSuggestion {
679679
var declaringClass = element.enclosingElement2;
680680
var referencingInterface = this.referencingInterface;
681681
if (referencingInterface != null && declaringClass is InterfaceElement2) {
682-
inheritanceDistance = featureComputer.inheritanceDistanceFeature2(
682+
inheritanceDistance = featureComputer.inheritanceDistanceFeature(
683683
referencingInterface,
684684
declaringClass,
685685
);

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

Lines changed: 32 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ import 'package:analysis_server/src/utilities/extensions/element.dart';
1616
import 'package:analysis_server/src/utilities/extensions/numeric.dart';
1717
import 'package:analyzer/dart/ast/token.dart';
1818
import 'package:analyzer/dart/ast/visitor.dart';
19-
import 'package:analyzer/dart/element/element.dart';
2019
import 'package:analyzer/dart/element/element2.dart';
2120
import 'package:analyzer/dart/element/type.dart';
2221
import 'package:analyzer/dart/element/type_provider.dart';
2322
import 'package:analyzer/dart/element/type_system.dart';
2423
import 'package:analyzer/src/dart/ast/ast.dart';
2524
import 'package:analyzer/src/dart/element/extensions.dart';
2625
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
27-
import 'package:analyzer/src/utilities/extensions/element.dart';
2826
import 'package:analyzer/src/utilities/extensions/object.dart';
2927
import 'package:analyzer_plugin/utilities/range_factory.dart';
3028

@@ -190,19 +188,19 @@ class FeatureComputer {
190188
/// setters are always mapped into a different kind: FIELD for getters and
191189
/// setters declared in a class or extension, and TOP_LEVEL_VARIABLE for
192190
/// top-level getters and setters.
193-
protocol.ElementKind computeElementKind(Element element) {
194-
if (element is LibraryElement) {
191+
protocol.ElementKind computeElementKind(Element2 element) {
192+
if (element is LibraryElement2) {
195193
return protocol.ElementKind.PREFIX;
196-
} else if (element is EnumElement) {
194+
} else if (element is EnumElement2) {
197195
return protocol.ElementKind.ENUM;
198-
} else if (element is MixinElement) {
196+
} else if (element is MixinElement2) {
199197
return protocol.ElementKind.MIXIN;
200-
} else if (element is ClassElement) {
198+
} else if (element is ClassElement2) {
201199
return protocol.ElementKind.CLASS;
202-
} else if (element is FieldElement && element.isEnumConstant) {
200+
} else if (element is FieldElement2 && element.isEnumConstant) {
203201
return protocol.ElementKind.ENUM_CONSTANT;
204-
} else if (element is PropertyAccessorElement) {
205-
var variable = element.variable2;
202+
} else if (element is PropertyAccessorElement2) {
203+
var variable = element.variable3;
206204
if (variable == null) {
207205
return protocol.ElementKind.UNKNOWN;
208206
}
@@ -333,31 +331,6 @@ class FeatureComputer {
333331
/// completing at the given [completionLocation]. If a [distance] is given it
334332
/// will be used to provide finer-grained relevance scores.
335333
double elementKindFeature(
336-
Element element,
337-
String? completionLocation, {
338-
double? distance,
339-
}) {
340-
if (completionLocation == null) {
341-
return 0.0;
342-
}
343-
var locationTable = elementKindRelevance[completionLocation];
344-
if (locationTable == null) {
345-
return 0.0;
346-
}
347-
var range = locationTable[computeElementKind(element)];
348-
if (range == null) {
349-
return 0.0;
350-
}
351-
if (distance == null) {
352-
return range.middle;
353-
}
354-
return range.conditionalProbability(distance);
355-
}
356-
357-
/// Return the value of the _element kind_ feature for the [element] when
358-
/// completing at the given [completionLocation]. If a [distance] is given it
359-
/// will be used to provide finer-grained relevance scores.
360-
double elementKindFeature2(
361334
Element2 element,
362335
String? completionLocation, {
363336
double? distance,
@@ -379,13 +352,8 @@ class FeatureComputer {
379352
return range.conditionalProbability(distance);
380353
}
381354

382-
/// Return the value of the _has deprecated_ feature for the given [element].
383-
double hasDeprecatedFeature(Element element) {
384-
return element.hasOrInheritsDeprecated ? -1.0 : 0.0;
385-
}
386-
387355
// Return the value of the _has deprecated_ feature for the given [element].
388-
double hasDeprecatedFeature2(Element2 element) {
356+
double hasDeprecatedFeature(Element2 element) {
389357
return element.hasOrInheritsDeprecated ? -1.0 : 0.0;
390358
}
391359

@@ -396,21 +364,6 @@ class FeatureComputer {
396364
/// supertype if the two types are not the same. Return `-1` if the [subclass]
397365
/// is not a subclass of the [superclass].
398366
int inheritanceDistance(
399-
InterfaceElement subclass,
400-
InterfaceElement superclass,
401-
) {
402-
// This method is only visible for the metrics computation and might be made
403-
// private at some future date.
404-
return _inheritanceDistance(subclass.asElement2, superclass.asElement2, {});
405-
}
406-
407-
/// Return the inheritance distance between the [subclass] and the
408-
/// [superclass]. We define the inheritance distance between two types to be
409-
/// zero if the two types are the same and the minimum number of edges that
410-
/// must be traversed in the type graph to get from the subtype to the
411-
/// supertype if the two types are not the same. Return `-1` if the [subclass]
412-
/// is not a subclass of the [superclass].
413-
int inheritanceDistance2(
414367
InterfaceElement2 subclass,
415368
InterfaceElement2 superclass,
416369
) {
@@ -423,21 +376,6 @@ class FeatureComputer {
423376
/// defined in the [superclass] that is being accessed through an expression
424377
/// whose static type is the [subclass].
425378
double inheritanceDistanceFeature(
426-
InterfaceElement subclass,
427-
InterfaceElement superclass,
428-
) {
429-
var distance = _inheritanceDistance(
430-
subclass.asElement2,
431-
superclass.asElement2,
432-
{},
433-
);
434-
return distanceToPercent(distance);
435-
}
436-
437-
/// Return the value of the _inheritance distance_ feature for a member
438-
/// defined in the [superclass] that is being accessed through an expression
439-
/// whose static type is the [subclass].
440-
double inheritanceDistanceFeature2(
441379
InterfaceElement2 subclass,
442380
InterfaceElement2 superclass,
443381
) {
@@ -446,24 +384,7 @@ class FeatureComputer {
446384
}
447385

448386
/// Return the value of the _is constant_ feature for the given [element].
449-
double isConstantFeature(Element element) {
450-
if (element is ConstructorElement && element.isConst) {
451-
return 1.0;
452-
} else if (element is FieldElement && element.isStatic && element.isConst) {
453-
return 1.0;
454-
} else if (element is TopLevelVariableElement && element.isConst) {
455-
return 1.0;
456-
} else if (element is PropertyAccessorElement && element.isSynthetic) {
457-
var variable = element.variable2;
458-
if (variable != null && variable.isStatic && variable.isConst) {
459-
return 1.0;
460-
}
461-
}
462-
return 0.0;
463-
}
464-
465-
/// Return the value of the _is constant_ feature for the given [element].
466-
double isConstantFeature2(Element2 element) {
387+
double isConstantFeature(Element2 element) {
467388
if (element is ConstructorElement2 && element.isConst) {
468389
return 1.0;
469390
} else if (element is FieldElement2 &&
@@ -491,7 +412,7 @@ class FeatureComputer {
491412
// override of `noSuchMethod`.
492413
return 0.0;
493414
}
494-
return proposedMemberName == FunctionElement.NO_SUCH_METHOD_METHOD_NAME
415+
return proposedMemberName == MethodElement2.NO_SUCH_METHOD_METHOD_NAME
495416
? -1.0
496417
: 0.0;
497418
}
@@ -612,7 +533,7 @@ class _ContextTypeVisitor extends SimpleAstVisitor<DartType> {
612533
if (range
613534
.endStart(node.leftParenthesis, node.rightParenthesis)
614535
.contains(offset)) {
615-
var parameters = node.functionType?.parameters;
536+
var parameters = node.functionType?.formalParameters;
616537
if (parameters == null) {
617538
return null;
618539
}
@@ -637,14 +558,14 @@ class _ContextTypeVisitor extends SimpleAstVisitor<DartType> {
637558
}
638559
if (argument.contains(offset)) {
639560
if (offset >= argument.name.end) {
640-
return argument.staticParameterElement?.type;
561+
return argument.element2?.type;
641562
}
642563
return null;
643564
}
644565
} else {
645566
if (previousArgument == null || previousArgument.end < offset) {
646567
if (offset <= argument.end) {
647-
return argument.staticParameterElement?.type;
568+
return argument.correspondingParameter?.type;
648569
}
649570
}
650571
previousArgument = argument;
@@ -717,7 +638,7 @@ class _ContextTypeVisitor extends SimpleAstVisitor<DartType> {
717638
@override
718639
DartType? visitBinaryExpression(BinaryExpression node) {
719640
if (node.operator.end <= offset) {
720-
return node.rightOperand.staticParameterElement?.type;
641+
return node.rightOperand.correspondingParameter?.type;
721642
}
722643
return _visitParent(node);
723644
}
@@ -742,8 +663,8 @@ class _ContextTypeVisitor extends SimpleAstVisitor<DartType> {
742663
@override
743664
DartType? visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
744665
if (node.equals.end <= offset) {
745-
var element = node.fieldName.staticElement;
746-
if (element is FieldElement) {
666+
var element = node.fieldName.element;
667+
if (element is FieldElement2) {
747668
return element.type;
748669
}
749670
}
@@ -764,7 +685,7 @@ class _ContextTypeVisitor extends SimpleAstVisitor<DartType> {
764685
DartType? visitDefaultFormalParameter(DefaultFormalParameter node) {
765686
var separator = node.separator;
766687
if (separator != null && separator.end <= offset) {
767-
return node.parameter.declaredElement?.type;
688+
return node.parameter.declaredFragment?.element.type;
768689
}
769690
return null;
770691
}
@@ -1073,7 +994,7 @@ parent3: ${node.parent?.parent?.parent}
1073994

1074995
@override
1075996
DartType? visitPostfixExpression(PostfixExpression node) {
1076-
return node.operand.staticParameterElement?.type;
997+
return node.operand.correspondingParameter?.type;
1077998
}
1078999

10791000
@override
@@ -1083,7 +1004,7 @@ parent3: ${node.parent?.parent?.parent}
10831004

10841005
@override
10851006
DartType? visitPrefixExpression(PrefixExpression node) {
1086-
return node.operand.staticParameterElement?.type;
1007+
return node.operand.correspondingParameter?.type;
10871008
}
10881009

10891010
@override
@@ -1305,17 +1226,17 @@ parent3: ${node.parent?.parent?.parent}
13051226
// TODO(brianwilkerson): Replace with `patternTypeSchema` (on AST) where
13061227
// possible.
13071228
pattern = pattern.unParenthesized;
1308-
Element? element;
1229+
Element2? element;
13091230
if (pattern is AssignedVariablePattern) {
1310-
element = pattern.element;
1231+
element = pattern.element2;
13111232
} else if (pattern is DeclaredVariablePattern) {
1312-
element = pattern.declaredElement;
1233+
element = pattern.declaredElement2;
13131234
// } else if (pattern is RecordPattern) {
13141235
// pattern.fields.map((e) => _requiredTypeOfPattern(e.pattern)).toList();
13151236
} else if (pattern is ListPattern) {
13161237
return pattern.requiredType;
13171238
}
1318-
if (element is VariableElement) {
1239+
if (element is VariableElement2) {
13191240
return element.type;
13201241
}
13211242
return null;
@@ -1337,19 +1258,14 @@ parent3: ${node.parent?.parent?.parent}
13371258
if (type is! InterfaceType) {
13381259
return null;
13391260
}
1340-
var declaredElement2 = (field.root as CompilationUnit).declaredElement;
1341-
var uri = declaredElement2?.source.uri;
1342-
if (uri == null) {
1343-
return null;
1344-
}
1261+
var declaredElement2 = field.element2?.library2;
1262+
var uri = declaredElement2?.uri;
13451263
var manager = InheritanceManager3();
1346-
var member = manager.getMember(type, Name(uri, name));
1347-
if (member is PropertyAccessorElement) {
1348-
if (member.isGetter) {
1349-
return member.type.returnType;
1350-
}
1351-
} else if (member is MethodElement) {
1352-
return member.type;
1264+
var member = manager.getMember3(type, Name(uri, name));
1265+
if (member is GetterElement) {
1266+
return member.returnType;
1267+
} else if (member is MethodElement2) {
1268+
return member.returnType;
13531269
}
13541270
return null;
13551271
}
@@ -1421,7 +1337,7 @@ extension on ArgumentList {
14211337
FunctionType? get functionType {
14221338
var parent = this.parent;
14231339
if (parent is InstanceCreationExpression) {
1424-
return parent.constructorName.staticElement?.type;
1340+
return parent.constructorName.element?.type;
14251341
} else if (parent is MethodInvocation) {
14261342
var type = parent.staticInvokeType;
14271343
if (type is FunctionType) {

0 commit comments

Comments
 (0)