Skip to content

Commit

Permalink
Elements. Migrate ElementSuggestionBuilder and related.
Browse files Browse the repository at this point in the history
Change-Id: I3f282c1ec2f2ba277d9ae011b4723fa835e44c37
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403961
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 7d5f0a6 commit 26d9167
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 87 deletions.
5 changes: 0 additions & 5 deletions pkg/analyzer_plugin/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
lib/src/utilities/completion/completion_target.dart
lib/src/utilities/completion/element_suggestion_builder.dart
lib/src/utilities/completion/optype.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/type_member_contributor.dart
test/src/utilities/completion/completion_target_test.dart
test/src/utilities/visitors/local_declaration_visitor_test.dart
test/utilities/analyzer_converter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// 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/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 All @@ -25,7 +24,7 @@ mixin ElementSuggestionBuilder {
<String, CompletionSuggestion>{};

/// Return the library in which the completion is requested.
LibraryElement? get containingLibrary;
LibraryElement2? get containingLibrary;

/// Return the kind of suggestions that should be built.
CompletionSuggestionKind? get kind;
Expand All @@ -34,10 +33,10 @@ mixin ElementSuggestionBuilder {
ResourceProvider? get resourceProvider;

/// Add a suggestion based upon the given element.
void addSuggestion(Element element,
void addSuggestion(Element2 element,
{String? prefix, int relevance = DART_RELEVANCE_DEFAULT}) {
if (element.isPrivate) {
if (element.library != containingLibrary) {
if (element.library2 != containingLibrary) {
return;
}
}
Expand All @@ -53,25 +52,19 @@ mixin ElementSuggestionBuilder {
return;
}
var builder = SuggestionBuilderImpl(resourceProvider);
var suggestion = builder.forElement(element.asElement2,
var suggestion = builder.forElement(element,
completion: completion, kind: kind, relevance: relevance);
if (suggestion != null) {
if (element.isSynthetic && element is PropertyAccessorElement) {
String? cacheKey;
if (element.isGetter) {
cacheKey = element.name;
}
if (element.isSetter) {
cacheKey = element.name;
cacheKey = cacheKey.substring(0, cacheKey.length - 1);
}
if (element.isSynthetic && element is PropertyAccessorElement2) {
var cacheKey = element.name3;
if (cacheKey != null) {
var existingSuggestion = _syntheticMap[cacheKey];

// Pair getter/setter by updating the existing suggestion
if (existingSuggestion != null) {
var getter = element.isGetter ? suggestion : existingSuggestion;
var elemKind = element.enclosingElement3 is ClassElement
var getter =
element is GetterElement ? suggestion : existingSuggestion;
var elemKind = element.enclosingElement2 is ClassElement2
? protocol.ElementKind.FIELD
: protocol.ElementKind.TOP_LEVEL_VARIABLE;
existingSuggestion.element = protocol.Element(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';

/// A visitor that visits an [AstNode] and its parent recursively along with any
/// declarations in those nodes. Consumers typically call [visit] which catches
Expand Down Expand Up @@ -38,14 +38,14 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {
void declaredLocalVar(
Token name,
TypeAnnotation? type,
LocalVariableElement declaredElement,
LocalVariableElement2 declaredElement,
) {}

void declaredMethod(MethodDeclaration declaration) {}

void declaredMixin(MixinDeclaration declaration) {}

void declaredParam(Token name, Element? element, TypeAnnotation? type) {}
void declaredParam(Token name, Element2? element, TypeAnnotation? type) {}

void declaredTopLevelVar(
VariableDeclarationList varList, VariableDeclaration varDecl) {}
Expand Down Expand Up @@ -82,7 +82,7 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {
if (exceptionParameter != null) {
declaredParam(
exceptionParameter.name,
exceptionParameter.declaredElement,
exceptionParameter.declaredElement2,
node.exceptionType,
);
}
Expand All @@ -91,7 +91,7 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {
if (stackTraceParameter != null) {
declaredParam(
stackTraceParameter.name,
stackTraceParameter.declaredElement,
stackTraceParameter.declaredElement2,
null,
);
}
Expand Down Expand Up @@ -134,12 +134,12 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {
if (forLoopParts is ForEachPartsWithDeclaration) {
var loopVariable = forLoopParts.loopVariable;
declaredLocalVar(
loopVariable.name, loopVariable.type, loopVariable.declaredElement!);
loopVariable.name, loopVariable.type, loopVariable.declaredElement2!);
} else if (forLoopParts is ForPartsWithDeclarations) {
var varList = forLoopParts.variables;
for (var varDecl in varList.variables) {
declaredLocalVar(varDecl.name, varList.type,
varDecl.declaredElement as LocalVariableElement);
varDecl.declaredElement2 as LocalVariableElement2);
}
}
visitNode(node);
Expand All @@ -151,12 +151,12 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {
if (forLoopParts is ForEachPartsWithDeclaration) {
var loopVariable = forLoopParts.loopVariable;
declaredLocalVar(
loopVariable.name, loopVariable.type, loopVariable.declaredElement!);
loopVariable.name, loopVariable.type, loopVariable.declaredElement2!);
} else if (forLoopParts is ForPartsWithDeclarations) {
var varList = forLoopParts.variables;
for (var varDecl in varList.variables) {
declaredLocalVar(varDecl.name, varList.type,
varDecl.declaredElement as LocalVariableElement);
varDecl.declaredElement2 as LocalVariableElement2);
}
}
visitNode(node);
Expand Down Expand Up @@ -412,7 +412,7 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {

/// Visit the given [pattern] without visiting any of its parents.
void _visitDeclaredVariablePattern(DeclaredVariablePattern pattern) {
var declaredElement = pattern.declaredElement;
var declaredElement = pattern.declaredElement2;
if (declaredElement != null) {
declaredLocalVar(pattern.name, pattern.type, declaredElement);
}
Expand All @@ -435,7 +435,7 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {
} else if (normalParam is SimpleFormalParameter) {
type = normalParam.type;
}
declaredParam(param.name!, param.declaredElement, type);
declaredParam(param.name!, param.declaredFragment?.element, type);
}
}
}
Expand All @@ -448,7 +448,7 @@ abstract class LocalDeclarationVisitor extends UnifyingAstVisitor {
for (var varDecl in varList.variables) {
if (varDecl.end < offset) {
declaredLocalVar(varDecl.name, varList.type,
varDecl.declaredElement as LocalVariableElement);
varDecl.declaredElement2 as LocalVariableElement2);
}
}
} else if (stmt is FunctionDeclarationStatement) {
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/dart/element/type.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
Expand All @@ -20,7 +20,7 @@ class InheritedReferenceContributor
with ElementSuggestionBuilder
implements CompletionContributor {
@override
LibraryElement? containingLibrary;
LibraryElement2? containingLibrary;

@override
CompletionSuggestionKind? kind;
Expand All @@ -41,19 +41,19 @@ class InheritedReferenceContributor
return;
}
var classDecl = _enclosingClass(target);
if (classDecl == null || classDecl.declaredElement == null) {
if (classDecl == null || classDecl.declaredFragment == null) {
return;
}
containingLibrary = request.result.libraryElement;
containingLibrary = request.result.libraryElement2;
_computeSuggestionsForClass2(
collector, target, classDecl.declaredElement!, optype);
collector, target, classDecl.declaredFragment!.element, optype);
}

/// Clients should not overload this function.
Future<void> computeSuggestionsForClass(
DartCompletionRequest request,
CompletionCollector collector,
ClassElement? classElement, {
ClassElement2? classElement, {
AstNode? entryPoint,
bool skipChildClass = true,
CompletionTarget? target,
Expand All @@ -67,32 +67,31 @@ class InheritedReferenceContributor
}
if (classElement == null) {
var classDecl = _enclosingClass(target);
if (classDecl == null || classDecl.declaredElement == null) {
if (classDecl == null || classDecl.declaredFragment == null) {
return;
}
classElement = classDecl.declaredElement;
classElement = classDecl.declaredFragment!.element;
}
containingLibrary = request.result.libraryElement;
_computeSuggestionsForClass2(collector, target, classElement!, optype,
containingLibrary = request.result.libraryElement2;
_computeSuggestionsForClass2(collector, target, classElement, optype,
skipChildClass: skipChildClass);
}

void _addSuggestionsForType(InterfaceType type, OpType optype,
{bool isFunctionalArgument = false}) {
if (!isFunctionalArgument) {
for (var elem in type.accessors) {
if (elem.isGetter) {
if (optype.includeReturnValueSuggestions) {
addSuggestion(elem);
}
} else {
if (optype.includeVoidReturnSuggestions) {
addSuggestion(elem);
}
for (var elem in type.getters) {
if (optype.includeReturnValueSuggestions) {
addSuggestion(elem);
}
}
for (var elem in type.setters) {
if (optype.includeVoidReturnSuggestions) {
addSuggestion(elem);
}
}
}
for (var elem in type.methods) {
for (var elem in type.methods2) {
if (elem.returnType is! VoidType) {
if (optype.includeReturnValueSuggestions) {
addSuggestion(elem);
Expand All @@ -106,7 +105,7 @@ class InheritedReferenceContributor
}

void _computeSuggestionsForClass2(CompletionCollector collector,
CompletionTarget target, ClassElement classElement, OpType optype,
CompletionTarget target, ClassElement2 classElement, OpType optype,
{bool skipChildClass = true}) {
var isFunctionalArgument = target.isFunctionalArgument();
kind = isFunctionalArgument
Expand Down
Loading

0 comments on commit 26d9167

Please sign in to comment.