Chromium Code Reviews| 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_local; | 5 library services.src.refactoring.extract_local; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'package:analysis_server/src/protocol_server.dart' hide Element; | 10 import 'package:analysis_server/src/protocol_server.dart' hide Element; |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 int numTrailing = countTrailingWhitespaces(selectionStr); | 223 int numTrailing = countTrailingWhitespaces(selectionStr); |
| 224 int offset = selectionRange.offset + numLeading; | 224 int offset = selectionRange.offset + numLeading; |
| 225 int end = selectionRange.end - numTrailing; | 225 int end = selectionRange.end - numTrailing; |
| 226 selectionRange = new SourceRange(offset, end - offset); | 226 selectionRange = new SourceRange(offset, end - offset); |
| 227 } | 227 } |
| 228 // get covering node | 228 // get covering node |
| 229 AstNode coveringNode = | 229 AstNode coveringNode = |
| 230 new NodeLocator(selectionRange.offset, selectionRange.end) | 230 new NodeLocator(selectionRange.offset, selectionRange.end) |
| 231 .searchWithin(unit); | 231 .searchWithin(unit); |
| 232 // compute covering expressions | 232 // compute covering expressions |
| 233 for (AstNode node = coveringNode; | 233 for (AstNode node = coveringNode;; node = node.parent) { |
|
Brian Wilkerson
2015/12/27 16:49:42
Perhaps replace the condition with "node != null"?
| |
| 234 node is Expression || node is ArgumentList; | |
| 235 node = node.parent) { | |
| 236 AstNode parent = node.parent; | 234 AstNode parent = node.parent; |
| 235 // skip some nodes | |
| 236 if (node is ArgumentList || | |
| 237 node is AssignmentExpression || | |
| 238 node is NamedExpression || | |
| 239 node is TypeArgumentList) { | |
| 240 continue; | |
| 241 } | |
| 242 if (node is ConstructorName || node is Label || node is TypeName) { | |
| 243 rootExpression = null; | |
| 244 coveringExpressionOffsets.clear(); | |
| 245 coveringExpressionLengths.clear(); | |
| 246 continue; | |
| 247 } | |
| 248 // cannot extract the name part of a property access | |
| 249 if (parent is PrefixedIdentifier && parent.identifier == node || | |
| 250 parent is PropertyAccess && parent.propertyName == node) { | |
| 251 continue; | |
| 252 } | |
| 253 // stop if not an Expression | |
| 254 if (node is! Expression) { | |
| 255 break; | |
| 256 } | |
| 237 // stop at void method invocations | 257 // stop at void method invocations |
| 238 if (node is MethodInvocation) { | 258 if (node is MethodInvocation) { |
| 239 MethodInvocation invocation = node; | 259 MethodInvocation invocation = node; |
| 240 Element element = invocation.methodName.bestElement; | 260 Element element = invocation.methodName.bestElement; |
| 241 if (element is ExecutableElement && | 261 if (element is ExecutableElement && |
| 242 element.returnType != null && | 262 element.returnType != null && |
| 243 element.returnType.isVoid) { | 263 element.returnType.isVoid) { |
| 244 if (rootExpression == null) { | 264 if (rootExpression == null) { |
| 245 return new RefactoringStatus.fatal( | 265 return new RefactoringStatus.fatal( |
| 246 'Cannot extract the void expression.', | 266 'Cannot extract the void expression.', |
| 247 newLocation_fromNode(node)); | 267 newLocation_fromNode(node)); |
| 248 } | 268 } |
| 249 break; | 269 break; |
| 250 } | 270 } |
| 251 } | 271 } |
| 252 // skip ArgumentList | |
| 253 if (node is ArgumentList) { | |
| 254 continue; | |
| 255 } | |
| 256 // skip AssignmentExpression | |
| 257 if (node is AssignmentExpression) { | |
| 258 continue; | |
| 259 } | |
| 260 // cannot extract the name part of a property access | |
| 261 if (parent is PrefixedIdentifier && parent.identifier == node || | |
| 262 parent is PropertyAccess && parent.propertyName == node) { | |
| 263 continue; | |
| 264 } | |
| 265 // fatal selection problems | 272 // fatal selection problems |
| 266 if (coveringExpressionOffsets.isEmpty) { | 273 if (coveringExpressionOffsets.isEmpty) { |
| 267 if (node is SimpleIdentifier) { | 274 if (node is SimpleIdentifier) { |
| 268 if (node.inDeclarationContext()) { | 275 if (node.inDeclarationContext()) { |
| 269 return new RefactoringStatus.fatal( | 276 return new RefactoringStatus.fatal( |
| 270 'Cannot extract the name part of a declaration.', | 277 'Cannot extract the name part of a declaration.', |
| 271 newLocation_fromNode(node)); | 278 newLocation_fromNode(node)); |
| 272 } | 279 } |
| 273 Element element = node.bestElement; | 280 Element element = node.bestElement; |
| 274 if (element is FunctionElement || element is MethodElement) { | 281 if (element is FunctionElement || element is MethodElement) { |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 | 685 |
| 679 _TokenLocalElementVisitor(this.map); | 686 _TokenLocalElementVisitor(this.map); |
| 680 | 687 |
| 681 visitSimpleIdentifier(SimpleIdentifier node) { | 688 visitSimpleIdentifier(SimpleIdentifier node) { |
| 682 Element element = node.staticElement; | 689 Element element = node.staticElement; |
| 683 if (element is LocalVariableElement) { | 690 if (element is LocalVariableElement) { |
| 684 map[node.token] = element; | 691 map[node.token] = element; |
| 685 } | 692 } |
| 686 } | 693 } |
| 687 } | 694 } |
| OLD | NEW |