Skip to content

Commit

Permalink
Version 3.5.0-316.0.dev
Browse files Browse the repository at this point in the history
Merge 644823b into dev
  • Loading branch information
Dart CI committed Jun 28, 2024
2 parents 89b164e + 644823b commit 160ace0
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:analysis_server/src/protocol_server.dart'
show CompletionSuggestionKind;
import 'package:analysis_server/src/services/completion/dart/feature_computer.dart';
import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
Expand Down Expand Up @@ -209,19 +210,21 @@ final class ExtensionTypeSuggestion extends ImportableSuggestion {
}

/// The information about a candidate suggestion based on a field.
final class FieldSuggestion extends CandidateSuggestion {
final class FieldSuggestion extends CandidateSuggestion with MemberSuggestion {
/// The element on which the suggestion is based.
@override
final FieldElement element;

/// The element defined by the declaration in which the suggestion is to be
/// applied, or `null` if the completion is in a static context.
@override
final InterfaceElement? referencingInterface;

/// Initialize a newly created candidate suggestion to suggest the [element].
FieldSuggestion(
{required this.element,
required this.referencingInterface,
required super.matcherScore});
required super.matcherScore,
required this.referencingInterface});

@override
String get completion => element.name;
Expand Down Expand Up @@ -300,6 +303,10 @@ sealed class ImportableSuggestion extends CandidateSuggestion {
return prefixName == null ? '' : '$prefixName.';
}

/// Whether this is suggesing an element that is not yet imported into the
/// library in which completion was requested.
bool get isNotImported => importData?.isNotImported ?? false;

/// The prefix to be used in order to access the element.
String? get prefix => importData?.prefix;
}
Expand Down Expand Up @@ -467,13 +474,44 @@ final class LocalVariableSuggestion extends CandidateSuggestion {
String get completion => element.name;
}

/// Behavior common to suggestions that are for members of a class, enum, mixin,
/// etc.
mixin MemberSuggestion {
/// The element on which the suggestion is based.
Element get element;

/// The element defined by the declaration in which the suggestion is to be
/// applied, or `null` if the completion is in a static context.
InterfaceElement? get referencingInterface;

/// Returns the value of the inheritance distance feature.
///
/// Uses the [featureComputer] to compute the value.
double inheritanceDistance(FeatureComputer featureComputer) {
var inheritanceDistance = 0.0;
var element = this.element;
if (!(element is FieldElement && element.isEnumConstant)) {
var declaringClass = element.enclosingElement;
var referencingInterface = this.referencingInterface;
if (referencingInterface != null && declaringClass is InterfaceElement) {
inheritanceDistance = featureComputer.inheritanceDistanceFeature(
referencingInterface, declaringClass);
}
}
return inheritanceDistance;
}
}

/// The information about a candidate suggestion based on a method.
final class MethodSuggestion extends ExecutableSuggestion {
final class MethodSuggestion extends ExecutableSuggestion
with MemberSuggestion {
/// The element on which the suggestion is based.
@override
final MethodElement element;

/// The element defined by the declaration in which the suggestion is to be
/// applied, or `null` if the completion is in a static context.
@override
final InterfaceElement? referencingInterface;

/// Initialize a newly created candidate suggestion to suggest the [element].
Expand Down Expand Up @@ -575,12 +613,15 @@ final class OverrideSuggestion extends CandidateSuggestion {
}

/// The information about a candidate suggestion based on a getter or setter.
final class PropertyAccessSuggestion extends ImportableSuggestion {
final class PropertyAccessSuggestion extends ImportableSuggestion
with MemberSuggestion {
/// The element on which the suggestion is based.
@override
final PropertyAccessorElement element;

/// The element defined by the declaration in which the suggestion is to be
/// applied, or `null` if the completion is in a static context.
@override
final InterfaceElement? referencingInterface;

/// Initialize a newly created candidate suggestion to suggest the [element].
Expand Down Expand Up @@ -793,25 +834,8 @@ extension SuggestionBuilderExtension on SuggestionBuilder {
libraryUriStr = uri.toString();
}
}
var inheritanceDistance = 0.0;
if (suggestion is FieldSuggestion && !suggestion.element.isEnumConstant) {
inheritanceDistance = _inheritanceDistance(
suggestion.referencingInterface, suggestion.element.enclosingElement);
} else if (suggestion is MethodSuggestion) {
inheritanceDistance = _inheritanceDistance(
suggestion.referencingInterface, suggestion.element.enclosingElement);
} else if (suggestion is PropertyAccessSuggestion) {
var referencingClass = suggestion.referencingInterface;
var declaringClass = suggestion.element.enclosingElement;
if (referencingClass != null && declaringClass is InterfaceElement) {
inheritanceDistance = request.featureComputer
.inheritanceDistanceFeature(referencingClass, declaringClass);
}
}

var relevance = relevanceComputer.computeRelevance(suggestion,
isNotImportedLibrary: isNotImportedLibrary,
inheritanceDistance: inheritanceDistance);
var relevance = relevanceComputer.computeRelevance(suggestion);
switch (suggestion) {
case ClassSuggestion():
suggestInterface(suggestion.element,
Expand Down Expand Up @@ -850,8 +874,13 @@ extension SuggestionBuilderExtension on SuggestionBuilder {
if (fieldElement.isEnumConstant) {
suggestEnumConstant(fieldElement, relevance: relevance);
} else {
suggestField(fieldElement,
inheritanceDistance: inheritanceDistance, relevance: relevance);
var inheritanceDistance =
suggestion.inheritanceDistance(request.featureComputer);
suggestField(
fieldElement,
inheritanceDistance: inheritanceDistance,
relevance: relevance,
);
}
case FormalParameterSuggestion():
suggestFormalParameter(
Expand Down Expand Up @@ -896,6 +925,8 @@ extension SuggestionBuilderExtension on SuggestionBuilder {
var kind = request.target.isFunctionalArgument()
? CompletionSuggestionKind.IDENTIFIER
: suggestion.kind;
var inheritanceDistance =
suggestion.inheritanceDistance(request.featureComputer);
suggestMethod(
suggestion.element,
kind: kind,
Expand All @@ -921,8 +952,13 @@ extension SuggestionBuilderExtension on SuggestionBuilder {
skipAt: suggestion.skipAt,
);
case PropertyAccessSuggestion():
suggestAccessor(suggestion.element,
inheritanceDistance: inheritanceDistance, relevance: relevance);
var inheritanceDistance =
suggestion.inheritanceDistance(request.featureComputer);
suggestAccessor(
suggestion.element,
inheritanceDistance: inheritanceDistance,
relevance: relevance,
);
case RecordFieldSuggestion():
suggestRecordField(
field: suggestion.field,
Expand Down Expand Up @@ -972,16 +1008,4 @@ extension SuggestionBuilderExtension on SuggestionBuilder {
await suggestFromCandidate(suggestion);
}
}

/// Returns the inheritance distance from the [referencingClass] to the
/// [declaringClass].
double _inheritanceDistance(
InterfaceElement? referencingClass, Element? declaringClass) {
var distance = 0.0;
if (referencingClass != null && declaringClass is InterfaceElement) {
distance = request.featureComputer
.inheritanceDistanceFeature(referencingClass, declaringClass);
}
return distance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,46 +86,48 @@ class RelevanceComputer {
}

/// Compute the relevance for the given [CandidateSuggestion].
int computeRelevance(CandidateSuggestion suggestion,
{double inheritanceDistance = 0.0, bool isNotImportedLibrary = false}) {
int computeRelevance(CandidateSuggestion suggestion) {
var neverType = request.libraryElement.typeProvider.neverType;
switch (suggestion) {
case ClassSuggestion():
return computeTopLevelRelevance(suggestion.element,
elementType:
instantiateInstanceElement(suggestion.element, neverType),
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case ClosureSuggestion():
return Relevance.closure;
case ConstructorSuggestion():
return _computeConstructorRelevance(
suggestion.element, neverType, isNotImportedLibrary);
suggestion.element, neverType, suggestion.isNotImported);
case EnumConstantSuggestion():
return _computeEnumConstRelevance(
suggestion, isNotImportedLibrary, inheritanceDistance);
suggestion, suggestion.isNotImported, 0.0);
case EnumSuggestion():
return computeTopLevelRelevance(suggestion.element,
elementType:
instantiateInstanceElement(suggestion.element, neverType),
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case ExtensionSuggestion():
return computeTopLevelRelevance(suggestion.element,
elementType: suggestion.element.extendedType,
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case ExtensionTypeSuggestion():
return computeTopLevelRelevance(suggestion.element,
elementType:
instantiateInstanceElement(suggestion.element, neverType),
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case FieldSuggestion():
var fieldElement = suggestion.element;
if (fieldElement.isEnumConstant) {
// TODO(brianwilkerson): We are assuming that the enum constant is
// imported because it appears to be the case that we never create a
// `FieldSuggestion` on an enum constant except when adding members
// of the enclosing declaration. We should enforce this assumption.
return computeTopLevelRelevance(fieldElement,
elementType: fieldElement.type,
isNotImportedLibrary: isNotImportedLibrary);
elementType: fieldElement.type, isNotImportedLibrary: false);
} else {
return computeFieldElementRelevance(
fieldElement, inheritanceDistance);
fieldElement, suggestion.inheritanceDistance(featureComputer));
}
case FormalParameterSuggestion():
return _computeFormalParameterRelevance(suggestion);
Expand All @@ -146,17 +148,20 @@ class RelevanceComputer {
case LocalFunctionSuggestion():
return computeTopLevelRelevance(suggestion.element,
elementType: suggestion.element.returnType,
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case LocalVariableSuggestion():
return _computeLocalVariableRelevance(suggestion);
case MethodSuggestion():
return _computeMethodRelevance(
suggestion.element, inheritanceDistance, isNotImportedLibrary);
suggestion.element,
suggestion.inheritanceDistance(featureComputer),
suggestion.isNotImported,
);
case MixinSuggestion():
return computeTopLevelRelevance(suggestion.element,
elementType:
instantiateInstanceElement(suggestion.element, neverType),
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case NamedArgumentSuggestion():
var parameter = suggestion.parameter;
if (parameter.isRequiredNamed || parameter.hasRequired) {
Expand All @@ -170,7 +175,10 @@ class RelevanceComputer {
return Relevance.override;
case PropertyAccessSuggestion():
return _computePropertyAccessorRelevance(
suggestion.element, inheritanceDistance, isNotImportedLibrary);
suggestion.element,
suggestion.inheritanceDistance(featureComputer),
suggestion.isNotImported,
);
case RecordFieldSuggestion():
var contextType = featureComputer.contextTypeFeature(
request.contextType, suggestion.field.type);
Expand All @@ -181,27 +189,30 @@ class RelevanceComputer {
return Relevance.requiredNamedArgument;
case StaticFieldSuggestion():
return _computeStaticFieldRelevance(
suggestion.element, inheritanceDistance, isNotImportedLibrary);
suggestion.element,
0.0,
suggestion.isNotImported,
);
case SuperParameterSuggestion():
return Relevance.superFormalParameter;
case TopLevelFunctionSuggestion():
var function = suggestion.element;
return computeTopLevelRelevance(function,
elementType: function.returnType,
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case TopLevelPropertyAccessSuggestion():
return _computeTopLevelPropertyAccessorRelevance(
suggestion.element, isNotImportedLibrary);
suggestion.element, suggestion.isNotImported);
case TopLevelVariableSuggestion():
var variable = suggestion.element;
return computeTopLevelRelevance(variable,
elementType: variable.type,
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case TypeAliasSuggestion():
var typeAlias = suggestion.element;
return computeTopLevelRelevance(typeAlias,
elementType: _instantiateTypeAlias(typeAlias),
isNotImportedLibrary: isNotImportedLibrary);
isNotImportedLibrary: suggestion.isNotImported);
case TypeParameterSuggestion():
return _computeTypeParameterRelevance(suggestion.element);
case UriSuggestion():
Expand Down
Loading

0 comments on commit 160ace0

Please sign in to comment.