Skip to content

Commit

Permalink
Elements. Migrate lib/src/domains/analysis/implemented_dart.dart
Browse files Browse the repository at this point in the history
Change-Id: Iac8d3c225fa5f7c19add953defb01261c4ac6c9f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399540
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Dec 7, 2024
1 parent 1cca342 commit e87c93b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
2 changes: 0 additions & 2 deletions pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ lib/src/analysis_server.dart
lib/src/cider/rename.dart
lib/src/computer/computer_call_hierarchy.dart
lib/src/computer/computer_documentation.dart
lib/src/domains/analysis/implemented_dart.dart
lib/src/domains/analysis/occurrences_dart.dart
lib/src/handler/legacy/edit_get_available_refactorings.dart
lib/src/handler/legacy/legacy_handler.dart
Expand All @@ -18,7 +17,6 @@ lib/src/lsp/handlers/handler_definition.dart
lib/src/lsp/handlers/handler_implementation.dart
lib/src/lsp/handlers/handler_references.dart
lib/src/lsp/handlers/handler_rename.dart
lib/src/operation/operation_analysis.dart
lib/src/protocol_server.dart
lib/src/search/element_references.dart
lib/src/services/correction/namespace.dart
Expand Down
75 changes: 43 additions & 32 deletions pkg/analysis_server/lib/src/domains/analysis/implemented_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

import 'package:analysis_server/src/protocol_server.dart' as protocol;
import 'package:analysis_server/src/services/search/search_engine.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

class ImplementedComputer {
final SearchEngine searchEngine;
final CompilationUnitElement unitElement;
final LibraryFragment unitElement;

List<protocol.ImplementedClass> classes = <protocol.ImplementedClass>[];
List<protocol.ImplementedMember> members = <protocol.ImplementedMember>[];
Expand All @@ -18,33 +19,41 @@ class ImplementedComputer {
ImplementedComputer(this.searchEngine, this.unitElement);

Future<void> compute() async {
for (var element in unitElement.classes) {
await _computeForInterfaceElement(element);
for (var fragment in unitElement.classes2) {
await _computeForInterfaceElement(fragment.element);
}
for (var element in unitElement.enums) {
await _computeForInterfaceElement(element);
for (var fragment in unitElement.enums2) {
await _computeForInterfaceElement(fragment.element);
}
for (var element in unitElement.extensionTypes) {
await _computeForInterfaceElement(element);
for (var fragment in unitElement.extensionTypes2) {
await _computeForInterfaceElement(fragment.element);
}
for (var element in unitElement.mixins) {
await _computeForInterfaceElement(element);
for (var fragment in unitElement.mixins2) {
await _computeForInterfaceElement(fragment.element);
}
}

void _addImplementedClass(InterfaceElement element) {
var offset = element.nameOffset;
var length = element.nameLength;
classes.add(protocol.ImplementedClass(offset, length));
void _addImplementedClass(InterfaceElement2 element) {
for (var fragment in element.fragments) {
var offset = fragment.nameOffset2;
var name = fragment.name2;
if (offset != null && name != null) {
classes.add(protocol.ImplementedClass(offset, name.length));
}
}
}

void _addImplementedMember(Element member) {
var offset = member.nameOffset;
var length = member.nameLength;
members.add(protocol.ImplementedMember(offset, length));
void _addImplementedMember(Element2 element) {
for (var fragment in element.fragments) {
var offset = fragment.nameOffset2;
var name = fragment.name2;
if (offset != null && name != null) {
members.add(protocol.ImplementedMember(offset, name.length));
}
}
}

void _addMemberIfImplemented(Element element) {
void _addMemberIfImplemented(Element2 element) {
if (element.isSynthetic || _isStatic(element)) {
return;
}
Expand All @@ -53,36 +62,38 @@ class ImplementedComputer {
}
}

Future<void> _computeForInterfaceElement(InterfaceElement element) async {
Future<void> _computeForInterfaceElement(InterfaceElement2 element) async {
// Always include Object and its members.
if (element is ClassElement && element.isDartCoreObject) {
if (element is ClassElement2 && element.isDartCoreObject) {
_addImplementedClass(element);
element.accessors.forEach(_addImplementedMember);
element.fields.forEach(_addImplementedMember);
element.methods.forEach(_addImplementedMember);
element.getters2.forEach(_addImplementedMember);
element.setters2.forEach(_addImplementedMember);
element.fields2.forEach(_addImplementedMember);
element.methods2.forEach(_addImplementedMember);
return;
}

// Analyze subtypes.
subtypeMembers = await searchEngine.membersOfSubtypes(element);
subtypeMembers = await searchEngine.membersOfSubtypes2(element);
if (subtypeMembers != null) {
_addImplementedClass(element);
element.accessors.forEach(_addMemberIfImplemented);
element.fields.forEach(_addMemberIfImplemented);
element.methods.forEach(_addMemberIfImplemented);
element.getters2.forEach(_addMemberIfImplemented);
element.setters2.forEach(_addMemberIfImplemented);
element.fields2.forEach(_addMemberIfImplemented);
element.methods2.forEach(_addMemberIfImplemented);
}
}

bool _hasOverride(Element element) {
bool _hasOverride(Element2 element) {
var name = element.displayName;
return subtypeMembers!.contains(name);
}

/// Return `true` if the given [element] is a static element.
static bool _isStatic(Element element) {
if (element is ExecutableElement) {
static bool _isStatic(Element2 element) {
if (element is ExecutableElement2) {
return element.isStatic;
} else if (element is PropertyInducingElement) {
} else if (element is PropertyInducingElement2) {
return element.isStatic;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Future<void> scheduleImplementedNotification(
var searchEngine = server.searchEngine;
for (var file in files) {
var unit = server.getCachedResolvedUnit(file)?.unit;
var unitElement = unit?.declaredElement;
var unitElement = unit?.declaredFragment;
if (unitElement != null) {
try {
var computer = ImplementedComputer(searchEngine, unitElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ abstract class SearchEngine {
/// return `null`.
Future<Set<String>?> membersOfSubtypes(InterfaceElement type);

/// If the [type] has subtypes, return the set of names of members which these
/// subtypes declare, possibly empty. If the [type] does not have subtypes,
/// return `null`.
Future<Set<String>?> membersOfSubtypes2(InterfaceElement2 type);

/// Returns declarations of class members with the given name.
///
/// [name] - the name being declared by the found matches.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class SearchEngineImpl implements SearchEngine {
return members;
}

@override
Future<Set<String>?> membersOfSubtypes2(InterfaceElement2 type) async {
return await membersOfSubtypes(type.asElement);
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/analyzer/lib/src/utilities/extensions/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ extension ConstructorElementExtension on ConstructorElement {
}

extension Element2Extension on Element2 {
List<Fragment> get fragments {
return [
for (Fragment? fragment = firstFragment;
fragment != null;
fragment = fragment.nextFragment)
fragment,
];
}

/// Whether the element is effectively [internal].
bool get isInternal {
if (this case Annotatable annotatable) {
Expand Down

0 comments on commit e87c93b

Please sign in to comment.