| 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.src.refactoring.extract_method; | 5 library services.src.refactoring.extract_method; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 9 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| 10 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; | 10 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 final int selectionLength; | 64 final int selectionLength; |
| 65 CompilationUnitElement unitElement; | 65 CompilationUnitElement unitElement; |
| 66 LibraryElement libraryElement; | 66 LibraryElement libraryElement; |
| 67 SourceRange selectionRange; | 67 SourceRange selectionRange; |
| 68 CorrectionUtils utils; | 68 CorrectionUtils utils; |
| 69 Set<LibraryElement> librariesToImport = new Set<LibraryElement>(); | 69 Set<LibraryElement> librariesToImport = new Set<LibraryElement>(); |
| 70 | 70 |
| 71 String returnType; | 71 String returnType; |
| 72 String name; | 72 String name; |
| 73 bool extractAll = true; | 73 bool extractAll = true; |
| 74 bool canCreateGetter = false; |
| 74 bool createGetter = false; | 75 bool createGetter = false; |
| 75 final List<String> names = <String>[]; | 76 final List<String> names = <String>[]; |
| 76 final List<int> offsets = <int>[]; | 77 final List<int> offsets = <int>[]; |
| 77 final List<int> lengths = <int>[]; | 78 final List<int> lengths = <int>[]; |
| 78 | 79 |
| 79 Set<String> _usedNames = new Set<String>(); | 80 Set<String> _usedNames = new Set<String>(); |
| 80 Set<String> _excludedNames = new Set<String>(); | 81 Set<String> _excludedNames = new Set<String>(); |
| 81 List<RefactoringMethodParameter> _parameters = <RefactoringMethodParameter>[]; | 82 List<RefactoringMethodParameter> _parameters = <RefactoringMethodParameter>[]; |
| 82 Map<String, RefactoringMethodParameter> _parametersMap = | 83 Map<String, RefactoringMethodParameter> _parametersMap = |
| 83 <String, RefactoringMethodParameter>{}; | 84 <String, RefactoringMethodParameter>{}; |
| 84 Map<String, List<SourceRange>> _parameterReferencesMap = | 85 Map<String, List<SourceRange>> _parameterReferencesMap = |
| 85 <String, List<SourceRange>>{}; | 86 <String, List<SourceRange>>{}; |
| 86 DartType _returnType; | 87 DartType _returnType; |
| 87 String _returnVariableName; | 88 String _returnVariableName; |
| 88 AstNode _parentMember; | 89 AstNode _parentMember; |
| 89 Expression _selectionExpression; | 90 Expression _selectionExpression; |
| 90 FunctionExpression _selectionFunctionExpression; | 91 FunctionExpression _selectionFunctionExpression; |
| 91 List<Statement> _selectionStatements; | 92 List<Statement> _selectionStatements; |
| 92 List<_Occurrence> _occurrences = []; | 93 List<_Occurrence> _occurrences = []; |
| 93 bool _staticContext = false; | 94 bool _staticContext = false; |
| 94 | 95 |
| 95 ExtractMethodRefactoringImpl(this.searchEngine, this.unit, | 96 ExtractMethodRefactoringImpl(this.searchEngine, this.unit, |
| 96 this.selectionOffset, this.selectionLength) { | 97 this.selectionOffset, this.selectionLength) { |
| 97 unitElement = unit.element; | 98 unitElement = unit.element; |
| 98 libraryElement = unitElement.library; | 99 libraryElement = unitElement.library; |
| 99 selectionRange = new SourceRange(selectionOffset, selectionLength); | 100 selectionRange = new SourceRange(selectionOffset, selectionLength); |
| 100 utils = new CorrectionUtils(unit); | 101 utils = new CorrectionUtils(unit); |
| 101 } | 102 } |
| 102 | 103 |
| 103 bool get canCreateGetter { | 104 /** |
| 105 * Initializes [canCreateGetter] flag. |
| 106 */ |
| 107 bool _computeCanCreateGetter() { |
| 108 // is a function expression |
| 109 if (_selectionFunctionExpression != null) { |
| 110 return false; |
| 111 } |
| 112 // has parameters |
| 104 if (!parameters.isEmpty) { | 113 if (!parameters.isEmpty) { |
| 105 return false; | 114 return false; |
| 106 } | 115 } |
| 116 // is assignment |
| 107 if (_selectionExpression != null) { | 117 if (_selectionExpression != null) { |
| 108 if (_selectionExpression is AssignmentExpression) { | 118 if (_selectionExpression is AssignmentExpression) { |
| 109 return false; | 119 return false; |
| 110 } | 120 } |
| 111 } | 121 } |
| 122 // doesn't return a value |
| 112 if (_selectionStatements != null) { | 123 if (_selectionStatements != null) { |
| 113 return returnType != 'void'; | 124 return returnType != 'void'; |
| 114 } | 125 } |
| 126 // OK |
| 115 return true; | 127 return true; |
| 116 } | 128 } |
| 117 | 129 |
| 118 @override | 130 @override |
| 119 List<RefactoringMethodParameter> get parameters => _parameters; | 131 List<RefactoringMethodParameter> get parameters => _parameters; |
| 120 | 132 |
| 121 @override | 133 @override |
| 122 void set parameters(List<RefactoringMethodParameter> parameters) { | 134 void set parameters(List<RefactoringMethodParameter> parameters) { |
| 123 _parameters = parameters.toList(); | 135 _parameters = parameters.toList(); |
| 124 } | 136 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 Future<RefactoringStatus> checkInitialConditions() { | 193 Future<RefactoringStatus> checkInitialConditions() { |
| 182 RefactoringStatus result = new RefactoringStatus(); | 194 RefactoringStatus result = new RefactoringStatus(); |
| 183 // selection | 195 // selection |
| 184 result.addStatus(_checkSelection()); | 196 result.addStatus(_checkSelection()); |
| 185 if (result.hasFatalError) { | 197 if (result.hasFatalError) { |
| 186 return new Future.value(result); | 198 return new Future.value(result); |
| 187 } | 199 } |
| 188 // prepare parts | 200 // prepare parts |
| 189 result.addStatus(_initializeParameters()); | 201 result.addStatus(_initializeParameters()); |
| 190 _initializeReturnType(); | 202 _initializeReturnType(); |
| 191 _initializeGetter(); | |
| 192 // occurrences | 203 // occurrences |
| 193 _initializeOccurrences(); | 204 _initializeOccurrences(); |
| 194 _prepareOffsetsLengths(); | 205 _prepareOffsetsLengths(); |
| 206 // getter |
| 207 canCreateGetter = _computeCanCreateGetter(); |
| 208 _initializeCreateGetter(); |
| 195 // names | 209 // names |
| 196 _prepareExcludedNames(); | 210 _prepareExcludedNames(); |
| 197 _prepareNames(); | 211 _prepareNames(); |
| 198 // closure cannot have parameters | 212 // closure cannot have parameters |
| 199 if (_selectionFunctionExpression != null && !_parameters.isEmpty) { | 213 if (_selectionFunctionExpression != null && !_parameters.isEmpty) { |
| 200 String message = format( | 214 String message = format( |
| 201 'Cannot extract closure as method, it references {0} external variable
(s).', | 215 'Cannot extract closure as method, it references {0} external variable
(s).', |
| 202 _parameters.length); | 216 _parameters.length); |
| 203 RefactoringStatus result = new RefactoringStatus.fatal(message); | 217 RefactoringStatus result = new RefactoringStatus.fatal(message); |
| 204 return new Future.value(result); | 218 return new Future.value(result); |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 return pattern; | 518 return pattern; |
| 505 } | 519 } |
| 506 | 520 |
| 507 String _getTypeCode(DartType type) { | 521 String _getTypeCode(DartType type) { |
| 508 return utils.getTypeSource(type, librariesToImport); | 522 return utils.getTypeSource(type, librariesToImport); |
| 509 } | 523 } |
| 510 | 524 |
| 511 /** | 525 /** |
| 512 * Initializes [createGetter] flag. | 526 * Initializes [createGetter] flag. |
| 513 */ | 527 */ |
| 514 void _initializeGetter() { | 528 void _initializeCreateGetter() { |
| 515 createGetter = false; | 529 createGetter = false; |
| 516 // maybe we cannot at all | 530 // maybe we cannot at all |
| 517 if (!canCreateGetter) { | 531 if (!canCreateGetter) { |
| 518 return; | 532 return; |
| 519 } | 533 } |
| 520 // OK, just expression | 534 // OK, just expression |
| 521 if (_selectionExpression != null) { | 535 if (_selectionExpression != null) { |
| 522 createGetter = !_hasMethodInvocation(_selectionExpression); | 536 createGetter = !_hasMethodInvocation(_selectionExpression); |
| 523 return; | 537 return; |
| 524 } | 538 } |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1142 return false; | 1156 return false; |
| 1143 } | 1157 } |
| 1144 for (int i = 0; i < parameterTypes.length; i++) { | 1158 for (int i = 0; i < parameterTypes.length; i++) { |
| 1145 if (other.parameterTypes[i] != parameterTypes[i]) { | 1159 if (other.parameterTypes[i] != parameterTypes[i]) { |
| 1146 return false; | 1160 return false; |
| 1147 } | 1161 } |
| 1148 } | 1162 } |
| 1149 return true; | 1163 return true; |
| 1150 } | 1164 } |
| 1151 } | 1165 } |
| OLD | NEW |