| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 final List<String> names = <String>[]; | 44 final List<String> names = <String>[]; |
| 45 final List<int> offsets = <int>[]; | 45 final List<int> offsets = <int>[]; |
| 46 final List<int> lengths = <int>[]; | 46 final List<int> lengths = <int>[]; |
| 47 | 47 |
| 48 Expression rootExpression; | 48 Expression rootExpression; |
| 49 Expression singleExpression; | 49 Expression singleExpression; |
| 50 bool wholeStatementExpression = false; | 50 bool wholeStatementExpression = false; |
| 51 String stringLiteralPart; | 51 String stringLiteralPart; |
| 52 final List<SourceRange> occurrences = <SourceRange>[]; | 52 final List<SourceRange> occurrences = <SourceRange>[]; |
| 53 final Map<Element, int> elementIds = <Element, int>{}; | 53 final Map<Element, int> elementIds = <Element, int>{}; |
| 54 final Set<String> excludedVariableNames = new Set<String>(); | 54 Set<String> excludedVariableNames = new Set<String>(); |
| 55 | 55 |
| 56 ExtractLocalRefactoringImpl( | 56 ExtractLocalRefactoringImpl( |
| 57 this.unit, this.selectionOffset, this.selectionLength) { | 57 this.unit, this.selectionOffset, this.selectionLength) { |
| 58 unitElement = unit.element; | 58 unitElement = unit.element; |
| 59 selectionRange = new SourceRange(selectionOffset, selectionLength); | 59 selectionRange = new SourceRange(selectionOffset, selectionLength); |
| 60 utils = new CorrectionUtils(unit); | 60 utils = new CorrectionUtils(unit); |
| 61 } | 61 } |
| 62 | 62 |
| 63 @override | 63 @override |
| 64 String get refactoringName => 'Extract Local Variable'; | 64 String get refactoringName => 'Extract Local Variable'; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 87 RefactoringStatus result = new RefactoringStatus(); | 87 RefactoringStatus result = new RefactoringStatus(); |
| 88 // selection | 88 // selection |
| 89 result.addStatus(_checkSelection()); | 89 result.addStatus(_checkSelection()); |
| 90 if (result.hasFatalError) { | 90 if (result.hasFatalError) { |
| 91 return new Future.value(result); | 91 return new Future.value(result); |
| 92 } | 92 } |
| 93 // occurrences | 93 // occurrences |
| 94 _prepareOccurrences(); | 94 _prepareOccurrences(); |
| 95 _prepareOffsetsLengths(); | 95 _prepareOffsetsLengths(); |
| 96 // names | 96 // names |
| 97 _prepareExcludedNames(); | 97 excludedVariableNames = |
| 98 utils.findPossibleLocalVariableConflicts(selectionOffset); |
| 98 _prepareNames(); | 99 _prepareNames(); |
| 99 // done | 100 // done |
| 100 return new Future.value(result); | 101 return new Future.value(result); |
| 101 } | 102 } |
| 102 | 103 |
| 103 @override | 104 @override |
| 104 RefactoringStatus checkName() { | 105 RefactoringStatus checkName() { |
| 105 return validateVariableName(name); | 106 return validateVariableName(name); |
| 106 } | 107 } |
| 107 | 108 |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 node is BinaryExpression || | 365 node is BinaryExpression || |
| 365 node is ParenthesizedExpression || | 366 node is ParenthesizedExpression || |
| 366 node is PrefixExpression || | 367 node is PrefixExpression || |
| 367 node is Literal || | 368 node is Literal || |
| 368 node is MapLiteralEntry) { | 369 node is MapLiteralEntry) { |
| 369 return _isPartOfConstantExpression(node.parent); | 370 return _isPartOfConstantExpression(node.parent); |
| 370 } | 371 } |
| 371 return false; | 372 return false; |
| 372 } | 373 } |
| 373 | 374 |
| 374 void _prepareExcludedNames() { | |
| 375 excludedVariableNames.clear(); | |
| 376 AstNode enclosingNode = | |
| 377 new NodeLocator.con1(selectionOffset).searchWithin(unit); | |
| 378 Block enclosingBlock = enclosingNode.getAncestor((node) => node is Block); | |
| 379 if (enclosingBlock != null) { | |
| 380 SourceRange newVariableVisibleRange = | |
| 381 rangeStartEnd(selectionRange, enclosingBlock.end); | |
| 382 ExecutableElement enclosingExecutable = | |
| 383 getEnclosingExecutableElement(enclosingNode); | |
| 384 if (enclosingExecutable != null) { | |
| 385 visitChildren(enclosingExecutable, (Element element) { | |
| 386 if (element is LocalElement) { | |
| 387 SourceRange elementRange = element.visibleRange; | |
| 388 if (elementRange != null && | |
| 389 elementRange.intersects(newVariableVisibleRange)) { | |
| 390 excludedVariableNames.add(element.displayName); | |
| 391 } | |
| 392 } | |
| 393 return true; | |
| 394 }); | |
| 395 } | |
| 396 } | |
| 397 } | |
| 398 | |
| 399 void _prepareNames() { | 375 void _prepareNames() { |
| 400 names.clear(); | 376 names.clear(); |
| 401 if (stringLiteralPart != null) { | 377 if (stringLiteralPart != null) { |
| 402 names.addAll(getVariableNameSuggestionsForText( | 378 names.addAll(getVariableNameSuggestionsForText( |
| 403 stringLiteralPart, excludedVariableNames)); | 379 stringLiteralPart, excludedVariableNames)); |
| 404 } else if (singleExpression != null) { | 380 } else if (singleExpression != null) { |
| 405 names.addAll(getVariableNameSuggestionsForExpression( | 381 names.addAll(getVariableNameSuggestionsForExpression( |
| 406 singleExpression.staticType, singleExpression, | 382 singleExpression.staticType, singleExpression, |
| 407 excludedVariableNames)); | 383 excludedVariableNames)); |
| 408 } | 384 } |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 | 598 |
| 623 _TokenLocalElementVisitor(this.map); | 599 _TokenLocalElementVisitor(this.map); |
| 624 | 600 |
| 625 visitSimpleIdentifier(SimpleIdentifier node) { | 601 visitSimpleIdentifier(SimpleIdentifier node) { |
| 626 Element element = node.staticElement; | 602 Element element = node.staticElement; |
| 627 if (element is LocalVariableElement) { | 603 if (element is LocalVariableElement) { |
| 628 map[node.token] = element; | 604 map[node.token] = element; |
| 629 } | 605 } |
| 630 } | 606 } |
| 631 } | 607 } |
| OLD | NEW |