| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 services.correction.fix; | 5 library analysis_server.src.services.correction.fix; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart' show SourceChange; | 7 import 'package:analysis_server/edit/fix/fix_core.dart'; |
| 8 import 'package:analysis_server/src/services/correction/fix_internal.dart'; | 8 import 'package:analysis_server/src/plugin/server_plugin.dart'; |
| 9 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart'; | 10 import 'package:analyzer/src/generated/error.dart'; |
| 11 import 'package:analyzer/src/generated/java_engine.dart'; |
| 11 | 12 |
| 12 /** | 13 /** |
| 13 * Computes [Fix]s for the given [AnalysisError]. | 14 * Compute and return the fixes available for the given [error]. The error was |
| 14 * | 15 * reported after it's source was analyzed in the given [context]. The [plugin] |
| 15 * Returns the computed [Fix]s, not `null`. | 16 * is used to get the list of fix contributors. |
| 16 */ | 17 */ |
| 17 List<Fix> computeFixes(CompilationUnit unit, AnalysisError error) { | 18 List<Fix> computeFixes( |
| 18 var processor = new FixProcessor(unit, error); | 19 ServerPlugin plugin, AnalysisContext context, AnalysisError error) { |
| 19 List<Fix> fixes = processor.compute(); | 20 List<Fix> fixes = <Fix>[]; |
| 20 fixes.sort((Fix a, Fix b) { | 21 List<FixContributor> contributors = plugin.fixContributors(); |
| 21 return a.kind.relevance - b.kind.relevance; | 22 for (FixContributor contributor in contributors) { |
| 22 }); | 23 try { |
| 24 List<Fix> contributedFixes = contributor.computeFixes(context, error); |
| 25 if (contributedFixes != null) { |
| 26 fixes.addAll(contributedFixes); |
| 27 } |
| 28 } catch (exception, stackTrace) { |
| 29 AnalysisEngine.instance.logger.logError( |
| 30 'Exception from fix contributor: ${contributor.runtimeType}', |
| 31 new CaughtException(exception, stackTrace)); |
| 32 } |
| 33 } |
| 34 fixes.sort(Fix.SORT_BY_RELEVANCE); |
| 23 return fixes; | 35 return fixes; |
| 24 } | 36 } |
| 25 | 37 |
| 26 /** | 38 /** |
| 27 * A description of a single proposed fix for some problem. | |
| 28 */ | |
| 29 class Fix { | |
| 30 final FixKind kind; | |
| 31 final SourceChange change; | |
| 32 | |
| 33 Fix(this.kind, this.change); | |
| 34 | |
| 35 @override | |
| 36 String toString() { | |
| 37 return '[kind=$kind, change=$change]'; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 /** | |
| 42 * An enumeration of possible quick fix kinds. | 39 * An enumeration of possible quick fix kinds. |
| 43 */ | 40 */ |
| 44 class FixKind { | 41 class DartFixKind { |
| 45 static const ADD_ASYNC = | 42 static const ADD_ASYNC = |
| 46 const FixKind('ADD_ASYNC', 50, "Add 'async' modifier"); | 43 const FixKind('ADD_ASYNC', 50, "Add 'async' modifier"); |
| 47 static const ADD_FIELD_FORMAL_PARAMETERS = const FixKind( | 44 static const ADD_FIELD_FORMAL_PARAMETERS = const FixKind( |
| 48 'ADD_FIELD_FORMAL_PARAMETERS', 30, "Add final field formal parameters"); | 45 'ADD_FIELD_FORMAL_PARAMETERS', 30, "Add final field formal parameters"); |
| 49 static const ADD_PACKAGE_DEPENDENCY = const FixKind( | 46 static const ADD_PACKAGE_DEPENDENCY = const FixKind( |
| 50 'ADD_PACKAGE_DEPENDENCY', 50, "Add dependency on package '{0}'"); | 47 'ADD_PACKAGE_DEPENDENCY', 50, "Add dependency on package '{0}'"); |
| 51 static const ADD_SUPER_CONSTRUCTOR_INVOCATION = const FixKind( | 48 static const ADD_SUPER_CONSTRUCTOR_INVOCATION = const FixKind( |
| 52 'ADD_SUPER_CONSTRUCTOR_INVOCATION', 50, | 49 'ADD_SUPER_CONSTRUCTOR_INVOCATION', 50, |
| 53 "Add super constructor {0} invocation"); | 50 "Add super constructor {0} invocation"); |
| 54 static const CHANGE_TO = const FixKind('CHANGE_TO', 49, "Change to '{0}'"); | 51 static const CHANGE_TO = const FixKind('CHANGE_TO', 49, "Change to '{0}'"); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 static const MAKE_CLASS_ABSTRACT = | 89 static const MAKE_CLASS_ABSTRACT = |
| 93 const FixKind('MAKE_CLASS_ABSTRACT', 50, "Make class '{0}' abstract"); | 90 const FixKind('MAKE_CLASS_ABSTRACT', 50, "Make class '{0}' abstract"); |
| 94 static const REMOVE_PARAMETERS_IN_GETTER_DECLARATION = const FixKind( | 91 static const REMOVE_PARAMETERS_IN_GETTER_DECLARATION = const FixKind( |
| 95 'REMOVE_PARAMETERS_IN_GETTER_DECLARATION', 50, | 92 'REMOVE_PARAMETERS_IN_GETTER_DECLARATION', 50, |
| 96 "Remove parameters in getter declaration"); | 93 "Remove parameters in getter declaration"); |
| 97 static const REMOVE_PARENTHESIS_IN_GETTER_INVOCATION = const FixKind( | 94 static const REMOVE_PARENTHESIS_IN_GETTER_INVOCATION = const FixKind( |
| 98 'REMOVE_PARENTHESIS_IN_GETTER_INVOCATION', 50, | 95 'REMOVE_PARENTHESIS_IN_GETTER_INVOCATION', 50, |
| 99 "Remove parentheses in getter invocation"); | 96 "Remove parentheses in getter invocation"); |
| 100 static const REMOVE_UNNECASSARY_CAST = | 97 static const REMOVE_UNNECASSARY_CAST = |
| 101 const FixKind('REMOVE_UNNECASSARY_CAST', 50, "Remove unnecessary cast"); | 98 const FixKind('REMOVE_UNNECASSARY_CAST', 50, "Remove unnecessary cast"); |
| 102 static const REMOVE_UNUSED_CATCH_CLAUSE = const FixKind( | 99 static const REMOVE_UNUSED_CATCH_CLAUSE = |
| 103 'REMOVE_UNUSED_CATCH', 50, "Remove unused 'catch' clause"); | 100 const FixKind('REMOVE_UNUSED_CATCH', 50, "Remove unused 'catch' clause"); |
| 104 static const REMOVE_UNUSED_CATCH_STACK = const FixKind( | 101 static const REMOVE_UNUSED_CATCH_STACK = const FixKind( |
| 105 'REMOVE_UNUSED_CATCH_STACK', 50, "Remove unused stack trace variable"); | 102 'REMOVE_UNUSED_CATCH_STACK', 50, "Remove unused stack trace variable"); |
| 106 static const REMOVE_UNUSED_IMPORT = | 103 static const REMOVE_UNUSED_IMPORT = |
| 107 const FixKind('REMOVE_UNUSED_IMPORT', 50, "Remove unused import"); | 104 const FixKind('REMOVE_UNUSED_IMPORT', 50, "Remove unused import"); |
| 108 static const REPLACE_BOOLEAN_WITH_BOOL = const FixKind( | 105 static const REPLACE_BOOLEAN_WITH_BOOL = const FixKind( |
| 109 'REPLACE_BOOLEAN_WITH_BOOL', 50, "Replace 'boolean' with 'bool'"); | 106 'REPLACE_BOOLEAN_WITH_BOOL', 50, "Replace 'boolean' with 'bool'"); |
| 110 static const REPLACE_IMPORT_URI = | 107 static const REPLACE_IMPORT_URI = |
| 111 const FixKind('REPLACE_IMPORT_URI', 50, "Replace with '{0}'"); | 108 const FixKind('REPLACE_IMPORT_URI', 50, "Replace with '{0}'"); |
| 112 static const REPLACE_VAR_WITH_DYNAMIC = const FixKind( | 109 static const REPLACE_VAR_WITH_DYNAMIC = const FixKind( |
| 113 'REPLACE_VAR_WITH_DYNAMIC', 50, "Replace 'var' with 'dynamic'"); | 110 'REPLACE_VAR_WITH_DYNAMIC', 50, "Replace 'var' with 'dynamic'"); |
| 114 static const REPLACE_RETURN_TYPE_FUTURE = const FixKind( | 111 static const REPLACE_RETURN_TYPE_FUTURE = const FixKind( |
| 115 'REPLACE_RETURN_TYPE_FUTURE', 50, | 112 'REPLACE_RETURN_TYPE_FUTURE', 50, |
| 116 "Return 'Future' from 'async' function"); | 113 "Return 'Future' from 'async' function"); |
| 117 static const USE_CONST = const FixKind('USE_CONST', 50, "Change to constant"); | 114 static const USE_CONST = const FixKind('USE_CONST', 50, "Change to constant"); |
| 118 static const USE_EFFECTIVE_INTEGER_DIVISION = const FixKind( | 115 static const USE_EFFECTIVE_INTEGER_DIVISION = const FixKind( |
| 119 'USE_EFFECTIVE_INTEGER_DIVISION', 50, | 116 'USE_EFFECTIVE_INTEGER_DIVISION', 50, |
| 120 "Use effective integer division ~/"); | 117 "Use effective integer division ~/"); |
| 121 static const USE_EQ_EQ_NULL = | 118 static const USE_EQ_EQ_NULL = |
| 122 const FixKind('USE_EQ_EQ_NULL', 50, "Use == null instead of 'is Null'"); | 119 const FixKind('USE_EQ_EQ_NULL', 50, "Use == null instead of 'is Null'"); |
| 123 static const USE_NOT_EQ_NULL = | 120 static const USE_NOT_EQ_NULL = |
| 124 const FixKind('USE_NOT_EQ_NULL', 50, "Use != null instead of 'is! Null'"); | 121 const FixKind('USE_NOT_EQ_NULL', 50, "Use != null instead of 'is! Null'"); |
| 125 | |
| 126 final name; | |
| 127 final int relevance; | |
| 128 final String message; | |
| 129 | |
| 130 const FixKind(this.name, this.relevance, this.message); | |
| 131 | |
| 132 @override | |
| 133 String toString() => name; | |
| 134 } | 122 } |
| OLD | NEW |