Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: pkg/analysis_server/lib/src/services/refactoring/extract_method.dart

Issue 832343004: Issue 21959. Check that type of parameters in 'Extract Method' occurrences are exactly the same. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/extract_method_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_method; 5 library services.src.refactoring.extract_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/name_suggestion.dart'; 10 import 'package:analysis_server/src/services/correction/name_suggestion.dart';
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 // done 496 // done
497 return source; 497 return source;
498 } 498 }
499 499
500 _SourcePattern _getSourcePattern(SourceRange range) { 500 _SourcePattern _getSourcePattern(SourceRange range) {
501 String originalSource = utils.getText(range.offset, range.length); 501 String originalSource = utils.getText(range.offset, range.length);
502 _SourcePattern pattern = new _SourcePattern(); 502 _SourcePattern pattern = new _SourcePattern();
503 List<SourceEdit> replaceEdits = <SourceEdit>[]; 503 List<SourceEdit> replaceEdits = <SourceEdit>[];
504 unit.accept(new _GetSourcePatternVisitor(range, pattern, replaceEdits)); 504 unit.accept(new _GetSourcePatternVisitor(range, pattern, replaceEdits));
505 replaceEdits = replaceEdits.reversed.toList(); 505 replaceEdits = replaceEdits.reversed.toList();
506 pattern.patternSource = 506 String source = SourceEdit.applySequence(originalSource, replaceEdits);
507 SourceEdit.applySequence(originalSource, replaceEdits); 507 pattern.normalizedSource = _getNormalizedSource(source);
508 return pattern; 508 return pattern;
509 } 509 }
510 510
511 /** 511 /**
512 * Initializes [createGetter] flag. 512 * Initializes [createGetter] flag.
513 */ 513 */
514 void _initializeGetter() { 514 void _initializeGetter() {
515 createGetter = false; 515 createGetter = false;
516 // maybe we cannot at all 516 // maybe we cannot at all
517 if (!canCreateGetter) { 517 if (!canCreateGetter) {
(...skipping 20 matching lines...) Expand all
538 } 538 }
539 } 539 }
540 540
541 /** 541 /**
542 * Fills [_occurrences] field. 542 * Fills [_occurrences] field.
543 */ 543 */
544 void _initializeOccurrences() { 544 void _initializeOccurrences() {
545 _occurrences.clear(); 545 _occurrences.clear();
546 // prepare selection 546 // prepare selection
547 _SourcePattern selectionPattern = _getSourcePattern(selectionRange); 547 _SourcePattern selectionPattern = _getSourcePattern(selectionRange);
548 String selectionSource =
549 _getNormalizedSource(selectionPattern.patternSource);
550 Map<String, String> patternToSelectionName = 548 Map<String, String> patternToSelectionName =
551 _inverseMap(selectionPattern.originalToPatternNames); 549 _inverseMap(selectionPattern.originalToPatternNames);
552 // prepare an enclosing parent - class or unit 550 // prepare an enclosing parent - class or unit
553 AstNode enclosingMemberParent = _parentMember.parent; 551 AstNode enclosingMemberParent = _parentMember.parent;
554 // visit nodes which will able to access extracted method 552 // visit nodes which will able to access extracted method
555 enclosingMemberParent.accept( 553 enclosingMemberParent.accept(
556 new _InitializeOccurrencesVisitor( 554 new _InitializeOccurrencesVisitor(
557 this, 555 this,
558 selectionSource, 556 selectionPattern,
559 patternToSelectionName)); 557 patternToSelectionName));
560 } 558 }
561 559
562 /** 560 /**
563 * Prepares information about used variables, which should be turned into 561 * Prepares information about used variables, which should be turned into
564 * parameters. 562 * parameters.
565 */ 563 */
566 RefactoringStatus _initializeParameters() { 564 RefactoringStatus _initializeParameters() {
567 _parameters.clear(); 565 _parameters.clear();
568 _parametersMap.clear(); 566 _parametersMap.clear();
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 getLocalOrParameterVariableElement(node); 832 getLocalOrParameterVariableElement(node);
835 if (variableElement != null) { 833 if (variableElement != null) {
836 // name of a named expression 834 // name of a named expression
837 if (isNamedExpressionName(node)) { 835 if (isNamedExpressionName(node)) {
838 return; 836 return;
839 } 837 }
840 // continue 838 // continue
841 String originalName = variableElement.displayName; 839 String originalName = variableElement.displayName;
842 String patternName = pattern.originalToPatternNames[originalName]; 840 String patternName = pattern.originalToPatternNames[originalName];
843 if (patternName == null) { 841 if (patternName == null) {
842 pattern.parameterTypes.add(variableElement.type);
844 patternName = '__refVar${pattern.originalToPatternNames.length}'; 843 patternName = '__refVar${pattern.originalToPatternNames.length}';
845 pattern.originalToPatternNames[originalName] = patternName; 844 pattern.originalToPatternNames[originalName] = patternName;
846 } 845 }
847 replaceEdits.add( 846 replaceEdits.add(
848 new SourceEdit( 847 new SourceEdit(
849 nodeRange.offset - partRange.offset, 848 nodeRange.offset - partRange.offset,
850 nodeRange.length, 849 nodeRange.length,
851 patternName)); 850 patternName));
852 } 851 }
853 } 852 }
854 } 853 }
855 } 854 }
856 855
857 856
858 857
859 class _HasMethodInvocationVisitor extends RecursiveAstVisitor { 858 class _HasMethodInvocationVisitor extends RecursiveAstVisitor {
860 bool result = false; 859 bool result = false;
861 860
862 @override 861 @override
863 visitMethodInvocation(MethodInvocation node) { 862 visitMethodInvocation(MethodInvocation node) {
864 result = true; 863 result = true;
865 } 864 }
866 } 865 }
867 866
868 867
869 class _InitializeOccurrencesVisitor extends GeneralizingAstVisitor<Object> { 868 class _InitializeOccurrencesVisitor extends GeneralizingAstVisitor<Object> {
870 final ExtractMethodRefactoringImpl ref; 869 final ExtractMethodRefactoringImpl ref;
871 final String selectionSource; 870 final _SourcePattern selectionPattern;
872 final Map<String, String> patternToSelectionName; 871 final Map<String, String> patternToSelectionName;
873 872
874 bool forceStatic = false; 873 bool forceStatic = false;
875 874
876 _InitializeOccurrencesVisitor(this.ref, this.selectionSource, 875 _InitializeOccurrencesVisitor(this.ref, this.selectionPattern,
877 this.patternToSelectionName); 876 this.patternToSelectionName);
878 877
879 @override 878 @override
880 Object visitBlock(Block node) { 879 Object visitBlock(Block node) {
881 if (ref._selectionStatements != null) { 880 if (ref._selectionStatements != null) {
882 _visitStatements(node.statements); 881 _visitStatements(node.statements);
883 } 882 }
884 return super.visitBlock(node); 883 return super.visitBlock(node);
885 } 884 }
886 885
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 } 923 }
925 924
926 /** 925 /**
927 * Checks if given [SourceRange] matched selection source and adds [_Occurrenc e]. 926 * Checks if given [SourceRange] matched selection source and adds [_Occurrenc e].
928 */ 927 */
929 bool _tryToFindOccurrence(SourceRange nodeRange) { 928 bool _tryToFindOccurrence(SourceRange nodeRange) {
930 // check if can be extracted 929 // check if can be extracted
931 if (!ref._isExtractable(nodeRange)) { 930 if (!ref._isExtractable(nodeRange)) {
932 return false; 931 return false;
933 } 932 }
934 // prepare normalized node source 933 // prepare node source
935 _SourcePattern nodePattern = ref._getSourcePattern(nodeRange); 934 _SourcePattern nodePattern = ref._getSourcePattern(nodeRange);
936 String nodeSource = _getNormalizedSource(nodePattern.patternSource);
937 // if matches normalized node source, then add as occurrence 935 // if matches normalized node source, then add as occurrence
938 if (nodeSource == selectionSource) { 936 if (selectionPattern.isCompatible(nodePattern)) {
939 _Occurrence occurrence = 937 _Occurrence occurrence =
940 new _Occurrence(nodeRange, ref.selectionRange.intersects(nodeRange)); 938 new _Occurrence(nodeRange, ref.selectionRange.intersects(nodeRange));
941 ref._occurrences.add(occurrence); 939 ref._occurrences.add(occurrence);
942 // prepare mapping of parameter names to the occurrence variables 940 // prepare mapping of parameter names to the occurrence variables
943 nodePattern.originalToPatternNames.forEach( 941 nodePattern.originalToPatternNames.forEach(
944 (String originalName, String patternName) { 942 (String originalName, String patternName) {
945 String selectionName = patternToSelectionName[patternName]; 943 String selectionName = patternToSelectionName[patternName];
946 occurrence._parameterOldToOccurrenceName[selectionName] = originalName; 944 occurrence._parameterOldToOccurrenceName[selectionName] = originalName;
947 }); 945 });
948 // update static 946 // update static
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 } 1128 }
1131 } 1129 }
1132 1130
1133 1131
1134 /** 1132 /**
1135 * Generalized version of some source, in which references to the specific 1133 * Generalized version of some source, in which references to the specific
1136 * variables are replaced with pattern variables, with back mapping from the 1134 * variables are replaced with pattern variables, with back mapping from the
1137 * pattern to the original variable names. 1135 * pattern to the original variable names.
1138 */ 1136 */
1139 class _SourcePattern { 1137 class _SourcePattern {
1140 String patternSource; 1138 final List<DartType> parameterTypes = <DartType>[];
1139 String normalizedSource;
1141 Map<String, String> originalToPatternNames = {}; 1140 Map<String, String> originalToPatternNames = {};
1141
1142 bool isCompatible(_SourcePattern other) {
1143 if (other.normalizedSource != normalizedSource) {
1144 return false;
1145 }
1146 if (other.parameterTypes.length != parameterTypes.length) {
1147 return false;
1148 }
1149 for (int i = 0; i < parameterTypes.length; i++) {
1150 if (other.parameterTypes[i] != parameterTypes[i]) {
1151 return false;
1152 }
1153 }
1154 return true;
1155 }
1142 } 1156 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/extract_method_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698