| 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_local; | 5 library services.src.refactoring.inline_local; |
| 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/source_range.dart'; | 10 import 'package:analysis_server/src/services/correction/source_range.dart'; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 { | 72 { |
| 73 AstNode offsetNode = new NodeLocator.con1(offset).searchWithin(unit); | 73 AstNode offsetNode = new NodeLocator.con1(offset).searchWithin(unit); |
| 74 if (offsetNode is SimpleIdentifier) { | 74 if (offsetNode is SimpleIdentifier) { |
| 75 Element element = offsetNode.staticElement; | 75 Element element = offsetNode.staticElement; |
| 76 if (element is LocalVariableElement) { | 76 if (element is LocalVariableElement) { |
| 77 _variableElement = element; | 77 _variableElement = element; |
| 78 _variableNode = element.node; | 78 _variableNode = element.node; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 if (_variableNode == null) { | 82 // validate node declaration |
| 83 if (!_isVariableDeclaredInStatement()) { |
| 83 result = new RefactoringStatus.fatal( | 84 result = new RefactoringStatus.fatal( |
| 84 'Local variable declaration or reference must be selected to activate
this refactoring.'); | 85 'Local variable declaration or reference must be selected ' |
| 85 return new Future.value(result); | 86 'to activate this refactoring.'); |
| 86 } | |
| 87 // should be normal variable declaration statement | |
| 88 if (_variableNode.parent is! VariableDeclarationList || | |
| 89 _variableNode.parent.parent is! VariableDeclarationStatement || | |
| 90 _variableNode.parent.parent.parent is! Block) { | |
| 91 result = new RefactoringStatus.fatal( | |
| 92 'Local variable declared in ' | |
| 93 'statement should be selected to activate this refactoring.'); | |
| 94 return new Future.value(result); | 87 return new Future.value(result); |
| 95 } | 88 } |
| 96 // should have initializer at declaration | 89 // should have initializer at declaration |
| 97 if (_variableNode.initializer == null) { | 90 if (_variableNode.initializer == null) { |
| 98 String message = format( | 91 String message = format( |
| 99 "Local variable '{0}' is not initialized at declaration.", | 92 "Local variable '{0}' is not initialized at declaration.", |
| 100 _variableElement.displayName); | 93 _variableElement.displayName); |
| 101 result = | 94 result = |
| 102 new RefactoringStatus.fatal(message, newLocation_fromNode(_variableNod
e)); | 95 new RefactoringStatus.fatal(message, newLocation_fromNode(_variableNod
e)); |
| 103 return new Future.value(result); | 96 return new Future.value(result); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 unitElement, | 174 unitElement, |
| 182 newSourceEdit_range(range, codeForReference)); | 175 newSourceEdit_range(range, codeForReference)); |
| 183 } | 176 } |
| 184 // done | 177 // done |
| 185 return new Future.value(change); | 178 return new Future.value(change); |
| 186 } | 179 } |
| 187 | 180 |
| 188 @override | 181 @override |
| 189 bool requiresPreview() => false; | 182 bool requiresPreview() => false; |
| 190 | 183 |
| 184 bool _isVariableDeclaredInStatement() { |
| 185 if (_variableNode == null) { |
| 186 return false; |
| 187 } |
| 188 AstNode parent = _variableNode.parent; |
| 189 if (parent is VariableDeclarationList) { |
| 190 parent = parent.parent; |
| 191 if (parent is VariableDeclarationStatement) { |
| 192 parent = parent.parent; |
| 193 return parent is Block || parent is SwitchCase; |
| 194 } |
| 195 } |
| 196 return false; |
| 197 } |
| 198 |
| 191 static bool _shouldBeExpressionInterpolation(InterpolationExpression target, | 199 static bool _shouldBeExpressionInterpolation(InterpolationExpression target, |
| 192 Expression expression) { | 200 Expression expression) { |
| 193 TokenType targetType = target.beginToken.type; | 201 TokenType targetType = target.beginToken.type; |
| 194 return targetType == TokenType.STRING_INTERPOLATION_IDENTIFIER && | 202 return targetType == TokenType.STRING_INTERPOLATION_IDENTIFIER && |
| 195 expression is! SimpleIdentifier; | 203 expression is! SimpleIdentifier; |
| 196 } | 204 } |
| 197 | 205 |
| 198 static bool _shouldUseParenthesis(Expression init, AstNode node) { | 206 static bool _shouldUseParenthesis(Expression init, AstNode node) { |
| 199 // check precedence | 207 // check precedence |
| 200 int initPrecedence = getExpressionPrecedence(init); | 208 int initPrecedence = getExpressionPrecedence(init); |
| 201 if (initPrecedence < getExpressionParentPrecedence(node)) { | 209 if (initPrecedence < getExpressionParentPrecedence(node)) { |
| 202 return true; | 210 return true; |
| 203 } | 211 } |
| 204 // special case for '-' | 212 // special case for '-' |
| 205 AstNode parent = node.parent; | 213 AstNode parent = node.parent; |
| 206 if (init is PrefixExpression && parent is PrefixExpression) { | 214 if (init is PrefixExpression && parent is PrefixExpression) { |
| 207 if (parent.operator.type == TokenType.MINUS) { | 215 if (parent.operator.type == TokenType.MINUS) { |
| 208 TokenType initializerOperator = init.operator.type; | 216 TokenType initializerOperator = init.operator.type; |
| 209 if (initializerOperator == TokenType.MINUS || | 217 if (initializerOperator == TokenType.MINUS || |
| 210 initializerOperator == TokenType.MINUS_MINUS) { | 218 initializerOperator == TokenType.MINUS_MINUS) { |
| 211 return true; | 219 return true; |
| 212 } | 220 } |
| 213 } | 221 } |
| 214 } | 222 } |
| 215 // no () is needed | 223 // no () is needed |
| 216 return false; | 224 return false; |
| 217 } | 225 } |
| 218 } | 226 } |
| OLD | NEW |