| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; | 7 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; |
| 8 import 'package:analysis_server/src/services/correction/fix_internal.dart' | 8 import 'package:analysis_server/src/services/correction/fix_internal.dart' |
| 9 show DartFixContextImpl; | 9 show DartFixContextImpl; |
| 10 import 'package:analysis_server/src/services/correction/namespace.dart' | |
| 11 show getExportedElement; | |
| 12 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| 13 import 'package:analyzer/dart/element/element.dart'; | 11 import 'package:analyzer/src/dart/analysis/ast_provider_driver.dart'; |
| 14 import 'package:analyzer/src/dart/analysis/ast_provider_context.dart'; | 12 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 15 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; | 13 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; |
| 16 import 'package:analyzer/src/dart/element/ast_provider.dart'; | 14 import 'package:analyzer/src/dart/element/ast_provider.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 19 import 'package:analyzer/src/task/dart.dart' show LIBRARY_ELEMENT4; | |
| 20 | 17 |
| 21 /** | 18 /** |
| 22 * Complete with top-level declarations with the given [name]. | 19 * Complete with top-level declarations with the given [name]. |
| 23 */ | 20 */ |
| 24 typedef Future<List<TopLevelDeclarationInSource>> GetTopLevelDeclarations( | 21 typedef Future<List<TopLevelDeclarationInSource>> GetTopLevelDeclarations( |
| 25 String name); | 22 String name); |
| 26 | 23 |
| 27 /** | 24 /** |
| 28 * An object used to provide context information for [DartFixContributor]s. | 25 * An object used to provide context information for [DartFixContributor]s. |
| 29 * | 26 * |
| (...skipping 18 matching lines...) Expand all Loading... |
| 48 | 45 |
| 49 /** | 46 /** |
| 50 * A [FixContributor] that can be used to contribute fixes for errors in Dart | 47 * A [FixContributor] that can be used to contribute fixes for errors in Dart |
| 51 * files. | 48 * files. |
| 52 * | 49 * |
| 53 * Clients may extend this class when implementing plugins. | 50 * Clients may extend this class when implementing plugins. |
| 54 */ | 51 */ |
| 55 abstract class DartFixContributor implements FixContributor { | 52 abstract class DartFixContributor implements FixContributor { |
| 56 @override | 53 @override |
| 57 Future<List<Fix>> computeFixes(FixContext context) async { | 54 Future<List<Fix>> computeFixes(FixContext context) async { |
| 58 AnalysisContext analysisContext = context.analysisContext; | 55 AnalysisDriver driver = context.analysisDriver; |
| 59 Source source = context.error.source; | 56 Source source = context.error.source; |
| 60 if (!AnalysisEngine.isDartFileName(source.fullName)) { | 57 if (!AnalysisEngine.isDartFileName(source.fullName)) { |
| 61 return Fix.EMPTY_LIST; | 58 return Fix.EMPTY_LIST; |
| 62 } | 59 } |
| 63 List<Source> libraries = analysisContext.getLibrariesContaining(source); | 60 CompilationUnit unit = (await driver.getResult(source.fullName)).unit; |
| 64 if (libraries.isEmpty) { | |
| 65 return Fix.EMPTY_LIST; | |
| 66 } | |
| 67 CompilationUnit unit = | |
| 68 analysisContext.getResolvedCompilationUnit2(source, libraries[0]); | |
| 69 if (unit == null) { | 61 if (unit == null) { |
| 70 return Fix.EMPTY_LIST; | 62 return Fix.EMPTY_LIST; |
| 71 } | 63 } |
| 72 DartFixContext dartContext = new DartFixContextImpl( | 64 DartFixContext dartContext = |
| 73 context, | 65 new DartFixContextImpl(context, new AstProviderForDriver(driver), unit); |
| 74 _getTopLevelDeclarations(analysisContext), | |
| 75 new AstProviderForContext(analysisContext), | |
| 76 unit); | |
| 77 return internalComputeFixes(dartContext); | 66 return internalComputeFixes(dartContext); |
| 78 } | 67 } |
| 79 | 68 |
| 80 /** | 69 /** |
| 81 * Return a list of fixes for the given [context]. | 70 * Return a list of fixes for the given [context]. |
| 82 */ | 71 */ |
| 83 Future<List<Fix>> internalComputeFixes(DartFixContext context); | 72 Future<List<Fix>> internalComputeFixes(DartFixContext context); |
| 84 | |
| 85 GetTopLevelDeclarations _getTopLevelDeclarations(AnalysisContext context) { | |
| 86 return (String name) async { | |
| 87 List<TopLevelDeclarationInSource> declarations = []; | |
| 88 List<Source> librarySources = context.librarySources; | |
| 89 for (Source librarySource in librarySources) { | |
| 90 // Prepare the LibraryElement. | |
| 91 LibraryElement libraryElement = | |
| 92 context.getResult(librarySource, LIBRARY_ELEMENT4); | |
| 93 if (libraryElement == null) { | |
| 94 continue; | |
| 95 } | |
| 96 // Prepare the exported Element. | |
| 97 Element element = getExportedElement(libraryElement, name); | |
| 98 if (element == null) { | |
| 99 continue; | |
| 100 } | |
| 101 if (element is PropertyAccessorElement) { | |
| 102 element = (element as PropertyAccessorElement).variable; | |
| 103 } | |
| 104 // Add a new declaration. | |
| 105 TopLevelDeclarationKind topLevelKind; | |
| 106 if (element.kind == ElementKind.CLASS || | |
| 107 element.kind == ElementKind.FUNCTION_TYPE_ALIAS) { | |
| 108 topLevelKind = TopLevelDeclarationKind.type; | |
| 109 } else if (element.kind == ElementKind.FUNCTION) { | |
| 110 topLevelKind = TopLevelDeclarationKind.function; | |
| 111 } else if (element.kind == ElementKind.TOP_LEVEL_VARIABLE) { | |
| 112 topLevelKind = TopLevelDeclarationKind.variable; | |
| 113 } | |
| 114 if (topLevelKind != null) { | |
| 115 bool isExported = element.librarySource != librarySource; | |
| 116 declarations.add(new TopLevelDeclarationInSource(librarySource, | |
| 117 new TopLevelDeclaration(topLevelKind, element.name), isExported)); | |
| 118 } | |
| 119 } | |
| 120 return declarations; | |
| 121 }; | |
| 122 } | |
| 123 } | 73 } |
| OLD | NEW |