| 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 analysis_server.src.services.correction.fix_internal; | 5 library analysis_server.src.services.correction.fix_internal; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:core'; | 9 import 'dart:core'; |
| 10 | 10 |
| (...skipping 2018 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2029 } | 2029 } |
| 2030 } | 2030 } |
| 2031 } | 2031 } |
| 2032 | 2032 |
| 2033 void _addFix_undefinedMethod_create() { | 2033 void _addFix_undefinedMethod_create() { |
| 2034 if (node is SimpleIdentifier && node.parent is MethodInvocation) { | 2034 if (node is SimpleIdentifier && node.parent is MethodInvocation) { |
| 2035 String name = (node as SimpleIdentifier).name; | 2035 String name = (node as SimpleIdentifier).name; |
| 2036 MethodInvocation invocation = node.parent as MethodInvocation; | 2036 MethodInvocation invocation = node.parent as MethodInvocation; |
| 2037 // prepare environment | 2037 // prepare environment |
| 2038 Element targetElement; | 2038 Element targetElement; |
| 2039 String prefix; | |
| 2040 int insertOffset; | |
| 2041 String sourcePrefix; | |
| 2042 String sourceSuffix; | |
| 2043 bool staticModifier = false; | 2039 bool staticModifier = false; |
| 2040 |
| 2041 ClassDeclaration targetClassNode; |
| 2044 Expression target = invocation.realTarget; | 2042 Expression target = invocation.realTarget; |
| 2045 if (target == null) { | 2043 if (target == null) { |
| 2046 targetElement = unitElement; | 2044 targetElement = unitElement; |
| 2047 ClassMember enclosingMember = | 2045 ClassMember enclosingMember = |
| 2048 node.getAncestor((node) => node is ClassMember); | 2046 node.getAncestor((node) => node is ClassMember); |
| 2049 ClassDeclaration enclosingClass = enclosingMember.parent; | 2047 targetClassNode = enclosingMember.parent; |
| 2050 utils.targetClassElement = enclosingClass.element; | 2048 utils.targetClassElement = targetClassNode.element; |
| 2051 staticModifier = _inStaticContext(); | 2049 staticModifier = _inStaticContext(); |
| 2052 prefix = utils.getNodePrefix(enclosingMember); | |
| 2053 insertOffset = enclosingMember.end; | |
| 2054 sourcePrefix = '$eol$eol'; | |
| 2055 sourceSuffix = ''; | |
| 2056 } else { | 2050 } else { |
| 2057 // prepare target interface type | 2051 // prepare target interface type |
| 2058 DartType targetType = target.bestType; | 2052 DartType targetType = target.bestType; |
| 2059 if (targetType is! InterfaceType) { | 2053 if (targetType is! InterfaceType) { |
| 2060 return; | 2054 return; |
| 2061 } | 2055 } |
| 2062 ClassElement targetClassElement = targetType.element as ClassElement; | 2056 ClassElement targetClassElement = targetType.element as ClassElement; |
| 2063 targetElement = targetClassElement; | 2057 targetElement = targetClassElement; |
| 2064 // prepare target ClassDeclaration | 2058 // prepare target ClassDeclaration |
| 2065 AstNode targetTypeNode = getParsedClassElementNode(targetClassElement); | 2059 AstNode targetTypeNode = getParsedClassElementNode(targetClassElement); |
| 2066 if (targetTypeNode is! ClassDeclaration) { | 2060 if (targetTypeNode is! ClassDeclaration) { |
| 2067 return; | 2061 return; |
| 2068 } | 2062 } |
| 2069 ClassDeclaration targetClassNode = targetTypeNode; | 2063 targetClassNode = targetTypeNode; |
| 2070 // maybe static | 2064 // maybe static |
| 2071 if (target is Identifier) { | 2065 if (target is Identifier) { |
| 2072 staticModifier = target.bestElement.kind == ElementKind.CLASS; | 2066 staticModifier = target.bestElement.kind == ElementKind.CLASS; |
| 2073 } | 2067 } |
| 2074 // prepare insert offset | |
| 2075 prefix = ' '; | |
| 2076 insertOffset = targetClassNode.end - 1; | |
| 2077 if (targetClassNode.members.isEmpty) { | |
| 2078 sourcePrefix = ''; | |
| 2079 } else { | |
| 2080 sourcePrefix = eol; | |
| 2081 } | |
| 2082 sourceSuffix = eol; | |
| 2083 // use different utils | 2068 // use different utils |
| 2084 CompilationUnitElement targetUnitElement = | 2069 CompilationUnitElement targetUnitElement = |
| 2085 getCompilationUnitElement(targetClassElement); | 2070 getCompilationUnitElement(targetClassElement); |
| 2086 CompilationUnit targetUnit = getParsedUnit(targetUnitElement); | 2071 CompilationUnit targetUnit = getParsedUnit(targetUnitElement); |
| 2087 utils = new CorrectionUtils(targetUnit); | 2072 utils = new CorrectionUtils(targetUnit); |
| 2088 } | 2073 } |
| 2074 ClassMemberLocation targetLocation = |
| 2075 utils.prepareNewMethodLocation(targetClassNode); |
| 2089 String targetFile = targetElement.source.fullName; | 2076 String targetFile = targetElement.source.fullName; |
| 2090 // build method source | 2077 // build method source |
| 2091 SourceBuilder sb = new SourceBuilder(targetFile, insertOffset); | 2078 SourceBuilder sb = new SourceBuilder(targetFile, targetLocation.offset); |
| 2092 { | 2079 { |
| 2093 sb.append(sourcePrefix); | 2080 sb.append(targetLocation.prefix); |
| 2094 sb.append(prefix); | |
| 2095 // maybe "static" | 2081 // maybe "static" |
| 2096 if (staticModifier) { | 2082 if (staticModifier) { |
| 2097 sb.append('static '); | 2083 sb.append('static '); |
| 2098 } | 2084 } |
| 2099 // append return type | 2085 // append return type |
| 2100 { | 2086 { |
| 2101 DartType type = _inferUndefinedExpressionType(invocation); | 2087 DartType type = _inferUndefinedExpressionType(invocation); |
| 2102 _appendType(sb, type, groupId: 'RETURN_TYPE'); | 2088 _appendType(sb, type, groupId: 'RETURN_TYPE'); |
| 2103 } | 2089 } |
| 2104 // append name | 2090 // append name |
| 2105 { | 2091 { |
| 2106 sb.startPosition('NAME'); | 2092 sb.startPosition('NAME'); |
| 2107 sb.append(name); | 2093 sb.append(name); |
| 2108 sb.endPosition(); | 2094 sb.endPosition(); |
| 2109 } | 2095 } |
| 2110 _addFix_undefinedMethod_create_parameters(sb, invocation.argumentList); | 2096 _addFix_undefinedMethod_create_parameters(sb, invocation.argumentList); |
| 2111 sb.append(') {$eol$prefix}'); | 2097 sb.append(') {}'); |
| 2112 sb.append(sourceSuffix); | 2098 sb.append(targetLocation.suffix); |
| 2113 } | 2099 } |
| 2114 // insert source | 2100 // insert source |
| 2115 _insertBuilder(sb, targetElement); | 2101 _insertBuilder(sb, targetElement); |
| 2116 // add linked positions | 2102 // add linked positions |
| 2117 if (targetFile == file) { | 2103 if (targetFile == file) { |
| 2118 _addLinkedPosition('NAME', sb, rf.rangeNode(node)); | 2104 _addLinkedPosition('NAME', sb, rf.rangeNode(node)); |
| 2119 } | 2105 } |
| 2120 // add proposal | 2106 // add proposal |
| 2121 _addFix(DartFixKind.CREATE_METHOD, [name]); | 2107 _addFix(DartFixKind.CREATE_METHOD, [name]); |
| 2122 } | 2108 } |
| (...skipping 830 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2953 } | 2939 } |
| 2954 } | 2940 } |
| 2955 } | 2941 } |
| 2956 | 2942 |
| 2957 void _updateList(Iterable<Element> elements) { | 2943 void _updateList(Iterable<Element> elements) { |
| 2958 for (Element element in elements) { | 2944 for (Element element in elements) { |
| 2959 _update(element); | 2945 _update(element); |
| 2960 } | 2946 } |
| 2961 } | 2947 } |
| 2962 } | 2948 } |
| OLD | NEW |