Skip to content

Commit

Permalink
Elements. Migrate CompletionTarget.
Browse files Browse the repository at this point in the history
Change-Id: I3a7f4d13d0174ff282a1ead65616a8db685ef0d9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403903
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Jan 9, 2025
1 parent 26d9167 commit 75882f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
4 changes: 4 additions & 0 deletions pkg/analyzer/lib/src/dart/ast/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15928,6 +15928,10 @@ final class SimpleIdentifierImpl extends IdentifierImpl
@override
Token get beginToken => token;

set element(Element2? element) {
staticElement = element?.asElement;
}

@override
Token get endToken => token;

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,6 +1,4 @@
lib/src/utilities/completion/completion_target.dart
lib/src/utilities/completion/optype.dart
lib/src/utilities/navigation/navigation_dart.dart
lib/utilities/analyzer_converter.dart
test/src/utilities/completion/completion_target_test.dart
test/utilities/analyzer_converter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/source/source_range.dart';
Expand Down Expand Up @@ -106,18 +105,18 @@ class CompletionTarget {
final int? argIndex;

/// If the target is an argument in an [ArgumentList], then this is the
/// invoked [ExecutableElement], otherwise this is `null`.
ExecutableElement? _executableElement;
/// invoked [ExecutableElement2], otherwise this is `null`.
ExecutableElement2? _executableElement;

/// If the target is in an argument list of a [FunctionExpressionInvocation],
/// then this is the static type of the function being invoked, otherwise this
/// is `null`.
FunctionType? _functionType;

/// If the target is an argument in an [ArgumentList], then this is the
/// corresponding [ParameterElement] in the invoked [ExecutableElement],
/// otherwise this is `null`.
ParameterElement? _parameterElement;
/// corresponding [FormalParameterElement] in the invoked
/// [ExecutableElement2], otherwise this is `null`.
FormalParameterElement? _parameterElement;

/// The enclosing [InterfaceElement2], or `null` if not in a class.
late final InterfaceElement2? enclosingInterfaceElement = () {
Expand All @@ -132,10 +131,11 @@ class CompletionTarget {
}
}();

/// The enclosing [ExtensionElement], or `null` if not in an extension.
late final ExtensionElement? enclosingExtensionElement = containingNode
/// The enclosing [ExtensionElement2], or `null` if not in an extension.
late final ExtensionElement2? enclosingExtensionElement = containingNode
.thisOrAncestorOfType<ExtensionDeclaration>()
?.declaredElement;
?.declaredFragment
?.element;

/// Compute the appropriate [CompletionTarget] for the given [offset] within
/// the [entryPoint].
Expand Down Expand Up @@ -282,7 +282,7 @@ class CompletionTarget {
var importPrefix = node.importPrefix;
if (importPrefix != null && identical(node.name2, entity)) {
return SimpleIdentifierImpl(importPrefix.name)
..staticElement = importPrefix.element;
..element = importPrefix.element2;
}
}
if (node is PropertyAccess) {
Expand All @@ -301,8 +301,8 @@ class CompletionTarget {
}

/// If the target is an argument in an argument list, and the invocation is
/// resolved, return the invoked [ExecutableElement].
ExecutableElement? get executableElement {
/// resolved, return the invoked [ExecutableElement2].
ExecutableElement2? get executableElement {
if (_executableElement == null) {
AstNode? argumentList = containingNode;
if (argumentList is NamedExpression) {
Expand All @@ -314,26 +314,26 @@ class CompletionTarget {

var invocation = argumentList.parent;

Element? executable;
Element2? executable;
if (invocation is Annotation) {
executable = invocation.element;
executable = invocation.element2;
} else if (invocation is EnumConstantArguments) {
var enumConstant = invocation.parent;
if (enumConstant is! EnumConstantDeclaration) {
return null;
}
executable = enumConstant.constructorElement;
executable = enumConstant.constructorElement2;
} else if (invocation is InstanceCreationExpression) {
executable = invocation.constructorName.staticElement;
executable = invocation.constructorName.element;
} else if (invocation is MethodInvocation) {
executable = invocation.methodName.staticElement;
executable = invocation.methodName.element;
} else if (invocation is RedirectingConstructorInvocation) {
executable = invocation.staticElement;
executable = invocation.element;
} else if (invocation is SuperConstructorInvocation) {
executable = invocation.staticElement;
executable = invocation.element;
}

if (executable is ExecutableElement) {
if (executable is ExecutableElement2) {
_executableElement = executable;
}
}
Expand Down Expand Up @@ -453,13 +453,13 @@ class CompletionTarget {
}

/// If the target is an argument in an argument list, and the invocation is
/// resolved, return the corresponding [ParameterElement].
ParameterElement? get parameterElement {
/// resolved, return the corresponding [FormalParameterElement].
FormalParameterElement? get parameterElement {
if (_parameterElement == null) {
var executable = executableElement;
if (executable != null) {
_parameterElement = _getParameterElement(
executable.parameters, containingNode, argIndex);
executable.formalParameters, containingNode, argIndex);
}
}
return _parameterElement;
Expand Down Expand Up @@ -699,17 +699,17 @@ class CompletionTarget {
return null;
}

/// Return the [ParameterElement] that corresponds to the given [argumentNode]
/// at the given [argumentIndex].
static ParameterElement? _getParameterElement(
List<ParameterElement> parameters,
/// Return the [FormalParameterElement] that corresponds to the given
/// [argumentNode] at the given [argumentIndex].
static FormalParameterElement? _getParameterElement(
List<FormalParameterElement> parameters,
AstNode argumentNode,
int? argumentIndex,
) {
if (argumentNode is NamedExpression) {
var name = argumentNode.name.label.name;
for (var parameter in parameters) {
if (parameter.name == name) {
if (parameter.name3 == name) {
return parameter;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/test_utilities/find_element.dart';
import 'package:analyzer/src/test_utilities/find_element2.dart';
import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart';
Expand Down Expand Up @@ -962,30 +962,30 @@ class _Base extends AbstractContextTest {
target = CompletionTarget.forOffset(result.unit, offset!);
}

static String _executableNameStr(ExecutableElement executable) {
var executableEnclosing = executable.enclosingElement3;
if (executableEnclosing is CompilationUnitElement) {
return executable.name;
} else if (executable is ConstructorElement) {
if (executable.name == '') {
return '${executableEnclosing.name}.<init>';
static String _executableNameStr(ExecutableElement2 executable) {
var executableEnclosing = executable.enclosingElement2;
if (executableEnclosing is LibraryElement2) {
return executable.name3!;
} else if (executable is ConstructorElement2) {
if (executable.name3 == 'new') {
return '${executableEnclosing!.name3}.<init>';
} else {
return '${executableEnclosing.name}.${executable.name}';
return '${executableEnclosing!.name3}.${executable.name3}';
}
} else if (executable is MethodElement) {
return '${executableEnclosing.name}.${executable.name}';
} else if (executable is MethodElement2) {
return '${executableEnclosing!.name3}.${executable.name3}';
}
fail('Unexpected element: $executable');
}

static String _executableStr(ExecutableElement element) {
static String _executableStr(ExecutableElement2 element) {
var executableStr = _executableNameStr(element);
var typeStr = element.type.getDisplayString();
return '$executableStr: $typeStr';
}

static String _parameterStr(ParameterElement element) {
static String _parameterStr(FormalParameterElement element) {
var typeStr = element.type.getDisplayString();
return '${element.name}: $typeStr';
return '${element.name3}: $typeStr';
}
}

0 comments on commit 75882f9

Please sign in to comment.