| 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 analysis_server.src.services.correction.fix; | 5 library analysis_server.src.services.correction.fix; |
| 6 | 6 |
| 7 import 'package:analysis_server/edit/fix/fix_core.dart'; | 7 import 'package:analysis_server/edit/fix/fix_core.dart'; |
| 8 import 'package:analysis_server/src/plugin/server_plugin.dart'; | 8 import 'package:analysis_server/src/plugin/server_plugin.dart'; |
| 9 import 'package:analyzer/file_system/file_system.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart'; | 11 import 'package:analyzer/src/generated/error.dart'; |
| 11 import 'package:analyzer/src/generated/java_engine.dart'; | 12 import 'package:analyzer/src/generated/java_engine.dart'; |
| 12 | 13 |
| 13 /** | 14 /** |
| 14 * Compute and return the fixes available for the given [error]. The error was | 15 * Compute and return the fixes available for the given [error]. The error was |
| 15 * reported after it's source was analyzed in the given [context]. The [plugin] | 16 * reported after it's source was analyzed in the given [context]. The [plugin] |
| 16 * is used to get the list of fix contributors. | 17 * is used to get the list of fix contributors. |
| 17 */ | 18 */ |
| 18 List<Fix> computeFixes( | 19 List<Fix> computeFixes(ServerPlugin plugin, ResourceProvider resourceProvider, |
| 19 ServerPlugin plugin, AnalysisContext context, AnalysisError error) { | 20 AnalysisContext context, AnalysisError error) { |
| 20 List<Fix> fixes = <Fix>[]; | 21 List<Fix> fixes = <Fix>[]; |
| 21 List<FixContributor> contributors = plugin.fixContributors; | 22 List<FixContributor> contributors = plugin.fixContributors; |
| 22 for (FixContributor contributor in contributors) { | 23 for (FixContributor contributor in contributors) { |
| 23 try { | 24 try { |
| 24 List<Fix> contributedFixes = contributor.computeFixes(context, error); | 25 List<Fix> contributedFixes = |
| 26 contributor.computeFixes(resourceProvider, context, error); |
| 25 if (contributedFixes != null) { | 27 if (contributedFixes != null) { |
| 26 fixes.addAll(contributedFixes); | 28 fixes.addAll(contributedFixes); |
| 27 } | 29 } |
| 28 } catch (exception, stackTrace) { | 30 } catch (exception, stackTrace) { |
| 29 AnalysisEngine.instance.logger.logError( | 31 AnalysisEngine.instance.logger.logError( |
| 30 'Exception from fix contributor: ${contributor.runtimeType}', | 32 'Exception from fix contributor: ${contributor.runtimeType}', |
| 31 new CaughtException(exception, stackTrace)); | 33 new CaughtException(exception, stackTrace)); |
| 32 } | 34 } |
| 33 } | 35 } |
| 34 fixes.sort(Fix.SORT_BY_RELEVANCE); | 36 fixes.sort(Fix.SORT_BY_RELEVANCE); |
| 35 return fixes; | 37 return fixes; |
| 36 } | 38 } |
| 37 | 39 |
| 38 /** | 40 /** |
| 39 * An enumeration of possible quick fix kinds. | 41 * An enumeration of possible quick fix kinds. |
| 40 */ | 42 */ |
| 41 class DartFixKind { | 43 class DartFixKind { |
| 42 static const ADD_ASYNC = | 44 static const ADD_ASYNC = |
| 43 const FixKind('ADD_ASYNC', 50, "Add 'async' modifier"); | 45 const FixKind('ADD_ASYNC', 50, "Add 'async' modifier"); |
| 44 static const ADD_FIELD_FORMAL_PARAMETERS = const FixKind( | 46 static const ADD_FIELD_FORMAL_PARAMETERS = const FixKind( |
| 45 'ADD_FIELD_FORMAL_PARAMETERS', 30, "Add final field formal parameters"); | 47 'ADD_FIELD_FORMAL_PARAMETERS', 30, "Add final field formal parameters"); |
| 46 static const ADD_MISSING_PARAMETER_POSITIONAL = const FixKind( | 48 static const ADD_MISSING_PARAMETER_POSITIONAL = const FixKind( |
| 47 'ADD_MISSING_PARAMETER_POSITIONAL', 31, | 49 'ADD_MISSING_PARAMETER_POSITIONAL', |
| 50 31, |
| 48 "Add optional positional parameter"); | 51 "Add optional positional parameter"); |
| 49 static const ADD_MISSING_PARAMETER_REQUIRED = const FixKind( | 52 static const ADD_MISSING_PARAMETER_REQUIRED = const FixKind( |
| 50 'ADD_MISSING_PARAMETER_REQUIRED', 30, "Add required parameter"); | 53 'ADD_MISSING_PARAMETER_REQUIRED', 30, "Add required parameter"); |
| 51 static const ADD_PACKAGE_DEPENDENCY = const FixKind( | 54 static const ADD_PACKAGE_DEPENDENCY = const FixKind( |
| 52 'ADD_PACKAGE_DEPENDENCY', 50, "Add dependency on package '{0}'"); | 55 'ADD_PACKAGE_DEPENDENCY', 50, "Add dependency on package '{0}'"); |
| 53 static const ADD_PART_OF = | 56 static const ADD_PART_OF = |
| 54 const FixKind('ADD_PART_OF', 50, "Add 'part of' directive"); | 57 const FixKind('ADD_PART_OF', 50, "Add 'part of' directive"); |
| 55 static const ADD_SUPER_CONSTRUCTOR_INVOCATION = const FixKind( | 58 static const ADD_SUPER_CONSTRUCTOR_INVOCATION = const FixKind( |
| 56 'ADD_SUPER_CONSTRUCTOR_INVOCATION', 50, | 59 'ADD_SUPER_CONSTRUCTOR_INVOCATION', |
| 60 50, |
| 57 "Add super constructor {0} invocation"); | 61 "Add super constructor {0} invocation"); |
| 58 static const CHANGE_TO = const FixKind('CHANGE_TO', 49, "Change to '{0}'"); | 62 static const CHANGE_TO = const FixKind('CHANGE_TO', 49, "Change to '{0}'"); |
| 59 static const CHANGE_TO_STATIC_ACCESS = const FixKind( | 63 static const CHANGE_TO_STATIC_ACCESS = const FixKind( |
| 60 'CHANGE_TO_STATIC_ACCESS', 50, "Change access to static using '{0}'"); | 64 'CHANGE_TO_STATIC_ACCESS', 50, "Change access to static using '{0}'"); |
| 61 static const CREATE_CLASS = | 65 static const CREATE_CLASS = |
| 62 const FixKind('CREATE_CLASS', 50, "Create class '{0}'"); | 66 const FixKind('CREATE_CLASS', 50, "Create class '{0}'"); |
| 63 static const CREATE_CONSTRUCTOR = | 67 static const CREATE_CONSTRUCTOR = |
| 64 const FixKind('CREATE_CONSTRUCTOR', 50, "Create constructor '{0}'"); | 68 const FixKind('CREATE_CONSTRUCTOR', 50, "Create constructor '{0}'"); |
| 65 static const CREATE_CONSTRUCTOR_FOR_FINAL_FIELDS = const FixKind( | 69 static const CREATE_CONSTRUCTOR_FOR_FINAL_FIELDS = const FixKind( |
| 66 'CREATE_CONSTRUCTOR_FOR_FINAL_FIELDS', 50, | 70 'CREATE_CONSTRUCTOR_FOR_FINAL_FIELDS', |
| 71 50, |
| 67 "Create constructor for final fields"); | 72 "Create constructor for final fields"); |
| 68 static const CREATE_CONSTRUCTOR_SUPER = const FixKind( | 73 static const CREATE_CONSTRUCTOR_SUPER = const FixKind( |
| 69 'CREATE_CONSTRUCTOR_SUPER', 50, "Create constructor to call {0}"); | 74 'CREATE_CONSTRUCTOR_SUPER', 50, "Create constructor to call {0}"); |
| 70 static const CREATE_FIELD = | 75 static const CREATE_FIELD = |
| 71 const FixKind('CREATE_FIELD', 51, "Create field '{0}'"); | 76 const FixKind('CREATE_FIELD', 51, "Create field '{0}'"); |
| 72 static const CREATE_FILE = | 77 static const CREATE_FILE = |
| 73 const FixKind('CREATE_FILE', 50, "Create file '{0}'"); | 78 const FixKind('CREATE_FILE', 50, "Create file '{0}'"); |
| 74 static const CREATE_FUNCTION = | 79 static const CREATE_FUNCTION = |
| 75 const FixKind('CREATE_FUNCTION', 51, "Create function '{0}'"); | 80 const FixKind('CREATE_FUNCTION', 51, "Create function '{0}'"); |
| 76 static const CREATE_GETTER = | 81 static const CREATE_GETTER = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 91 const FixKind('IMPORT_LIBRARY_SDK', 49, "Import library '{0}'"); | 96 const FixKind('IMPORT_LIBRARY_SDK', 49, "Import library '{0}'"); |
| 92 static const IMPORT_LIBRARY_SHOW = | 97 static const IMPORT_LIBRARY_SHOW = |
| 93 const FixKind('IMPORT_LIBRARY_SHOW', 49, "Update library '{0}' import"); | 98 const FixKind('IMPORT_LIBRARY_SHOW', 49, "Update library '{0}' import"); |
| 94 static const INSERT_SEMICOLON = | 99 static const INSERT_SEMICOLON = |
| 95 const FixKind('INSERT_SEMICOLON', 50, "Insert ';'"); | 100 const FixKind('INSERT_SEMICOLON', 50, "Insert ';'"); |
| 96 static const MAKE_CLASS_ABSTRACT = | 101 static const MAKE_CLASS_ABSTRACT = |
| 97 const FixKind('MAKE_CLASS_ABSTRACT', 50, "Make class '{0}' abstract"); | 102 const FixKind('MAKE_CLASS_ABSTRACT', 50, "Make class '{0}' abstract"); |
| 98 static const REMOVE_DEAD_CODE = | 103 static const REMOVE_DEAD_CODE = |
| 99 const FixKind('REMOVE_DEAD_CODE', 50, "Remove dead code"); | 104 const FixKind('REMOVE_DEAD_CODE', 50, "Remove dead code"); |
| 100 static const REMOVE_PARAMETERS_IN_GETTER_DECLARATION = const FixKind( | 105 static const REMOVE_PARAMETERS_IN_GETTER_DECLARATION = const FixKind( |
| 101 'REMOVE_PARAMETERS_IN_GETTER_DECLARATION', 50, | 106 'REMOVE_PARAMETERS_IN_GETTER_DECLARATION', |
| 107 50, |
| 102 "Remove parameters in getter declaration"); | 108 "Remove parameters in getter declaration"); |
| 103 static const REMOVE_PARENTHESIS_IN_GETTER_INVOCATION = const FixKind( | 109 static const REMOVE_PARENTHESIS_IN_GETTER_INVOCATION = const FixKind( |
| 104 'REMOVE_PARENTHESIS_IN_GETTER_INVOCATION', 50, | 110 'REMOVE_PARENTHESIS_IN_GETTER_INVOCATION', |
| 111 50, |
| 105 "Remove parentheses in getter invocation"); | 112 "Remove parentheses in getter invocation"); |
| 106 static const REMOVE_UNNECASSARY_CAST = | 113 static const REMOVE_UNNECASSARY_CAST = |
| 107 const FixKind('REMOVE_UNNECASSARY_CAST', 50, "Remove unnecessary cast"); | 114 const FixKind('REMOVE_UNNECASSARY_CAST', 50, "Remove unnecessary cast"); |
| 108 static const REMOVE_UNUSED_CATCH_CLAUSE = | 115 static const REMOVE_UNUSED_CATCH_CLAUSE = |
| 109 const FixKind('REMOVE_UNUSED_CATCH', 50, "Remove unused 'catch' clause"); | 116 const FixKind('REMOVE_UNUSED_CATCH', 50, "Remove unused 'catch' clause"); |
| 110 static const REMOVE_UNUSED_CATCH_STACK = const FixKind( | 117 static const REMOVE_UNUSED_CATCH_STACK = const FixKind( |
| 111 'REMOVE_UNUSED_CATCH_STACK', 50, "Remove unused stack trace variable"); | 118 'REMOVE_UNUSED_CATCH_STACK', 50, "Remove unused stack trace variable"); |
| 112 static const REMOVE_UNUSED_IMPORT = | 119 static const REMOVE_UNUSED_IMPORT = |
| 113 const FixKind('REMOVE_UNUSED_IMPORT', 50, "Remove unused import"); | 120 const FixKind('REMOVE_UNUSED_IMPORT', 50, "Remove unused import"); |
| 114 static const REPLACE_BOOLEAN_WITH_BOOL = const FixKind( | 121 static const REPLACE_BOOLEAN_WITH_BOOL = const FixKind( |
| 115 'REPLACE_BOOLEAN_WITH_BOOL', 50, "Replace 'boolean' with 'bool'"); | 122 'REPLACE_BOOLEAN_WITH_BOOL', 50, "Replace 'boolean' with 'bool'"); |
| 116 static const REPLACE_IMPORT_URI = | 123 static const REPLACE_IMPORT_URI = |
| 117 const FixKind('REPLACE_IMPORT_URI', 50, "Replace with '{0}'"); | 124 const FixKind('REPLACE_IMPORT_URI', 50, "Replace with '{0}'"); |
| 118 static const REPLACE_VAR_WITH_DYNAMIC = const FixKind( | 125 static const REPLACE_VAR_WITH_DYNAMIC = const FixKind( |
| 119 'REPLACE_VAR_WITH_DYNAMIC', 50, "Replace 'var' with 'dynamic'"); | 126 'REPLACE_VAR_WITH_DYNAMIC', 50, "Replace 'var' with 'dynamic'"); |
| 120 static const REPLACE_RETURN_TYPE_FUTURE = const FixKind( | 127 static const REPLACE_RETURN_TYPE_FUTURE = const FixKind( |
| 121 'REPLACE_RETURN_TYPE_FUTURE', 50, | 128 'REPLACE_RETURN_TYPE_FUTURE', |
| 129 50, |
| 122 "Return 'Future' from 'async' function"); | 130 "Return 'Future' from 'async' function"); |
| 123 static const USE_CONST = const FixKind('USE_CONST', 50, "Change to constant"); | 131 static const USE_CONST = const FixKind('USE_CONST', 50, "Change to constant"); |
| 124 static const USE_EFFECTIVE_INTEGER_DIVISION = const FixKind( | 132 static const USE_EFFECTIVE_INTEGER_DIVISION = const FixKind( |
| 125 'USE_EFFECTIVE_INTEGER_DIVISION', 50, | 133 'USE_EFFECTIVE_INTEGER_DIVISION', |
| 134 50, |
| 126 "Use effective integer division ~/"); | 135 "Use effective integer division ~/"); |
| 127 static const USE_EQ_EQ_NULL = | 136 static const USE_EQ_EQ_NULL = |
| 128 const FixKind('USE_EQ_EQ_NULL', 50, "Use == null instead of 'is Null'"); | 137 const FixKind('USE_EQ_EQ_NULL', 50, "Use == null instead of 'is Null'"); |
| 129 static const USE_NOT_EQ_NULL = | 138 static const USE_NOT_EQ_NULL = |
| 130 const FixKind('USE_NOT_EQ_NULL', 50, "Use != null instead of 'is! Null'"); | 139 const FixKind('USE_NOT_EQ_NULL', 50, "Use != null instead of 'is! Null'"); |
| 131 } | 140 } |
| OLD | NEW |