Skip to content

Commit

Permalink
Elements. Migrate lib/utilities/completion/suggestion_builder.dart an…
Browse files Browse the repository at this point in the history
…d required.

Change-Id: I6331e3f620bdaaec2a618aa0f6651099bdc606c0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403921
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Jan 9, 2025
1 parent 81b1313 commit 4356eac
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 46 deletions.
24 changes: 18 additions & 6 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4384,6 +4384,7 @@ class FormalParameterElementImpl extends PromotableElementImpl2
with
FragmentedAnnotatableElementMixin<FormalParameterFragment>,
FragmentedElementMixin<FormalParameterFragment>,
FormalParameterElementMixin,
_NonTopLevelVariableOrParameter
implements FormalParameterElementOrMember {
final ParameterElementImpl wrappedElement;
Expand Down Expand Up @@ -4500,12 +4501,6 @@ class FormalParameterElementImpl extends PromotableElementImpl2
return visitor.visitFormalParameterElement(this);
}

@override
void appendToWithoutDelimiters2(StringBuffer buffer) {
// TODO(augmentations): Implement the merge of formal parameters.
wrappedElement.appendToWithoutDelimiters(buffer);
}

@override
// TODO(augmentations): Implement the merge of formal parameters.
DartObject? computeConstantValue() => wrappedElement.computeConstantValue();
Expand All @@ -4521,6 +4516,23 @@ class FormalParameterElementImpl extends PromotableElementImpl2
// .toList();
}

/// A mixin that provides a common implementation for methods defined in
/// [FormalParameterElement].
mixin FormalParameterElementMixin implements FormalParameterElement {
@override
void appendToWithoutDelimiters2(StringBuffer buffer) {
buffer.write(
type.getDisplayString(),
);
buffer.write(' ');
buffer.write(displayName);
if (defaultValueCode != null) {
buffer.write(' = ');
buffer.write(defaultValueCode);
}
}
}

abstract class FormalParameterElementOrMember
implements
FormalParameterElement,
Expand Down
7 changes: 1 addition & 6 deletions pkg/analyzer/lib/src/dart/element/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ class MethodMember extends ExecutableMember
/// A parameter element defined in a parameterized type where the values of the
/// type parameters are known.
class ParameterMember extends VariableMember
with ParameterElementMixin
with ParameterElementMixin, FormalParameterElementMixin
implements ParameterElement, FormalParameterElementOrMember {
@override
final List<TypeParameterElement> typeParameters;
Expand Down Expand Up @@ -1211,11 +1211,6 @@ class ParameterMember extends VariableMember
builder.writeFormalParameter(this);
}

@override
void appendToWithoutDelimiters2(StringBuffer buffer) {
_element2.appendToWithoutDelimiters2(buffer);
}

@override
String displayString2(
{bool multiline = false, bool preferTypeAlias = false}) {
Expand Down
2 changes: 0 additions & 2 deletions pkg/analyzer_plugin/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
lib/src/utilities/completion/completion_target.dart
lib/src/utilities/completion/element_suggestion_builder.dart
lib/src/utilities/completion/optype.dart
lib/src/utilities/completion/suggestion_builder.dart
lib/src/utilities/navigation/navigation_dart.dart
lib/src/utilities/visitors/local_declaration_visitor.dart
lib/utilities/analyzer_converter.dart
lib/utilities/completion/inherited_reference_contributor.dart
lib/utilities/completion/suggestion_builder.dart
lib/utilities/completion/type_member_contributor.dart
test/src/utilities/change_builder/change_builder_dart_test.dart
test/src/utilities/completion/completion_target_test.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart'
hide Element, ElementKind;
import 'package:analyzer_plugin/protocol/protocol_common.dart' as protocol;
Expand Down Expand Up @@ -52,7 +53,7 @@ mixin ElementSuggestionBuilder {
return;
}
var builder = SuggestionBuilderImpl(resourceProvider);
var suggestion = builder.forElement(element,
var suggestion = builder.forElement(element.asElement2,
completion: completion, kind: kind, relevance: relevance);
if (suggestion != null) {
if (element.isSynthetic && element is PropertyAccessorElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart'
hide Element, ElementKind;
Expand All @@ -26,9 +26,9 @@ class SuggestionBuilderImpl implements SuggestionBuilder {
/// [requiredParams] and [namedParams].
void addDefaultArgDetails(
CompletionSuggestion suggestion,
Element element,
Iterable<ParameterElement> requiredParams,
Iterable<ParameterElement> namedParams) {
Element2 element,
Iterable<FormalParameterElement> requiredParams,
Iterable<FormalParameterElement> namedParams) {
// Copied from analysis_server/lib/src/services/completion/dart/suggestion_builder.dart
var buffer = StringBuffer();
var ranges = <int>[];
Expand All @@ -40,17 +40,17 @@ class SuggestionBuilderImpl implements SuggestionBuilder {
buffer.write(', ');
}
offset = buffer.length;
var name = param.name;
var name = param.name3 ?? '';
buffer.write(name);
ranges.addAll([offset, name.length]);
}

for (var param in namedParams) {
if (param.hasRequired || param.isRequiredNamed) {
if (param.metadata2.hasRequired || param.isRequiredNamed) {
if (buffer.isNotEmpty) {
buffer.write(', ');
}
var name = param.name;
var name = param.name3 ?? '';
buffer.write('$name: ');
offset = buffer.length;
buffer.write(name);
Expand All @@ -65,20 +65,26 @@ class SuggestionBuilderImpl implements SuggestionBuilder {
}

@override
CompletionSuggestion? forElement(Element? element,
CompletionSuggestion? forElement(Element2? element,
{String? completion,
CompletionSuggestionKind? kind,
int relevance = DART_RELEVANCE_DEFAULT}) {
// Copied from analysis_server/lib/src/services/completion/dart/suggestion_builder.dart
if (element == null) {
return null;
}
if (element is ExecutableElement && element.isOperator) {
if (element is MethodElement2 && element.isOperator) {
// Do not include operators in suggestions
return null;
}
completion ??= element.displayName;
var isDeprecated = element.hasDeprecated;

Annotatable? annotatable;
if (element case Annotatable annotatable2) {
annotatable = annotatable2;
}

var isDeprecated = annotatable?.metadata2.hasDeprecated ?? false;
var suggestion = CompletionSuggestion(
kind ?? CompletionSuggestionKind.INVOCATION,
isDeprecated ? DART_RELEVANCE_LOW : relevance,
Expand All @@ -89,31 +95,30 @@ class SuggestionBuilderImpl implements SuggestionBuilder {
false);

// Attach docs.
var doc = removeDartDocDelimiters(element.documentationComment);
var doc = removeDartDocDelimiters(annotatable?.documentationComment);
suggestion.docComplete = doc;
suggestion.docSummary = getDartDocSummary(doc);

suggestion.element = converter.convertElement(element);
var enclosingElement = element.enclosingElement3;
if (enclosingElement is ClassElement) {
suggestion.element = converter.convertElement2(element);
var enclosingElement = element.enclosingElement2;
if (enclosingElement is ClassElement2) {
suggestion.declaringType = enclosingElement.displayName;
}
suggestion.returnType = getReturnTypeString(element);
if (element is ExecutableElement && element is! PropertyAccessorElement) {
suggestion.parameterNames = element.parameters
.map((ParameterElement parameter) => parameter.name)
if (element is ExecutableElement2 && element is! PropertyAccessorElement2) {
suggestion.parameterNames = element.formalParameters
.map((parameter) => parameter.name3 ?? '')
.toList();
suggestion.parameterTypes =
element.parameters.map((ParameterElement parameter) {
suggestion.parameterTypes = element.formalParameters.map((parameter) {
return parameter.type.getDisplayString();
}).toList();

var requiredParameters = element.parameters
.where((ParameterElement param) => param.isRequiredPositional);
var requiredParameters =
element.formalParameters.where((param) => param.isRequiredPositional);
suggestion.requiredParameterCount = requiredParameters.length;

var namedParameters =
element.parameters.where((ParameterElement param) => param.isNamed);
element.formalParameters.where((param) => param.isNamed);
suggestion.hasNamedParameters = namedParameters.isNotEmpty;

addDefaultArgDetails(
Expand All @@ -122,20 +127,20 @@ class SuggestionBuilderImpl implements SuggestionBuilder {
return suggestion;
}

String? getReturnTypeString(Element element) {
String? getReturnTypeString(Element2 element) {
// Copied from analysis_server/lib/src/protocol_server.dart
if (element is ExecutableElement) {
if (element is ExecutableElement2) {
if (element.kind == ElementKind.SETTER) {
return null;
} else {
return element.returnType.getDisplayString();
}
} else if (element is VariableElement) {
} else if (element is VariableElement2) {
var type = element.type;
return type.getDisplayString();
} else if (element is TypeAliasElement) {
var aliasedElement = element.aliasedElement;
if (aliasedElement is GenericFunctionTypeElement) {
} else if (element is TypeAliasElement2) {
var aliasedElement = element.aliasedElement2;
if (aliasedElement is GenericFunctionTypeElement2) {
var returnType = aliasedElement.returnType;
return returnType.getDisplayString();
} else {
Expand Down
Loading

0 comments on commit 4356eac

Please sign in to comment.