| 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.inline_method; | 5 library services.src.refactoring.inline_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/source_range.dart'; | 10 import 'package:analysis_server/src/services/correction/source_range.dart'; |
| 11 import 'package:analysis_server/src/services/correction/status.dart'; | 11 import 'package:analysis_server/src/services/correction/status.dart'; |
| 12 import 'package:analysis_server/src/services/correction/util.dart'; | 12 import 'package:analysis_server/src/services/correction/util.dart'; |
| 13 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; | 13 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; |
| 14 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; | 14 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da
rt'; |
| 15 import 'package:analysis_server/src/services/search/element_visitors.dart'; | 15 import 'package:analysis_server/src/services/search/element_visitors.dart'; |
| 16 import 'package:analysis_server/src/services/search/hierarchy.dart'; | 16 import 'package:analysis_server/src/services/search/hierarchy.dart'; |
| 17 import 'package:analysis_server/src/services/search/search_engine.dart'; | 17 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 18 import 'package:analyzer/src/generated/ast.dart'; | 18 import 'package:analyzer/src/generated/ast.dart'; |
| 19 import 'package:analyzer/src/generated/element.dart'; | 19 import 'package:analyzer/src/generated/element.dart'; |
| 20 import 'package:analyzer/src/generated/scanner.dart'; | |
| 21 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 22 | 21 |
| 23 | 22 |
| 24 /** | 23 /** |
| 25 * Resolver sets [ParameterElement] for the most cases, but not for setter invoc
ation. | |
| 26 * | |
| 27 * Returns the best available [ParameterElement] for which the given [Expression
] is used. | |
| 28 */ | |
| 29 ParameterElement _getBestParameterElement(Expression expr) { | |
| 30 // TODO(scheglov) investigate why AssignmentExpression "f = 0" doesn't have | |
| 31 // an element. | |
| 32 // setter invocation | |
| 33 if (expr.parent is AssignmentExpression) { | |
| 34 AssignmentExpression assignment = expr.parent as AssignmentExpression; | |
| 35 if (assignment.rightHandSide == expr && | |
| 36 assignment.operator.type == TokenType.EQ) { | |
| 37 Expression lhs = assignment.leftHandSide; | |
| 38 Element lhsElement = null; | |
| 39 if (lhs is Identifier) { | |
| 40 lhsElement = lhs.bestElement; | |
| 41 } | |
| 42 if (lhs is PropertyAccess) { | |
| 43 lhsElement = lhs.propertyName.bestElement; | |
| 44 } | |
| 45 if (lhsElement is PropertyAccessorElement) { | |
| 46 List<ParameterElement> parameters = lhsElement.parameters; | |
| 47 if (parameters.length != 0) { | |
| 48 return parameters[0]; | |
| 49 } | |
| 50 } | |
| 51 } | |
| 52 } | |
| 53 // use resolver | |
| 54 return expr.bestParameterElement; | |
| 55 } | |
| 56 | |
| 57 | |
| 58 /** | |
| 59 * Returns the [SourceRange] to find conflicting locals in. | 24 * Returns the [SourceRange] to find conflicting locals in. |
| 60 */ | 25 */ |
| 61 SourceRange _getLocalsConflictingRange(AstNode node) { | 26 SourceRange _getLocalsConflictingRange(AstNode node) { |
| 62 // maybe Block | 27 // maybe Block |
| 63 Block block = node.getAncestor((node) => node is Block); | 28 Block block = node.getAncestor((node) => node is Block); |
| 64 if (block != null) { | 29 if (block != null) { |
| 65 int offset = node.offset; | 30 int offset = node.offset; |
| 66 int endOffset = block.end; | 31 int endOffset = block.end; |
| 67 return rangeStartEnd(offset, endOffset); | 32 return rangeStartEnd(offset, endOffset); |
| 68 } | 33 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 82 */ | 47 */ |
| 83 String _getMethodSourceForInvocation(_SourcePart part, CorrectionUtils utils, | 48 String _getMethodSourceForInvocation(_SourcePart part, CorrectionUtils utils, |
| 84 AstNode contextNode, Expression targetExpression, List<Expression> arguments
) { | 49 AstNode contextNode, Expression targetExpression, List<Expression> arguments
) { |
| 85 // prepare edits to replace parameters with arguments | 50 // prepare edits to replace parameters with arguments |
| 86 List<SourceEdit> edits = <SourceEdit>[]; | 51 List<SourceEdit> edits = <SourceEdit>[]; |
| 87 part._parameters.forEach( | 52 part._parameters.forEach( |
| 88 (ParameterElement parameter, List<_ParameterOccurrence> occurrences) { | 53 (ParameterElement parameter, List<_ParameterOccurrence> occurrences) { |
| 89 // prepare argument | 54 // prepare argument |
| 90 Expression argument = null; | 55 Expression argument = null; |
| 91 for (Expression arg in arguments) { | 56 for (Expression arg in arguments) { |
| 92 if (_getBestParameterElement(arg) == parameter) { | 57 if (arg.bestParameterElement == parameter) { |
| 93 argument = arg; | 58 argument = arg; |
| 94 break; | 59 break; |
| 95 } | 60 } |
| 96 } | 61 } |
| 97 if (argument is NamedExpression) { | 62 if (argument is NamedExpression) { |
| 98 argument = (argument as NamedExpression).expression; | 63 argument = (argument as NamedExpression).expression; |
| 99 } | 64 } |
| 100 int argumentPrecedence = getExpressionPrecedence(argument); | 65 int argumentPrecedence = getExpressionPrecedence(argument); |
| 101 String argumentSource = utils.getNodeText(argument); | 66 String argumentSource = utils.getNodeText(argument); |
| 102 // replace all occurrences of this parameter | 67 // replace all occurrences of this parameter |
| (...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 805 } | 770 } |
| 806 | 771 |
| 807 void _addVariable(SimpleIdentifier node) { | 772 void _addVariable(SimpleIdentifier node) { |
| 808 VariableElement variableElement = getLocalVariableElement(node); | 773 VariableElement variableElement = getLocalVariableElement(node); |
| 809 if (variableElement != null) { | 774 if (variableElement != null) { |
| 810 SourceRange nodeRange = rangeNode(node); | 775 SourceRange nodeRange = rangeNode(node); |
| 811 result.addVariable(variableElement, nodeRange); | 776 result.addVariable(variableElement, nodeRange); |
| 812 } | 777 } |
| 813 } | 778 } |
| 814 } | 779 } |
| OLD | NEW |