| 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 library analysis_server.plugin.edit.fix.fix_dart; | 5 library analysis_server.plugin.edit.fix.fix_dart; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; | 9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; |
| 10 import 'package:analysis_server/src/services/correction/fix_internal.dart' | 10 import 'package:analysis_server/src/services/correction/fix_internal.dart' |
| 11 show DartFixContextImpl; | 11 show DartFixContextImpl; |
| 12 import 'package:analysis_server/src/services/correction/namespace.dart' |
| 13 show getExportedElement; |
| 12 import 'package:analyzer/dart/ast/ast.dart'; | 14 import 'package:analyzer/dart/ast/ast.dart'; |
| 15 import 'package:analyzer/dart/element/element.dart'; |
| 16 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 17 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 18 import 'package:analyzer/src/generated/source.dart'; |
| 19 import 'package:analyzer/src/task/dart.dart' show LIBRARY_ELEMENT4; |
| 20 |
| 21 /** |
| 22 * Complete with top-level declarations with the given [name]. |
| 23 */ |
| 24 typedef Future<List<TopLevelDeclarationInSource>> GetTopLevelDeclarations( |
| 25 String name); |
| 15 | 26 |
| 16 /** | 27 /** |
| 17 * An object used to provide context information for [DartFixContributor]s. | 28 * An object used to provide context information for [DartFixContributor]s. |
| 18 * | 29 * |
| 19 * Clients may not extend, implement or mix-in this class. | 30 * Clients may not extend, implement or mix-in this class. |
| 20 */ | 31 */ |
| 21 abstract class DartFixContext implements FixContext { | 32 abstract class DartFixContext implements FixContext { |
| 22 /** | 33 /** |
| 34 * The function to get top-level declarations from. |
| 35 */ |
| 36 GetTopLevelDeclarations get getTopLevelDeclarations; |
| 37 |
| 38 /** |
| 23 * The [CompilationUnit] to compute fixes in. | 39 * The [CompilationUnit] to compute fixes in. |
| 24 */ | 40 */ |
| 25 CompilationUnit get unit; | 41 CompilationUnit get unit; |
| 26 } | 42 } |
| 27 | 43 |
| 28 /** | 44 /** |
| 29 * A [FixContributor] that can be used to contribute fixes for errors in Dart | 45 * A [FixContributor] that can be used to contribute fixes for errors in Dart |
| 30 * files. | 46 * files. |
| 31 * | 47 * |
| 32 * Clients may extend this class when implementing plugins. | 48 * Clients may extend this class when implementing plugins. |
| 33 */ | 49 */ |
| 34 abstract class DartFixContributor implements FixContributor { | 50 abstract class DartFixContributor implements FixContributor { |
| 35 @override | 51 @override |
| 36 Future<List<Fix>> computeFixes(FixContext context) async { | 52 Future<List<Fix>> computeFixes(FixContext context) async { |
| 37 AnalysisContext analysisContext = context.analysisContext; | 53 AnalysisContext analysisContext = context.analysisContext; |
| 38 Source source = context.error.source; | 54 Source source = context.error.source; |
| 39 if (!AnalysisEngine.isDartFileName(source.fullName)) { | 55 if (!AnalysisEngine.isDartFileName(source.fullName)) { |
| 40 return Fix.EMPTY_LIST; | 56 return Fix.EMPTY_LIST; |
| 41 } | 57 } |
| 42 List<Source> libraries = analysisContext.getLibrariesContaining(source); | 58 List<Source> libraries = analysisContext.getLibrariesContaining(source); |
| 43 if (libraries.isEmpty) { | 59 if (libraries.isEmpty) { |
| 44 return Fix.EMPTY_LIST; | 60 return Fix.EMPTY_LIST; |
| 45 } | 61 } |
| 46 CompilationUnit unit = | 62 CompilationUnit unit = |
| 47 analysisContext.getResolvedCompilationUnit2(source, libraries[0]); | 63 analysisContext.getResolvedCompilationUnit2(source, libraries[0]); |
| 48 if (unit == null) { | 64 if (unit == null) { |
| 49 return Fix.EMPTY_LIST; | 65 return Fix.EMPTY_LIST; |
| 50 } | 66 } |
| 51 DartFixContext dartContext = new DartFixContextImpl(context, unit); | 67 DartFixContext dartContext = new DartFixContextImpl( |
| 68 context, _getTopLevelDeclarations(analysisContext), unit); |
| 52 return internalComputeFixes(dartContext); | 69 return internalComputeFixes(dartContext); |
| 53 } | 70 } |
| 54 | 71 |
| 55 /** | 72 /** |
| 56 * Return a list of fixes for the given [context]. | 73 * Return a list of fixes for the given [context]. |
| 57 */ | 74 */ |
| 58 Future<List<Fix>> internalComputeFixes(DartFixContext context); | 75 Future<List<Fix>> internalComputeFixes(DartFixContext context); |
| 76 |
| 77 GetTopLevelDeclarations _getTopLevelDeclarations(AnalysisContext context) { |
| 78 return (String name) async { |
| 79 List<TopLevelDeclarationInSource> declarations = []; |
| 80 List<Source> librarySources = context.librarySources; |
| 81 for (Source librarySource in librarySources) { |
| 82 // Prepare the LibraryElement. |
| 83 LibraryElement libraryElement = |
| 84 context.getResult(librarySource, LIBRARY_ELEMENT4); |
| 85 if (libraryElement == null) { |
| 86 continue; |
| 87 } |
| 88 // Prepare the exported Element. |
| 89 Element element = getExportedElement(libraryElement, name); |
| 90 if (element == null) { |
| 91 continue; |
| 92 } |
| 93 if (element is PropertyAccessorElement) { |
| 94 element = (element as PropertyAccessorElement).variable; |
| 95 } |
| 96 // Add a new declaration. |
| 97 TopLevelDeclarationKind topLevelKind; |
| 98 if (element.kind == ElementKind.CLASS || |
| 99 element.kind == ElementKind.FUNCTION_TYPE_ALIAS) { |
| 100 topLevelKind = TopLevelDeclarationKind.type; |
| 101 } else if (element.kind == ElementKind.FUNCTION) { |
| 102 topLevelKind = TopLevelDeclarationKind.function; |
| 103 } else if (element.kind == ElementKind.TOP_LEVEL_VARIABLE) { |
| 104 topLevelKind = TopLevelDeclarationKind.variable; |
| 105 } |
| 106 if (topLevelKind != null) { |
| 107 bool isExported = element.librarySource != librarySource; |
| 108 declarations.add(new TopLevelDeclarationInSource(librarySource, |
| 109 new TopLevelDeclaration(topLevelKind, element.name), isExported)); |
| 110 } |
| 111 } |
| 112 return declarations; |
| 113 }; |
| 114 } |
| 59 } | 115 } |
| OLD | NEW |