| 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_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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 if (argumentPrecedence < occurrence.parentPrecedence) { | 94 if (argumentPrecedence < occurrence.parentPrecedence) { |
| 95 occurrenceArgumentSource = "($argumentSource)"; | 95 occurrenceArgumentSource = "($argumentSource)"; |
| 96 } else { | 96 } else { |
| 97 occurrenceArgumentSource = argumentSource; | 97 occurrenceArgumentSource = argumentSource; |
| 98 } | 98 } |
| 99 // do replace | 99 // do replace |
| 100 edits.add(newSourceEdit_range(range, occurrenceArgumentSource)); | 100 edits.add(newSourceEdit_range(range, occurrenceArgumentSource)); |
| 101 } | 101 } |
| 102 }); | 102 }); |
| 103 // replace static field "qualifier" with invocation target | 103 // replace static field "qualifier" with invocation target |
| 104 part._staticFieldQualifiers.forEach( | 104 part._staticFieldOffsets.forEach((String className, List<int> offsets) { |
| 105 (String className, List<SourceRange> ranges) { | 105 for (int offset in offsets) { |
| 106 for (SourceRange range in ranges) { | 106 // edits.add(newSourceEdit_range(range, className + '.')); |
| 107 edits.add(newSourceEdit_range(range, className + '.')); | 107 edits.add(new SourceEdit(offset, 0, className + '.')); |
| 108 } | 108 } |
| 109 }); | 109 }); |
| 110 // replace instance field "qualifier" with invocation target | 110 // replace "this" references with invocation target |
| 111 if (targetExpression != null) { | 111 if (targetExpression != null) { |
| 112 String targetSource = utils.getNodeText(targetExpression) + '.'; | 112 String targetSource = utils.getNodeText(targetExpression); |
| 113 for (SourceRange qualifierRange in part._instanceFieldQualifiers) { | 113 // explicit "this" references |
| 114 edits.add(newSourceEdit_range(qualifierRange, targetSource)); | 114 for (int offset in part._explicitThisOffsets) { |
| 115 edits.add(new SourceEdit(offset, 4, targetSource)); |
| 116 } |
| 117 // implicit "this" references |
| 118 targetSource += '.'; |
| 119 for (int offset in part._implicitThisOffsets) { |
| 120 edits.add(new SourceEdit(offset, 0, targetSource)); |
| 115 } | 121 } |
| 116 } | 122 } |
| 117 // prepare edits to replace conflicting variables | 123 // prepare edits to replace conflicting variables |
| 118 Set<String> conflictingNames = _getNamesConflictingAt(contextNode); | 124 Set<String> conflictingNames = _getNamesConflictingAt(contextNode); |
| 119 part._variables.forEach((VariableElement variable, List<SourceRange> ranges) { | 125 part._variables.forEach((VariableElement variable, List<SourceRange> ranges) { |
| 120 String originalName = variable.displayName; | 126 String originalName = variable.displayName; |
| 121 // prepare unique name | 127 // prepare unique name |
| 122 String uniqueName; | 128 String uniqueName; |
| 123 { | 129 { |
| 124 uniqueName = originalName; | 130 uniqueName = originalName; |
| (...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 final String _source; | 679 final String _source; |
| 674 | 680 |
| 675 /** | 681 /** |
| 676 * The original prefix of the method. | 682 * The original prefix of the method. |
| 677 */ | 683 */ |
| 678 final String _prefix; | 684 final String _prefix; |
| 679 | 685 |
| 680 /** | 686 /** |
| 681 * The occurrences of the method parameters. | 687 * The occurrences of the method parameters. |
| 682 */ | 688 */ |
| 683 Map<ParameterElement, List<_ParameterOccurrence>> _parameters = {}; | 689 final Map<ParameterElement, List<_ParameterOccurrence>> _parameters = {}; |
| 684 | 690 |
| 685 /** | 691 /** |
| 686 * The occurrences of the method local variables. | 692 * The occurrences of the method local variables. |
| 687 */ | 693 */ |
| 688 Map<VariableElement, List<SourceRange>> _variables = {}; | 694 final Map<VariableElement, List<SourceRange>> _variables = {}; |
| 689 | 695 |
| 690 /** | 696 /** |
| 691 * The source ranges of the qualifiers in instance field references. | 697 * The offsets of explicit `this` expression references. |
| 692 * Some of them have length `0`. | |
| 693 */ | 698 */ |
| 694 List<SourceRange> _instanceFieldQualifiers = []; | 699 final List<int> _explicitThisOffsets = []; |
| 695 | 700 |
| 696 /** | 701 /** |
| 697 * The source ranges of the qualifiers in instance field references. | 702 * The offsets of implicit `this` expression references. |
| 698 * Some of them have length `0`. | |
| 699 */ | 703 */ |
| 700 Map<String, List<SourceRange>> _staticFieldQualifiers = {}; | 704 final List<int> _implicitThisOffsets = []; |
| 705 |
| 706 /** |
| 707 * The offsets of the implicit class references in static field references. |
| 708 */ |
| 709 final Map<String, List<int>> _staticFieldOffsets = {}; |
| 701 | 710 |
| 702 _SourcePart(this._base, this._source, this._prefix); | 711 _SourcePart(this._base, this._source, this._prefix); |
| 703 | 712 |
| 704 void addInstanceFieldQualifier(SourceRange range) { | 713 void addExplicitThisOffset(int offset) { |
| 705 range = rangeFromBase(range, _base); | 714 _explicitThisOffsets.add(offset - _base); |
| 706 _instanceFieldQualifiers.add(range); | 715 } |
| 716 |
| 717 void addImplicitThisOffset(int offset) { |
| 718 _implicitThisOffsets.add(offset - _base); |
| 707 } | 719 } |
| 708 | 720 |
| 709 void addParameterOccurrence(ParameterElement parameter, SourceRange range, | 721 void addParameterOccurrence(ParameterElement parameter, SourceRange range, |
| 710 int precedence) { | 722 int precedence) { |
| 711 if (parameter != null) { | 723 if (parameter != null) { |
| 712 List<_ParameterOccurrence> occurrences = _parameters[parameter]; | 724 List<_ParameterOccurrence> occurrences = _parameters[parameter]; |
| 713 if (occurrences == null) { | 725 if (occurrences == null) { |
| 714 occurrences = []; | 726 occurrences = []; |
| 715 _parameters[parameter] = occurrences; | 727 _parameters[parameter] = occurrences; |
| 716 } | 728 } |
| 717 range = rangeFromBase(range, _base); | 729 range = rangeFromBase(range, _base); |
| 718 occurrences.add(new _ParameterOccurrence(precedence, range)); | 730 occurrences.add(new _ParameterOccurrence(precedence, range)); |
| 719 } | 731 } |
| 720 } | 732 } |
| 721 | 733 |
| 722 void addStaticFieldQualifier(String className, SourceRange range) { | 734 void addStaticFieldOffset(String className, int offset) { |
| 723 List<SourceRange> ranges = _staticFieldQualifiers[className]; | 735 List<int> offsets = _staticFieldOffsets[className]; |
| 724 if (ranges == null) { | 736 if (offsets == null) { |
| 725 ranges = []; | 737 offsets = []; |
| 726 _staticFieldQualifiers[className] = ranges; | 738 _staticFieldOffsets[className] = offsets; |
| 727 } | 739 } |
| 728 range = rangeFromBase(range, _base); | 740 offsets.add(offset - _base); |
| 729 ranges.add(range); | |
| 730 } | 741 } |
| 731 | 742 |
| 732 void addVariable(VariableElement element, SourceRange range) { | 743 void addVariable(VariableElement element, SourceRange range) { |
| 733 List<SourceRange> ranges = _variables[element]; | 744 List<SourceRange> ranges = _variables[element]; |
| 734 if (ranges == null) { | 745 if (ranges == null) { |
| 735 ranges = []; | 746 ranges = []; |
| 736 _variables[element] = ranges; | 747 _variables[element] = ranges; |
| 737 } | 748 } |
| 738 range = rangeFromBase(range, _base); | 749 range = rangeFromBase(range, _base); |
| 739 ranges.add(range); | 750 ranges.add(range); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 752 /** | 763 /** |
| 753 * The [SourceRange] of the element body. | 764 * The [SourceRange] of the element body. |
| 754 */ | 765 */ |
| 755 SourceRange bodyRange; | 766 SourceRange bodyRange; |
| 756 | 767 |
| 757 /** | 768 /** |
| 758 * The [_SourcePart] to record reference into. | 769 * The [_SourcePart] to record reference into. |
| 759 */ | 770 */ |
| 760 _SourcePart result; | 771 _SourcePart result; |
| 761 | 772 |
| 773 int offset; |
| 774 |
| 762 _VariablesVisitor(this.methodElement, this.bodyRange, this.result); | 775 _VariablesVisitor(this.methodElement, this.bodyRange, this.result); |
| 763 | 776 |
| 764 @override | 777 @override |
| 765 visitNode(AstNode node) { | 778 visitNode(AstNode node) { |
| 766 SourceRange nodeRange = rangeNode(node); | 779 SourceRange nodeRange = rangeNode(node); |
| 767 if (!bodyRange.intersects(nodeRange)) { | 780 if (!bodyRange.intersects(nodeRange)) { |
| 768 return null; | 781 return null; |
| 769 } | 782 } |
| 770 super.visitNode(node); | 783 super.visitNode(node); |
| 771 } | 784 } |
| 772 | 785 |
| 773 @override | 786 @override |
| 774 visitSimpleIdentifier(SimpleIdentifier node) { | 787 visitSimpleIdentifier(SimpleIdentifier node) { |
| 775 SourceRange nodeRange = rangeNode(node); | 788 SourceRange nodeRange = rangeNode(node); |
| 776 if (bodyRange.covers(nodeRange)) { | 789 if (bodyRange.covers(nodeRange)) { |
| 777 _addInstanceFieldQualifier(node); | 790 _addInstanceFieldQualifier(node); |
| 778 _addParameter(node); | 791 _addParameter(node); |
| 779 _addVariable(node); | 792 _addVariable(node); |
| 780 } | 793 } |
| 781 } | 794 } |
| 782 | 795 |
| 796 @override |
| 797 visitThisExpression(ThisExpression node) { |
| 798 int offset = node.offset; |
| 799 if (bodyRange.contains(offset)) { |
| 800 result.addExplicitThisOffset(offset); |
| 801 } |
| 802 } |
| 803 |
| 783 void _addInstanceFieldQualifier(SimpleIdentifier node) { | 804 void _addInstanceFieldQualifier(SimpleIdentifier node) { |
| 784 PropertyAccessorElement accessor = getPropertyAccessorElement(node); | 805 PropertyAccessorElement accessor = getPropertyAccessorElement(node); |
| 785 if (isFieldAccessorElement(accessor)) { | 806 if (isFieldAccessorElement(accessor)) { |
| 786 AstNode qualifier = getNodeQualifier(node); | 807 AstNode qualifier = getNodeQualifier(node); |
| 787 if (qualifier == null || qualifier is ThisExpression) { | 808 if (qualifier == null) { |
| 809 int offset = node.offset; |
| 788 if (accessor.isStatic) { | 810 if (accessor.isStatic) { |
| 789 String className = accessor.enclosingElement.displayName; | 811 String className = accessor.enclosingElement.displayName; |
| 790 if (qualifier == null) { | 812 result.addStaticFieldOffset(className, offset); |
| 791 SourceRange qualifierRange = rangeStartLength(node, 0); | |
| 792 result.addStaticFieldQualifier(className, qualifierRange); | |
| 793 } | |
| 794 } else { | 813 } else { |
| 795 SourceRange qualifierRange; | 814 result.addImplicitThisOffset(offset); |
| 796 if (qualifier != null) { | |
| 797 qualifierRange = rangeStartStart(qualifier, node); | |
| 798 } else { | |
| 799 qualifierRange = rangeStartLength(node, 0); | |
| 800 } | |
| 801 result.addInstanceFieldQualifier(qualifierRange); | |
| 802 } | 815 } |
| 803 } | 816 } |
| 804 } | 817 } |
| 805 } | 818 } |
| 806 | 819 |
| 807 void _addParameter(SimpleIdentifier node) { | 820 void _addParameter(SimpleIdentifier node) { |
| 808 ParameterElement parameterElement = getParameterElement(node); | 821 ParameterElement parameterElement = getParameterElement(node); |
| 809 // not a parameter | 822 // not a parameter |
| 810 if (parameterElement == null) { | 823 if (parameterElement == null) { |
| 811 return; | 824 return; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 824 } | 837 } |
| 825 | 838 |
| 826 void _addVariable(SimpleIdentifier node) { | 839 void _addVariable(SimpleIdentifier node) { |
| 827 VariableElement variableElement = getLocalVariableElement(node); | 840 VariableElement variableElement = getLocalVariableElement(node); |
| 828 if (variableElement != null) { | 841 if (variableElement != null) { |
| 829 SourceRange nodeRange = rangeNode(node); | 842 SourceRange nodeRange = rangeNode(node); |
| 830 result.addVariable(variableElement, nodeRange); | 843 result.addVariable(variableElement, nodeRange); |
| 831 } | 844 } |
| 832 } | 845 } |
| 833 } | 846 } |
| OLD | NEW |