| 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.dart' hide Element; | 9 import 'package:analysis_server/src/protocol.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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 bool get canCreateGetter { | 99 bool get canCreateGetter { |
| 100 if (!parameters.isEmpty) { | 100 if (!parameters.isEmpty) { |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 if (_selectionExpression != null) { | 103 if (_selectionExpression != null) { |
| 104 if (_selectionExpression is AssignmentExpression) { | 104 if (_selectionExpression is AssignmentExpression) { |
| 105 return false; | 105 return false; |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 if (_selectionStatements != null) { | 108 if (_selectionStatements != null) { |
| 109 return returnType != null; | 109 return returnType != 'void'; |
| 110 } | 110 } |
| 111 return true; | 111 return true; |
| 112 } | 112 } |
| 113 | 113 |
| 114 @override | 114 @override |
| 115 List<RefactoringMethodParameter> get parameters => _parameters; | 115 List<RefactoringMethodParameter> get parameters => _parameters; |
| 116 | 116 |
| 117 @override | 117 @override |
| 118 void set parameters(List<RefactoringMethodParameter> parameters) { | 118 void set parameters(List<RefactoringMethodParameter> parameters) { |
| 119 _parameters = parameters.toList(); | 119 _parameters = parameters.toList(); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 if (!extractAll && !occurence.isSelection) { | 219 if (!extractAll && !occurence.isSelection) { |
| 220 continue; | 220 continue; |
| 221 } | 221 } |
| 222 // prepare invocation source | 222 // prepare invocation source |
| 223 String invocationSource; | 223 String invocationSource; |
| 224 if (_selectionFunctionExpression != null) { | 224 if (_selectionFunctionExpression != null) { |
| 225 invocationSource = name; | 225 invocationSource = name; |
| 226 } else { | 226 } else { |
| 227 StringBuffer sb = new StringBuffer(); | 227 StringBuffer sb = new StringBuffer(); |
| 228 // may be returns value | 228 // may be returns value |
| 229 if (returnType != null) { | 229 if (_selectionStatements != null && returnType != 'void') { |
| 230 // single variable assignment / return statement | 230 // single variable assignment / return statement |
| 231 if (_returnVariableName != null) { | 231 if (_returnVariableName != null) { |
| 232 String occurrenceName = | 232 String occurrenceName = |
| 233 occurence._parameterOldToOccurrenceName[_returnVariableName]; | 233 occurence._parameterOldToOccurrenceName[_returnVariableName]; |
| 234 // may be declare variable | 234 // may be declare variable |
| 235 if (!_parametersMap.containsKey(_returnVariableName)) { | 235 if (!_parametersMap.containsKey(_returnVariableName)) { |
| 236 if (returnType.isEmpty) { | 236 if (returnType.isEmpty) { |
| 237 sb.write('var '); | 237 sb.write('var '); |
| 238 } else { | 238 } else { |
| 239 sb.write(returnType); | 239 sb.write(returnType); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 utils.getExpressionTypeSource(_selectionExpression); | 309 utils.getExpressionTypeSource(_selectionExpression); |
| 310 if (returnTypeName != null && returnTypeName != 'dynamic') { | 310 if (returnTypeName != null && returnTypeName != 'dynamic') { |
| 311 annotations += '${returnTypeName} '; | 311 annotations += '${returnTypeName} '; |
| 312 } | 312 } |
| 313 // just return expression | 313 // just return expression |
| 314 declarationSource = | 314 declarationSource = |
| 315 '${annotations}${signature} => ${returnExpressionSource};'; | 315 '${annotations}${signature} => ${returnExpressionSource};'; |
| 316 } | 316 } |
| 317 // statements | 317 // statements |
| 318 if (_selectionStatements != null) { | 318 if (_selectionStatements != null) { |
| 319 if (returnType != null) { | 319 if (returnType.isNotEmpty) { |
| 320 if (returnType.isNotEmpty) { | 320 annotations += returnType + ' '; |
| 321 annotations += returnType + ' '; | |
| 322 } | |
| 323 } else { | |
| 324 annotations += 'void '; | |
| 325 } | 321 } |
| 326 declarationSource = '${annotations}${signature} {${eol}'; | 322 declarationSource = '${annotations}${signature} {${eol}'; |
| 327 declarationSource += returnExpressionSource; | 323 declarationSource += returnExpressionSource; |
| 328 if (_returnVariableName != null) { | 324 if (_returnVariableName != null) { |
| 329 declarationSource += | 325 declarationSource += |
| 330 '${prefix} return ${_returnVariableName};$eol'; | 326 '${prefix} return ${_returnVariableName};$eol'; |
| 331 } | 327 } |
| 332 declarationSource += '${prefix}}'; | 328 declarationSource += '${prefix}}'; |
| 333 } | 329 } |
| 334 } | 330 } |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 * Prepares information about used variables, which should be turned into | 561 * Prepares information about used variables, which should be turned into |
| 566 * parameters. | 562 * parameters. |
| 567 */ | 563 */ |
| 568 RefactoringStatus _initializeParameters() { | 564 RefactoringStatus _initializeParameters() { |
| 569 _parameters.clear(); | 565 _parameters.clear(); |
| 570 _parametersMap.clear(); | 566 _parametersMap.clear(); |
| 571 _parameterReferencesMap.clear(); | 567 _parameterReferencesMap.clear(); |
| 572 RefactoringStatus result = new RefactoringStatus(); | 568 RefactoringStatus result = new RefactoringStatus(); |
| 573 List<VariableElement> assignedUsedVariables = []; | 569 List<VariableElement> assignedUsedVariables = []; |
| 574 unit.accept(new _InitializeParametersVisitor(this, assignedUsedVariables)); | 570 unit.accept(new _InitializeParametersVisitor(this, assignedUsedVariables)); |
| 571 // single expression |
| 572 if (_selectionExpression != null) { |
| 573 _returnType = _selectionExpression.bestType; |
| 574 } |
| 575 // may be ends with "return" statement | 575 // may be ends with "return" statement |
| 576 if (_selectionStatements != null) { | 576 if (_selectionStatements != null) { |
| 577 Statement lastStatement = | 577 Statement lastStatement = |
| 578 _selectionStatements[_selectionStatements.length - 1]; | 578 _selectionStatements[_selectionStatements.length - 1]; |
| 579 if (lastStatement is ReturnStatement) { | 579 if (lastStatement is ReturnStatement) { |
| 580 Expression expression = lastStatement.expression; | 580 Expression expression = lastStatement.expression; |
| 581 if (expression != null) { | 581 if (expression != null) { |
| 582 _returnType = expression.bestType; | 582 _returnType = expression.bestType; |
| 583 } | 583 } |
| 584 } | 584 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 609 'Ambiguous return value: Selected block contains more than one ' | 609 'Ambiguous return value: Selected block contains more than one ' |
| 610 'assignment to local variables. Affected variables are:\n\n{0}
', | 610 'assignment to local variables. Affected variables are:\n\n{0}
', |
| 611 sb.toString().trim())); | 611 sb.toString().trim())); |
| 612 } | 612 } |
| 613 // done | 613 // done |
| 614 return result; | 614 return result; |
| 615 } | 615 } |
| 616 | 616 |
| 617 void _initializeReturnType() { | 617 void _initializeReturnType() { |
| 618 if (_returnType == null) { | 618 if (_returnType == null) { |
| 619 returnType = null; | 619 returnType = 'void'; |
| 620 } else { | 620 } else { |
| 621 returnType = utils.getTypeSource(_returnType); | 621 returnType = utils.getTypeSource(_returnType); |
| 622 if (returnType == 'dynamic') { | 622 } |
| 623 returnType = ''; | 623 if (returnType == 'dynamic') { |
| 624 } | 624 returnType = ''; |
| 625 } | 625 } |
| 626 } | 626 } |
| 627 | 627 |
| 628 /** | 628 /** |
| 629 * Checks if the given [VariableElement] is declared in [selectionRange]. | 629 * Checks if the given [VariableElement] is declared in [selectionRange]. |
| 630 */ | 630 */ |
| 631 bool _isDeclaredInSelection(VariableElement element) { | 631 bool _isDeclaredInSelection(VariableElement element) { |
| 632 return selectionRange.contains(element.nameOffset); | 632 return selectionRange.contains(element.nameOffset); |
| 633 } | 633 } |
| 634 | 634 |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1098 | 1098 |
| 1099 /** | 1099 /** |
| 1100 * Generalized version of some source, in which references to the specific | 1100 * Generalized version of some source, in which references to the specific |
| 1101 * variables are replaced with pattern variables, with back mapping from the | 1101 * variables are replaced with pattern variables, with back mapping from the |
| 1102 * pattern to the original variable names. | 1102 * pattern to the original variable names. |
| 1103 */ | 1103 */ |
| 1104 class _SourcePattern { | 1104 class _SourcePattern { |
| 1105 String patternSource; | 1105 String patternSource; |
| 1106 Map<String, String> originalToPatternNames = {}; | 1106 Map<String, String> originalToPatternNames = {}; |
| 1107 } | 1107 } |
| OLD | NEW |