| Index: pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| index 2fab11c4912a31fa856602c6a8369f3d3d95ae52..0b1c40ea53a89f61d62b21fa0d0bd29581ced27d 100644
|
| --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| @@ -30,6 +30,7 @@ import 'package:analyzer/dart/element/element.dart';
|
| import 'package:analyzer/dart/element/type.dart';
|
| import 'package:analyzer/error/error.dart';
|
| import 'package:analyzer/file_system/file_system.dart';
|
| +import 'package:analyzer/src/dart/analysis/top_level_declaration.dart';
|
| import 'package:analyzer/src/dart/ast/token.dart';
|
| import 'package:analyzer/src/dart/ast/utilities.dart';
|
| import 'package:analyzer/src/dart/element/element.dart';
|
| @@ -58,12 +59,12 @@ typedef bool ElementPredicate(Element argument);
|
| * Clients may not extend, implement or mix-in this class.
|
| */
|
| class DartFixContextImpl extends FixContextImpl implements DartFixContext {
|
| - /**
|
| - * The [CompilationUnit] to compute fixes in.
|
| - */
|
| + final GetTopLevelDeclarations getTopLevelDeclarations;
|
| final CompilationUnit unit;
|
|
|
| - DartFixContextImpl(FixContext fixContext, this.unit) : super.from(fixContext);
|
| + DartFixContextImpl(
|
| + FixContext fixContext, this.getTopLevelDeclarations, this.unit)
|
| + : super.from(fixContext);
|
| }
|
|
|
| /**
|
| @@ -88,6 +89,7 @@ class FixProcessor {
|
| static const int MAX_LEVENSHTEIN_DISTANCE = 3;
|
|
|
| ResourceProvider resourceProvider;
|
| + GetTopLevelDeclarations getTopLevelDeclarations;
|
| CompilationUnit unit;
|
| AnalysisError error;
|
| AnalysisContext context;
|
| @@ -117,6 +119,7 @@ class FixProcessor {
|
|
|
| FixProcessor(DartFixContext dartContext) {
|
| resourceProvider = dartContext.resourceProvider;
|
| + getTopLevelDeclarations = dartContext.getTopLevelDeclarations;
|
| context = dartContext.analysisContext;
|
| // unit
|
| unit = dartContext.unit;
|
| @@ -179,9 +182,9 @@ class FixProcessor {
|
| if (name != null && name.staticElement == null) {
|
| node = name;
|
| if (annotation.arguments == null) {
|
| - _addFix_importLibrary_withTopLevelVariable();
|
| + await _addFix_importLibrary_withTopLevelVariable();
|
| } else {
|
| - _addFix_importLibrary_withType();
|
| + await _addFix_importLibrary_withType();
|
| _addFix_createClass();
|
| _addFix_undefinedClass_useSimilar();
|
| }
|
| @@ -295,7 +298,7 @@ class FixProcessor {
|
| errorCode == StaticWarningCode.CAST_TO_NON_TYPE ||
|
| errorCode == StaticWarningCode.TYPE_TEST_WITH_UNDEFINED_NAME ||
|
| errorCode == StaticWarningCode.UNDEFINED_CLASS) {
|
| - _addFix_importLibrary_withType();
|
| + await _addFix_importLibrary_withType();
|
| _addFix_createClass();
|
| _addFix_undefinedClass_useSimilar();
|
| }
|
| @@ -314,8 +317,8 @@ class FixProcessor {
|
| _addFix_createField();
|
| _addFix_createGetter();
|
| _addFix_createFunction_forFunctionType();
|
| - _addFix_importLibrary_withType();
|
| - _addFix_importLibrary_withTopLevelVariable();
|
| + await _addFix_importLibrary_withType();
|
| + await _addFix_importLibrary_withTopLevelVariable();
|
| _addFix_createLocalVariable();
|
| }
|
| if (errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER_AWAIT) {
|
| @@ -338,11 +341,11 @@ class FixProcessor {
|
| _addFix_nonBoolCondition_addNotNull();
|
| }
|
| if (errorCode == StaticTypeWarningCode.NON_TYPE_AS_TYPE_ARGUMENT) {
|
| - _addFix_importLibrary_withType();
|
| + await _addFix_importLibrary_withType();
|
| _addFix_createClass();
|
| }
|
| if (errorCode == StaticTypeWarningCode.UNDEFINED_FUNCTION) {
|
| - _addFix_importLibrary_withFunction();
|
| + await _addFix_importLibrary_withFunction();
|
| _addFix_undefinedFunction_useSimilar();
|
| _addFix_undefinedFunction_create();
|
| }
|
| @@ -354,7 +357,7 @@ class FixProcessor {
|
| }
|
| if (errorCode == HintCode.UNDEFINED_METHOD ||
|
| errorCode == StaticTypeWarningCode.UNDEFINED_METHOD) {
|
| - _addFix_importLibrary_withFunction();
|
| + await _addFix_importLibrary_withFunction();
|
| _addFix_undefinedMethod_useSimilar();
|
| _addFix_undefinedMethod_create();
|
| _addFix_undefinedFunction_create();
|
| @@ -1468,7 +1471,8 @@ class FixProcessor {
|
| _addFix(kind, [libraryUri], importsOnly: true);
|
| }
|
|
|
| - void _addFix_importLibrary_withElement(String name, ElementKind kind) {
|
| + Future<Null> _addFix_importLibrary_withElement(String name,
|
| + List<ElementKind> elementKinds, TopLevelDeclarationKind kind2) async {
|
| // ignore if private
|
| if (name.startsWith('_')) {
|
| return;
|
| @@ -1486,7 +1490,7 @@ class FixProcessor {
|
| if (element is PropertyAccessorElement) {
|
| element = (element as PropertyAccessorElement).variable;
|
| }
|
| - if (element.kind != kind) {
|
| + if (!elementKinds.contains(element.kind)) {
|
| continue;
|
| }
|
| // may be apply prefix
|
| @@ -1548,7 +1552,7 @@ class FixProcessor {
|
| if (element is PropertyAccessorElement) {
|
| element = (element as PropertyAccessorElement).variable;
|
| }
|
| - if (element.kind != kind) {
|
| + if (!elementKinds.contains(element.kind)) {
|
| continue;
|
| }
|
| // add import
|
| @@ -1558,31 +1562,19 @@ class FixProcessor {
|
| }
|
| // check project libraries
|
| {
|
| - List<Source> librarySources = context.librarySources;
|
| - for (Source librarySource in librarySources) {
|
| - // we don't need SDK libraries here
|
| - if (librarySource.isInSystemLibrary) {
|
| + List<TopLevelDeclarationInSource> declarations =
|
| + await getTopLevelDeclarations(name);
|
| + for (TopLevelDeclarationInSource declaration in declarations) {
|
| + // Check the kind.
|
| + if (declaration.declaration.kind != kind2) {
|
| continue;
|
| }
|
| - // maybe already imported
|
| - if (alreadyImportedWithPrefix.contains(librarySource)) {
|
| - continue;
|
| - }
|
| - // prepare LibraryElement
|
| - LibraryElement libraryElement =
|
| - context.getResult(librarySource, LIBRARY_ELEMENT4);
|
| - if (libraryElement == null) {
|
| - continue;
|
| - }
|
| - // prepare exported Element
|
| - Element element = getExportedElement(libraryElement, name);
|
| - if (element == null) {
|
| + // Check the source.
|
| + Source librarySource = declaration.source;
|
| + if (librarySource.isInSystemLibrary) {
|
| continue;
|
| }
|
| - if (element is PropertyAccessorElement) {
|
| - element = (element as PropertyAccessorElement).variable;
|
| - }
|
| - if (element.kind != kind) {
|
| + if (alreadyImportedWithPrefix.contains(librarySource)) {
|
| continue;
|
| }
|
| // Compute the fix kind.
|
| @@ -1592,7 +1584,7 @@ class FixProcessor {
|
| .contains('src')) {
|
| // Bad: non-API.
|
| fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT3;
|
| - } else if (element.library != libraryElement) {
|
| + } else if (declaration.isExported) {
|
| // Ugly: exports.
|
| fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT2;
|
| } else {
|
| @@ -1600,34 +1592,39 @@ class FixProcessor {
|
| fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT1;
|
| }
|
| // Add the fix.
|
| - _addFix_importLibrary(fixKind, libraryElement.source);
|
| + _addFix_importLibrary(fixKind, librarySource);
|
| }
|
| }
|
| }
|
|
|
| - void _addFix_importLibrary_withFunction() {
|
| + Future<Null> _addFix_importLibrary_withFunction() async {
|
| if (node is SimpleIdentifier && node.parent is MethodInvocation) {
|
| MethodInvocation invocation = node.parent as MethodInvocation;
|
| if (invocation.realTarget == null && invocation.methodName == node) {
|
| String name = (node as SimpleIdentifier).name;
|
| - _addFix_importLibrary_withElement(name, ElementKind.FUNCTION);
|
| + await _addFix_importLibrary_withElement(name,
|
| + const [ElementKind.FUNCTION], TopLevelDeclarationKind.function);
|
| }
|
| }
|
| }
|
|
|
| - void _addFix_importLibrary_withTopLevelVariable() {
|
| + Future<Null> _addFix_importLibrary_withTopLevelVariable() async {
|
| if (node is SimpleIdentifier) {
|
| String name = (node as SimpleIdentifier).name;
|
| - _addFix_importLibrary_withElement(name, ElementKind.TOP_LEVEL_VARIABLE);
|
| + await _addFix_importLibrary_withElement(
|
| + name,
|
| + const [ElementKind.TOP_LEVEL_VARIABLE],
|
| + TopLevelDeclarationKind.variable);
|
| }
|
| }
|
|
|
| - void _addFix_importLibrary_withType() {
|
| + Future<Null> _addFix_importLibrary_withType() async {
|
| if (_mayBeTypeIdentifier(node)) {
|
| String typeName = (node as SimpleIdentifier).name;
|
| - _addFix_importLibrary_withElement(typeName, ElementKind.CLASS);
|
| - _addFix_importLibrary_withElement(
|
| - typeName, ElementKind.FUNCTION_TYPE_ALIAS);
|
| + await _addFix_importLibrary_withElement(
|
| + typeName,
|
| + const [ElementKind.CLASS, ElementKind.FUNCTION_TYPE_ALIAS],
|
| + TopLevelDeclarationKind.type);
|
| }
|
| }
|
|
|
|
|