Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.utilities.change_builder_dart; | 5 library analysis_server.src.utilities.change_builder_dart; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart' hide ElementKind; | 7 import 'package:analysis_server/src/protocol.dart' hide ElementKind; |
| 8 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; | 8 import 'package:analysis_server/src/services/correction/name_suggestion.dart'; |
| 9 import 'package:analysis_server/src/services/correction/util.dart'; | 9 import 'package:analysis_server/src/services/correction/util.dart'; |
| 10 import 'package:analysis_server/src/utilities/change_builder_core.dart'; | 10 import 'package:analysis_server/src/utilities/change_builder_core.dart'; |
| 11 import 'package:analysis_server/utilities/change_builder_core.dart'; | 11 import 'package:analysis_server/utilities/change_builder_core.dart'; |
| 12 import 'package:analysis_server/utilities/change_builder_dart.dart'; | 12 import 'package:analysis_server/utilities/change_builder_dart.dart'; |
| 13 import 'package:analyzer/src/generated/ast.dart'; | 13 import 'package:analyzer/src/generated/ast.dart'; |
| 14 import 'package:analyzer/src/generated/element.dart'; | 14 import 'package:analyzer/src/generated/element.dart'; |
| 15 import 'package:analyzer/src/generated/engine.dart'; | 15 import 'package:analyzer/src/generated/engine.dart'; |
| 16 import 'package:analyzer/src/generated/scanner.dart'; | |
| 16 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/utilities_dart.dart'; | 18 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * A [ChangeBuilder] used to build changes in Dart files. | 21 * A [ChangeBuilder] used to build changes in Dart files. |
| 21 */ | 22 */ |
| 22 class DartChangeBuilderImpl extends ChangeBuilderImpl | 23 class DartChangeBuilderImpl extends ChangeBuilderImpl |
| 23 implements DartChangeBuilder { | 24 implements DartChangeBuilder { |
| 24 /** | 25 /** |
| 25 * The analysis context in which the files being edited were analyzed. | 26 * The analysis context in which the files being edited were analyzed. |
| 26 */ | 27 */ |
| 27 final AnalysisContext context; | 28 final AnalysisContext context; |
| 28 | 29 |
| 29 /** | 30 /** |
| 30 * Initialize a newly created change builder. | 31 * Initialize a newly created change builder. |
| 31 */ | 32 */ |
| 32 DartChangeBuilderImpl(this.context); | 33 DartChangeBuilderImpl(this.context); |
| 33 | 34 |
| 34 @override | 35 @override |
| 35 DartFileEditBuilderImpl createFileEditBuilder(Source source, int fileStamp) { | 36 DartFileEditBuilderImpl createFileEditBuilder(Source source, int fileStamp) { |
| 36 return new DartFileEditBuilderImpl(this, source, fileStamp); | 37 return new DartFileEditBuilderImpl(this, source, fileStamp); |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 | 40 |
| 40 /** | 41 /** |
| 41 * An [EditBuilder] used to build edits in Dart files. | 42 * An [EditBuilder] used to build edits in Dart files. |
| 42 */ | 43 */ |
| 43 class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder { | 44 class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder { |
| 44 /** | 45 /** |
| 45 * The string used when writing the 'abstract' modifier. | |
| 46 */ | |
| 47 static const String ABSTRACT_MODIFIER = 'abstract '; | |
| 48 | |
| 49 /** | |
| 50 * The string used when writing the 'const' modifier. | |
| 51 */ | |
| 52 static const String CONST_MODIFIER = 'const '; | |
| 53 | |
| 54 /** | |
| 55 * A utility class used to help build the source code. | 46 * A utility class used to help build the source code. |
| 56 */ | 47 */ |
| 57 final CorrectionUtils utils; | 48 final CorrectionUtils utils; |
| 58 | 49 |
| 59 // /** | |
| 60 // * The string used when writing the 'final' modifier. | |
| 61 // */ | |
| 62 // static const String FINAL_MODIFIER = 'final '; | |
| 63 // | |
| 64 // /** | |
| 65 // * The string used when writing the 'static' modifier. | |
| 66 // */ | |
| 67 // static const String STATIC_MODIFIER = 'static '; | |
| 68 | |
| 69 /** | 50 /** |
| 70 * Initialize a newly created builder to build a source edit. | 51 * Initialize a newly created builder to build a source edit. |
| 71 */ | 52 */ |
| 72 DartEditBuilderImpl( | 53 DartEditBuilderImpl( |
| 73 DartFileEditBuilderImpl sourceFileEditBuilder, int offset, int length) | 54 DartFileEditBuilderImpl sourceFileEditBuilder, int offset, int length) |
| 74 : utils = sourceFileEditBuilder.utils, | 55 : utils = sourceFileEditBuilder.utils, |
| 75 super(sourceFileEditBuilder, offset, length); | 56 super(sourceFileEditBuilder, offset, length); |
| 76 | 57 |
| 77 DartFileEditBuilderImpl get dartFileEditBuilder => fileEditBuilder; | 58 DartFileEditBuilderImpl get dartFileEditBuilder => fileEditBuilder; |
| 78 | 59 |
| 79 @override | 60 @override |
| 80 void writeClassDeclaration(String name, {Iterator<DartType> interfaces, | 61 void writeClassDeclaration(String name, {Iterable<DartType> interfaces, |
| 81 bool isAbstract: false, void memberWriter(), Iterator<DartType> mixins, Da rtType superclass}) { | 62 bool isAbstract: false, void memberWriter(), Iterable<DartType> mixins, |
| 63 String nameGroupName, DartType superclass}) { | |
|
scheglov
2015/05/04 04:41:27
The "nameGroupName" parameter is not used.
Brian Wilkerson
2015/05/04 15:03:37
Done
| |
| 82 // TODO(brianwilkerson) Add support for type parameters | 64 // TODO(brianwilkerson) Add support for type parameters |
| 83 // Map<String, DartType>, List<TypeParameter>? | 65 // Map<String, DartType>, List<TypeParameter>? |
| 84 // | |
| 85 // TODO(brianwilkerson) Make additional optional parameters visible in the | |
| 86 // public API. | |
| 87 if (isAbstract) { | 66 if (isAbstract) { |
| 88 write(ABSTRACT_MODIFIER); | 67 write(Keyword.ABSTRACT.syntax); |
|
scheglov
2015/05/04 04:41:27
I don't see why we need to use token literals.
We
Brian Wilkerson
2015/05/04 15:03:37
True. Perhaps it's overkill, but it seemed cleaner
| |
| 68 ; | |
|
scheglov
2015/05/04 04:41:27
Empty statement.
Brian Wilkerson
2015/05/04 15:03:37
Removed
| |
| 69 write(' '); | |
| 89 } | 70 } |
| 90 write('class '); | 71 write('class '); |
| 91 addLinkedEdit(DartEditBuilder.NAME_GROUP_ID, (LinkedEditBuilder builder) { | 72 addLinkedEdit(DartEditBuilder.NAME_GROUP_ID, (LinkedEditBuilder builder) { |
| 92 write(name); | 73 write(name); |
| 93 }); | 74 }); |
| 94 if (superclass != null) { | 75 if (superclass != null) { |
| 95 write(' extends '); | 76 write(' extends '); |
| 96 writeType(superclass, groupName: DartEditBuilder.SUPERCLASS_GROUP_ID); | 77 writeType(superclass, groupName: DartEditBuilder.SUPERCLASS_GROUP_ID); |
| 78 } else if (mixins != null && mixins.isNotEmpty) { | |
| 79 write(' extends Object '); | |
| 97 } | 80 } |
| 98 writeTypes(mixins, prefix: ' with '); | 81 writeTypes(mixins, prefix: ' with '); |
| 99 writeTypes(interfaces, prefix: ' implements '); | 82 writeTypes(interfaces, prefix: ' implements '); |
| 100 writeln(' {'); | 83 writeln(' {'); |
| 101 if (memberWriter != null) { | 84 if (memberWriter != null) { |
| 85 writeln(); | |
| 102 memberWriter(); | 86 memberWriter(); |
| 87 writeln(); | |
| 103 } | 88 } |
| 104 write('}'); | 89 write('}'); |
| 105 } | 90 } |
| 106 | 91 |
| 107 /** | 92 //@override |
| 108 * Write the code for a comma-separated list of [types], optionally prefixed | |
| 109 * by a [prefix]. If the list of [types] is `null` or does not return any | |
| 110 * types, then nothing will be written. | |
| 111 */ | |
| 112 void writeTypes(Iterator<DartType> types, {String prefix}) { | |
| 113 if (types == null) { | |
| 114 return; | |
| 115 } | |
| 116 bool first = true; | |
| 117 while (types.moveNext()) { | |
| 118 if (first) { | |
| 119 if (prefix != null) { | |
| 120 write(prefix); | |
| 121 } | |
| 122 first = false; | |
| 123 } else { | |
| 124 write(', '); | |
| 125 } | |
| 126 writeType(types.current); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void writeConstructorDeclaration(ClassElement classElement, | 93 void writeConstructorDeclaration(ClassElement classElement, |
| 131 {ArgumentList argumentList, SimpleIdentifier constructorName, | 94 {ArgumentList argumentList, SimpleIdentifier constructorName, |
| 132 bool isConst: false}) { | 95 bool isConst: false}) { |
| 133 // TODO(brianwilkerson) Clean up the API and add it to the public API. | 96 // TODO(brianwilkerson) Clean up the API and add it to the public API. |
| 134 // | 97 // |
| 135 // TODO(brianwilkerson) Support passing a list of final fields rather than | 98 // TODO(brianwilkerson) Support passing a list of final fields rather than |
| 136 // an argument list. | 99 // an argument list. |
| 137 if (isConst) { | 100 if (isConst) { |
| 138 write(CONST_MODIFIER); | 101 write(Keyword.CONST.syntax); |
| 102 write(' '); | |
| 139 } | 103 } |
| 140 write(classElement.name); | 104 write(classElement.name); |
| 141 write('.'); | 105 write('.'); |
| 142 if (constructorName != null) { | 106 if (constructorName != null) { |
| 143 addLinkedEdit(DartEditBuilder.NAME_GROUP_ID, (LinkedEditBuilder builder) { | 107 addLinkedEdit(DartEditBuilder.NAME_GROUP_ID, (LinkedEditBuilder builder) { |
| 144 write(constructorName.name); | 108 write(constructorName.name); |
| 145 }); | 109 }); |
| 146 CompilationUnit unit = constructorName | 110 CompilationUnit unit = constructorName |
| 147 .getAncestor((AstNode node) => node is CompilationUnit); | 111 .getAncestor((AstNode node) => node is CompilationUnit); |
| 148 if (unit != null) { | 112 if (unit != null) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 159 if (argumentList != null) { | 123 if (argumentList != null) { |
| 160 writeParametersMatchingArguments(argumentList); | 124 writeParametersMatchingArguments(argumentList); |
| 161 } else { | 125 } else { |
| 162 write('()'); | 126 write('()'); |
| 163 } | 127 } |
| 164 writeln(' {'); | 128 writeln(' {'); |
| 165 write(' }'); | 129 write(' }'); |
| 166 } | 130 } |
| 167 | 131 |
| 168 @override | 132 @override |
| 133 void writeFieldDeclaration(String name, {void initializerWriter(), | |
| 134 bool isConst: false, bool isFinal: false, bool isStatic: false, | |
| 135 String nameGroupName, DartType type, String typeGroupName}) { | |
| 136 if (isStatic) { | |
| 137 write(Keyword.STATIC.syntax); | |
| 138 write(' '); | |
| 139 } | |
| 140 bool typeRequired = true; | |
| 141 if (isConst) { | |
| 142 write(Keyword.CONST.syntax); | |
| 143 typeRequired = false; | |
| 144 } else if (isFinal) { | |
| 145 write(Keyword.FINAL.syntax); | |
| 146 typeRequired = false; | |
| 147 } | |
| 148 if (type != null) { | |
| 149 writeType(type, groupName: typeGroupName); | |
| 150 } else if (typeRequired) { | |
| 151 write(Keyword.VAR.syntax); | |
| 152 } | |
| 153 write(' '); | |
| 154 if (nameGroupName != null) { | |
| 155 addLinkedEdit(nameGroupName, (LinkedEditBuilder builder) { | |
| 156 write(name); | |
| 157 }); | |
| 158 } else { | |
| 159 write(name); | |
| 160 } | |
| 161 if (initializerWriter != null) { | |
| 162 write(' = '); | |
| 163 initializerWriter(); | |
| 164 } | |
| 165 write(';'); | |
| 166 } | |
| 167 | |
| 168 @override | |
| 169 void writeGetterDeclaration(String name, {void bodyWriter(), | |
| 170 bool isStatic: false, String nameGroupName, DartType returnType, | |
| 171 String returnTypeGroupName}) { | |
| 172 if (isStatic) { | |
| 173 write(Keyword.STATIC.syntax); | |
| 174 write(' '); | |
| 175 } | |
| 176 if (returnType != null) { | |
| 177 writeType(returnType, groupName: returnTypeGroupName); | |
| 178 write(' '); | |
| 179 } | |
| 180 write(Keyword.GET.syntax); | |
| 181 write(' '); | |
| 182 if (nameGroupName != null) { | |
| 183 addLinkedEdit(nameGroupName, (LinkedEditBuilder builder) { | |
| 184 write(name); | |
| 185 }); | |
| 186 } else { | |
| 187 write(name); | |
| 188 } | |
| 189 if (bodyWriter == null) { | |
| 190 write(' => null;'); | |
| 191 } else { | |
| 192 write(' '); | |
| 193 bodyWriter(); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 @override | |
| 169 void writeOverrideOfInheritedMember(ExecutableElement member) { | 198 void writeOverrideOfInheritedMember(ExecutableElement member) { |
| 170 // prepare environment | 199 // prepare environment |
| 171 String prefix = utils.getIndent(1); | 200 String prefix = utils.getIndent(1); |
| 172 // may be property | 201 // may be property |
| 173 String prefix2 = utils.getIndent(2); | 202 String prefix2 = utils.getIndent(2); |
| 174 ElementKind elementKind = member.kind; | 203 ElementKind elementKind = member.kind; |
| 175 bool isGetter = elementKind == ElementKind.GETTER; | 204 bool isGetter = elementKind == ElementKind.GETTER; |
| 176 bool isSetter = elementKind == ElementKind.SETTER; | 205 bool isSetter = elementKind == ElementKind.SETTER; |
| 177 bool isMethod = elementKind == ElementKind.METHOD; | 206 bool isMethod = elementKind == ElementKind.METHOD; |
| 178 bool isOperator = isMethod && (member as MethodElement).isOperator; | 207 bool isOperator = isMethod && (member as MethodElement).isOperator; |
| 179 write(prefix); | 208 write(prefix); |
| 180 if (isGetter) { | 209 if (isGetter) { |
| 181 writeln('// TODO: implement ${member.displayName}'); | 210 writeln('// TODO: implement ${member.displayName}'); |
| 182 write(prefix); | 211 write(prefix); |
| 183 } | 212 } |
| 184 // @override | 213 // @override |
| 185 writeln('@override'); | 214 writeln('@override'); |
| 186 write(prefix); | 215 write(prefix); |
| 187 // return type | 216 // return type |
| 188 // REVIEW: Added groupId | 217 // REVIEW: Added groupId |
| 189 bool shouldReturn = writeType(member.type.returnType, | 218 bool shouldReturn = writeType(member.type.returnType, |
| 190 groupName: DartEditBuilder.RETURN_TYPE_GROUP_ID); | 219 groupName: DartEditBuilder.RETURN_TYPE_GROUP_ID); |
| 191 write(' '); | 220 write(' '); |
| 192 if (isGetter) { | 221 if (isGetter) { |
| 193 write('get '); | 222 write(Keyword.GET.syntax); |
| 223 write(' '); | |
| 194 } else if (isSetter) { | 224 } else if (isSetter) { |
| 195 write('set '); | 225 write(Keyword.SET.syntax); |
| 226 write(' '); | |
| 196 } else if (isOperator) { | 227 } else if (isOperator) { |
| 197 write('operator '); | 228 write(Keyword.OPERATOR.syntax); |
| 229 write(' '); | |
| 198 } | 230 } |
| 199 // name | 231 // name |
| 200 write(member.displayName); | 232 write(member.displayName); |
| 201 // parameters + body | 233 // parameters + body |
| 202 if (isGetter) { | 234 if (isGetter) { |
| 203 writeln(' => null;'); | 235 writeln(' => null;'); |
| 204 } else { | 236 } else { |
| 205 List<ParameterElement> parameters = member.parameters; | 237 List<ParameterElement> parameters = member.parameters; |
| 206 writeParameters(parameters); | 238 writeParameters(parameters); |
| 207 writeln(' {'); | 239 writeln(' {'); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 write(typeSource); | 355 write(typeSource); |
| 324 if (addSupertypeProposals) { | 356 if (addSupertypeProposals) { |
| 325 _addSuperTypeProposals(builder, type, new Set<DartType>()); | 357 _addSuperTypeProposals(builder, type, new Set<DartType>()); |
| 326 } | 358 } |
| 327 }); | 359 }); |
| 328 } else { | 360 } else { |
| 329 write(typeSource); | 361 write(typeSource); |
| 330 } | 362 } |
| 331 return true; | 363 return true; |
| 332 } else if (required) { | 364 } else if (required) { |
| 333 write('var'); | 365 write(Keyword.VAR.syntax); |
| 334 } | 366 } |
| 335 return false; | 367 return false; |
| 336 } | 368 } |
| 337 | 369 |
| 370 /** | |
| 371 * Write the code for a comma-separated list of [types], optionally prefixed | |
| 372 * by a [prefix]. If the list of [types] is `null` or does not return any | |
| 373 * types, then nothing will be written. | |
| 374 */ | |
| 375 void writeTypes(Iterable<DartType> types, {String prefix}) { | |
| 376 if (types == null || types.isEmpty) { | |
| 377 return; | |
| 378 } | |
| 379 bool first = true; | |
| 380 for (DartType type in types) { | |
| 381 if (first) { | |
| 382 if (prefix != null) { | |
| 383 write(prefix); | |
| 384 } | |
| 385 first = false; | |
| 386 } else { | |
| 387 write(', '); | |
| 388 } | |
| 389 writeType(type); | |
| 390 } | |
| 391 } | |
| 392 | |
| 338 void _addSuperTypeProposals( | 393 void _addSuperTypeProposals( |
| 339 LinkedEditBuilder builder, DartType type, Set<DartType> alreadyAdded) { | 394 LinkedEditBuilder builder, DartType type, Set<DartType> alreadyAdded) { |
| 340 if (type != null && | 395 if (type != null && |
| 341 type.element is ClassElement && | 396 type.element is ClassElement && |
| 342 alreadyAdded.add(type)) { | 397 alreadyAdded.add(type)) { |
| 343 ClassElement element = type.element as ClassElement; | 398 ClassElement element = type.element as ClassElement; |
| 344 builder.addSuggestion(LinkedEditSuggestionKind.TYPE, element.name); | 399 builder.addSuggestion(LinkedEditSuggestionKind.TYPE, element.name); |
| 345 _addSuperTypeProposals(builder, element.supertype, alreadyAdded); | 400 _addSuperTypeProposals(builder, element.supertype, alreadyAdded); |
| 346 for (InterfaceType interfaceType in element.interfaces) { | 401 for (InterfaceType interfaceType in element.interfaces) { |
| 347 _addSuperTypeProposals(builder, interfaceType, alreadyAdded); | 402 _addSuperTypeProposals(builder, interfaceType, alreadyAdded); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 } | 458 } |
| 404 unit = context.resolveCompilationUnit2(source, librariesContaining[0]); | 459 unit = context.resolveCompilationUnit2(source, librariesContaining[0]); |
| 405 utils = new CorrectionUtils(unit); | 460 utils = new CorrectionUtils(unit); |
| 406 } | 461 } |
| 407 | 462 |
| 408 @override | 463 @override |
| 409 DartEditBuilderImpl createEditBuilder(int offset, int length) { | 464 DartEditBuilderImpl createEditBuilder(int offset, int length) { |
| 410 return new DartEditBuilderImpl(this, offset, length); | 465 return new DartEditBuilderImpl(this, offset, length); |
| 411 } | 466 } |
| 412 } | 467 } |
| OLD | NEW |