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

Side by Side Diff: pkg/analysis_services/lib/src/correction/fix.dart

Issue 418373002: More tests for creating undefined methods and functions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library services.src.correction.fix; 8 library services.src.correction.fix;
9 9
10 import 'package:analysis_services/correction/change.dart'; 10 import 'package:analysis_services/correction/change.dart';
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 _addFix_importLibrary_withType(); 160 _addFix_importLibrary_withType();
161 _addFix_importLibrary_withTopLevelVariable(); 161 _addFix_importLibrary_withTopLevelVariable();
162 } 162 }
163 if (errorCode == StaticTypeWarningCode.INSTANCE_ACCESS_TO_STATIC_MEMBER) { 163 if (errorCode == StaticTypeWarningCode.INSTANCE_ACCESS_TO_STATIC_MEMBER) {
164 _addFix_useStaticAccess_method(); 164 _addFix_useStaticAccess_method();
165 _addFix_useStaticAccess_property(); 165 _addFix_useStaticAccess_property();
166 } 166 }
167 if (errorCode == StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION) { 167 if (errorCode == StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION) {
168 _addFix_removeParentheses_inGetterInvocation(); 168 _addFix_removeParentheses_inGetterInvocation();
169 } 169 }
170 // if (identical(errorCode, StaticTypeWarningCode.UNDEFINED_FUNCTION)) { 170 if (errorCode == StaticTypeWarningCode.UNDEFINED_FUNCTION) {
171 // _addFix_importLibrary_withFunction(); 171 _addFix_importLibrary_withFunction();
172 // _addFix_undefinedFunction_useSimilar(); 172 _addFix_undefinedFunction_useSimilar();
173 // _addFix_undefinedFunction_create(); 173 _addFix_undefinedFunction_create();
174 // } 174 }
175 if (errorCode == StaticTypeWarningCode.UNDEFINED_GETTER) { 175 if (errorCode == StaticTypeWarningCode.UNDEFINED_GETTER) {
176 _addFix_createFunction_forFunctionType(); 176 _addFix_createFunction_forFunctionType();
177 } 177 }
178 if (errorCode == HintCode.UNDEFINED_METHOD || 178 if (errorCode == HintCode.UNDEFINED_METHOD ||
179 errorCode == StaticTypeWarningCode.UNDEFINED_METHOD) { 179 errorCode == StaticTypeWarningCode.UNDEFINED_METHOD) {
180 _addFix_undefinedMethod_useSimilar(); 180 _addFix_undefinedMethod_useSimilar();
181 _addFix_undefinedMethod_create(); 181 _addFix_undefinedMethod_create();
182 _addFix_undefinedFunction_create(); 182 _addFix_undefinedFunction_create();
183 } 183 }
184 // done 184 // done
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 } 584 }
585 585
586 void 586 void
587 _addFix_createMissingOverrides(List<ExecutableElement> missingOverrides) { 587 _addFix_createMissingOverrides(List<ExecutableElement> missingOverrides) {
588 // sort by name 588 // sort by name
589 missingOverrides.sort((Element firstElement, Element secondElement) { 589 missingOverrides.sort((Element firstElement, Element secondElement) {
590 return compareStrings( 590 return compareStrings(
591 firstElement.displayName, 591 firstElement.displayName,
592 secondElement.displayName); 592 secondElement.displayName);
593 }); 593 });
594 // TODO
595 ClassDeclaration targetClass = node.parent as ClassDeclaration; 594 ClassDeclaration targetClass = node.parent as ClassDeclaration;
596 int insertOffset = targetClass.end - 1; 595 int insertOffset = targetClass.end - 1;
597 SourceBuilder sb = new SourceBuilder(file, insertOffset); 596 SourceBuilder sb = new SourceBuilder(file, insertOffset);
598 // add elements 597 // add elements
599 bool isFirst = true; 598 bool isFirst = true;
600 for (ExecutableElement missingOverride in missingOverrides) { 599 for (ExecutableElement missingOverride in missingOverrides) {
601 if (!isFirst || !targetClass.members.isEmpty) { 600 if (!isFirst || !targetClass.members.isEmpty) {
602 sb.append(eol); 601 sb.append(eol);
603 } 602 }
604 _addFix_createMissingOverrides_single(sb, targetClass, missingOverride); 603 _addFix_createMissingOverrides_single(sb, targetClass, missingOverride);
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
1049 // if (closestName != null) { 1048 // if (closestName != null) {
1050 // _addFix( 1049 // _addFix(
1051 // FixKind.CHANGE_TO, 1050 // FixKind.CHANGE_TO,
1052 // [closestName]); 1051 // [closestName]);
1053 // } 1052 // }
1054 // } 1053 // }
1055 // } 1054 // }
1056 } 1055 }
1057 1056
1058 void _addFix_undefinedFunction_create() { 1057 void _addFix_undefinedFunction_create() {
1059 // TODO(scheglov) implement 1058 // should be the name of the invocation
1060 // // should be the name of the invocation 1059 if (node is SimpleIdentifier && node.parent is MethodInvocation) {
1061 // if (node is SimpleIdentifier && node.parent is MethodInvocation) { 1060 } else {
1062 // } else { 1061 return;
1063 // return; 1062 }
1064 // } 1063 String name = (node as SimpleIdentifier).name;
1065 // String name = (node as SimpleIdentifier).name; 1064 MethodInvocation invocation = node.parent as MethodInvocation;
1066 // MethodInvocation invocation = node.parent as MethodInvocation; 1065 // function invocation has no target
1067 // // function invocation has no target 1066 Expression target = invocation.realTarget;
1068 // Expression target = invocation.realTarget; 1067 if (target != null) {
1069 // if (target != null) { 1068 return;
1070 // return; 1069 }
1071 // } 1070 // prepare environment
1072 // // prepare environment 1071 int insertOffset;
1073 // int insertOffset; 1072 String sourcePrefix;
1074 // String sourcePrefix; 1073 AstNode enclosingMember =
1075 // AstNode enclosingMember = 1074 node.getAncestor((node) => node is CompilationUnitMember);
1076 // node.getAncestor((node) => node is CompilationUnitMember); 1075 insertOffset = enclosingMember.end;
1077 // insertOffset = enclosingMember.end; 1076 sourcePrefix = "${eol}${eol}";
1078 // sourcePrefix = "${eol}${eol}"; 1077 // build method source
1079 // // build method source 1078 SourceBuilder sb = new SourceBuilder(file, insertOffset);
1080 // SourceBuilder sb = new SourceBuilder.con1(insertOffset); 1079 {
1081 // { 1080 sb.append(sourcePrefix);
1082 // sb.append(sourcePrefix); 1081 // append return type
1083 // // may be return type 1082 {
1084 // { 1083 DartType type = _inferReturnType(invocation);
1085 // DartType type = 1084 _appendType(sb, type, 'RETURN_TYPE');
1086 // _addFix_undefinedMethod_create_getReturnType(invocation); 1085 }
1087 // if (type != null) { 1086 // append name
1088 // String typeSource = utils.getTypeSource2(type); 1087 {
1089 // if (typeSource != "dynamic") { 1088 sb.startPosition("NAME");
1090 // sb.startPosition("RETURN_TYPE"); 1089 sb.append(name);
1091 // sb.append(typeSource); 1090 sb.endPosition();
1092 // sb.endPosition(); 1091 }
1093 // sb.append(" "); 1092 _addFix_undefinedMethod_create_parameters(sb, invocation.argumentList);
1094 // } 1093 sb.append(") {${eol}}");
1095 // } 1094 }
1096 // } 1095 // insert source
1097 // // append name 1096 _insertBuilder(sb);
1098 // { 1097 _addLinkedPosition3('NAME', sb, rf.rangeNode(node));
1099 // sb.startPosition("NAME"); 1098 // add proposal
1100 // sb.append(name); 1099 _addFix(FixKind.CREATE_FUNCTION, [name]);
1101 // sb.endPosition();
1102 // }
1103 // _addFix_undefinedMethod_create_parameters(sb, invocation.argumentList);
1104 // sb.append(") {${eol}}");
1105 // }
1106 // // insert source
1107 // _addInsertEdit(insertOffset, sb.toString());
1108 // // add linked positions
1109 // _addLinkedPosition("NAME", sb, SourceRangeFactory.rangeNode(node));
1110 // _addLinkedPositions(sb);
1111 // // add proposal
1112 // _addFix(FixKind.CREATE_FUNCTION, [name]);
1113 } 1100 }
1114 1101
1115 void _addFix_undefinedFunction_useSimilar() { 1102 void _addFix_undefinedFunction_useSimilar() {
1116 // TODO(scheglov) implement 1103 // TODO(scheglov) implement
1117 // if (node is SimpleIdentifier) { 1104 // if (node is SimpleIdentifier) {
1118 // String name = (node as SimpleIdentifier).name; 1105 // String name = (node as SimpleIdentifier).name;
1119 // QuickFixProcessorImpl_ClosestElementFinder finder = 1106 // QuickFixProcessorImpl_ClosestElementFinder finder =
1120 // new QuickFixProcessorImpl_ClosestElementFinder( 1107 // new QuickFixProcessorImpl_ClosestElementFinder(
1121 // name, 1108 // name,
1122 // new Predicate_QuickFixProcessorImpl_addFix_undefinedFunction_use Similar()); 1109 // new Predicate_QuickFixProcessorImpl_addFix_undefinedFunction_use Similar());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1154 String sourceSuffix; 1141 String sourceSuffix;
1155 bool staticModifier = false; 1142 bool staticModifier = false;
1156 Expression target = invocation.realTarget; 1143 Expression target = invocation.realTarget;
1157 if (target == null) { 1144 if (target == null) {
1158 targetSource = source; 1145 targetSource = source;
1159 ClassMember enclosingMember = 1146 ClassMember enclosingMember =
1160 node.getAncestor((node) => node is ClassMember); 1147 node.getAncestor((node) => node is ClassMember);
1161 staticModifier = _inStaticContext(); 1148 staticModifier = _inStaticContext();
1162 prefix = utils.getNodePrefix(enclosingMember); 1149 prefix = utils.getNodePrefix(enclosingMember);
1163 insertOffset = enclosingMember.end; 1150 insertOffset = enclosingMember.end;
1164 sourcePrefix = "${eol}${prefix}${eol}"; 1151 sourcePrefix = "${eol}${eol}";
1165 sourceSuffix = ""; 1152 sourceSuffix = "";
1166 } else { 1153 } else {
1167 // prepare target interface type 1154 // prepare target interface type
1168 DartType targetType = target.bestType; 1155 DartType targetType = target.bestType;
1169 if (targetType is! InterfaceType) { 1156 if (targetType is! InterfaceType) {
1170 return; 1157 return;
1171 } 1158 }
1172 ClassElement targetElement = targetType.element as ClassElement; 1159 ClassElement targetElement = targetType.element as ClassElement;
1173 targetSource = targetElement.source; 1160 targetSource = targetElement.source;
1174 // may be static 1161 // may be static
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1351 */ 1338 */
1352 void _addInsertEdit(int offset, String text) { 1339 void _addInsertEdit(int offset, String text) {
1353 Edit edit = new Edit(offset, 0, text); 1340 Edit edit = new Edit(offset, 0, text);
1354 edits.add(edit); 1341 edits.add(edit);
1355 } 1342 }
1356 1343
1357 /** 1344 /**
1358 * Adds a single linked position to [groupId]. 1345 * Adds a single linked position to [groupId].
1359 */ 1346 */
1360 void _addLinkedPosition(String groupId, SourceRange range) { 1347 void _addLinkedPosition(String groupId, SourceRange range) {
1361 _addLinkedPosition2( 1348 Position position = new Position(file, range.offset, range.length);
1362 groupId, 1349 LinkedPositionGroup group = _getLinkedPosition(groupId);
1363 new Position(file, range.offset, range.length)); 1350 group.addPosition(position);
1364 } 1351 }
1365 1352
1366 /** 1353 /**
1367 * Adds a single linked position to [groupId].
1368 */
1369 void _addLinkedPosition2(String groupId, Position position) {
1370 LinkedPositionGroup group = linkedPositionGroups[groupId];
1371 if (group == null) {
1372 group = new LinkedPositionGroup(groupId);
1373 linkedPositionGroups[groupId] = group;
1374 }
1375 group.add(position);
1376 }
1377
1378 /**
1379 * Adds a single linked position to [groupId]. 1354 * Adds a single linked position to [groupId].
1380 */ 1355 */
1381 void _addLinkedPosition3(String groupId, SourceBuilder sb, 1356 void _addLinkedPosition3(String groupId, SourceBuilder sb,
1382 SourceRange range) { 1357 SourceRange range) {
1383 if (sb.offset < range.offset) { 1358 if (sb.offset < range.offset) {
1384 int delta = sb.length; 1359 int delta = sb.length;
1385 range = range.getTranslated(delta); 1360 range = range.getTranslated(delta);
1386 } 1361 }
1387 _addLinkedPosition(groupId, range); 1362 _addLinkedPosition(groupId, range);
1388 } 1363 }
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
1647 } 1622 }
1648 String valueSource = 1623 String valueSource =
1649 sourceContent.substring(valueRange.offset, valueRange.end); 1624 sourceContent.substring(valueRange.offset, valueRange.end);
1650 defaultSourceMap[parameter] = valueSource; 1625 defaultSourceMap[parameter] = valueSource;
1651 } 1626 }
1652 } 1627 }
1653 return defaultSourceMap; 1628 return defaultSourceMap;
1654 } 1629 }
1655 1630
1656 /** 1631 /**
1632 * Returns an existing or just added [LinkedPositionGroup] with [groupId].
1633 */
1634 LinkedPositionGroup _getLinkedPosition(String groupId) {
1635 LinkedPositionGroup group = linkedPositionGroups[groupId];
1636 if (group == null) {
1637 group = new LinkedPositionGroup(groupId);
1638 linkedPositionGroups[groupId] = group;
1639 }
1640 return group;
1641 }
1642
1643 /**
1657 * Returns `true` if [node] is in static context. 1644 * Returns `true` if [node] is in static context.
1658 */ 1645 */
1659 bool _inStaticContext() { 1646 bool _inStaticContext() {
1660 // constructor initializer cannot reference "this" 1647 // constructor initializer cannot reference "this"
1661 if (node.getAncestor((node) => node is ConstructorInitializer) != null) { 1648 if (node.getAncestor((node) => node is ConstructorInitializer) != null) {
1662 return true; 1649 return true;
1663 } 1650 }
1664 // field initializer cannot reference "this" 1651 // field initializer cannot reference "this"
1665 if (node.getAncestor((node) => node is FieldDeclaration) != null) { 1652 if (node.getAncestor((node) => node is FieldDeclaration) != null) {
1666 return true; 1653 return true;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1811 // } 1798 // }
1812 1799
1813 /** 1800 /**
1814 * Inserts the given [SourceBuilder] at its offset. 1801 * Inserts the given [SourceBuilder] at its offset.
1815 */ 1802 */
1816 void _insertBuilder(SourceBuilder builder) { 1803 void _insertBuilder(SourceBuilder builder) {
1817 String text = builder.toString(); 1804 String text = builder.toString();
1818 _addInsertEdit(builder.offset, text); 1805 _addInsertEdit(builder.offset, text);
1819 // add linked positions 1806 // add linked positions
1820 builder.linkedPositionGroups.forEach((LinkedPositionGroup group) { 1807 builder.linkedPositionGroups.forEach((LinkedPositionGroup group) {
1808 LinkedPositionGroup fixGroup = _getLinkedPosition(group.id);
1821 group.positions.forEach((Position position) { 1809 group.positions.forEach((Position position) {
1822 _addLinkedPosition2(group.id, position); 1810 fixGroup.addPosition(position);
1811 });
1812 group.proposals.forEach((String proposal) {
1813 fixGroup.addProposal(proposal);
1823 }); 1814 });
1824 }); 1815 });
1825 } 1816 }
1826 1817
1827 _ConstructorLocation 1818 _ConstructorLocation
1828 _prepareNewConstructorLocation(ClassDeclaration classDeclaration) { 1819 _prepareNewConstructorLocation(ClassDeclaration classDeclaration) {
1829 List<ClassMember> members = classDeclaration.members; 1820 List<ClassMember> members = classDeclaration.members;
1830 // find the last field/constructor 1821 // find the last field/constructor
1831 ClassMember lastFieldOrConstructor = null; 1822 ClassMember lastFieldOrConstructor = null;
1832 for (ClassMember member in members) { 1823 for (ClassMember member in members) {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
1913 return parent.prefix == node; 1904 return parent.prefix == node;
1914 } 1905 }
1915 } 1906 }
1916 return false; 1907 return false;
1917 } 1908 }
1918 } 1909 }
1919 1910
1920 1911
1921 /** 1912 /**
1922 * Describes the location for a newly created [ConstructorDeclaration]. 1913 * Describes the location for a newly created [ConstructorDeclaration].
1923 *
1924 * TODO(scheglov) rename
1925 */ 1914 */
1926 class _ConstructorLocation { 1915 class _ConstructorLocation {
1927 final String _prefix; 1916 final String _prefix;
1928 final int _offset; 1917 final int _offset;
1929 final String _suffix; 1918 final String _suffix;
1930 1919
1931 _ConstructorLocation(this._prefix, this._offset, this._suffix); 1920 _ConstructorLocation(this._prefix, this._offset, this._suffix);
1932 } 1921 }
OLDNEW
« no previous file with comments | « pkg/analysis_services/lib/correction/change.dart ('k') | pkg/analysis_services/lib/src/correction/source_buffer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698