| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 /** | 5 /** |
| 6 * A collection of utility methods used by completion contributors. | 6 * A collection of utility methods used by completion contributors. |
| 7 */ | 7 */ |
| 8 import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol | 8 import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol |
| 9 show Element, ElementKind; | 9 show Element, ElementKind; |
| 10 import 'package:analysis_server/src/protocol_server.dart' | 10 import 'package:analysis_server/src/protocol_server.dart' |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 const DYNAMIC = 'dynamic'; | 23 const DYNAMIC = 'dynamic'; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * A marker used in place of `null` when a function has no return type. | 26 * A marker used in place of `null` when a function has no return type. |
| 27 */ | 27 */ |
| 28 final TypeName NO_RETURN_TYPE = astFactory.typeName( | 28 final TypeName NO_RETURN_TYPE = astFactory.typeName( |
| 29 astFactory.simpleIdentifier(new StringToken(TokenType.IDENTIFIER, '', 0)), | 29 astFactory.simpleIdentifier(new StringToken(TokenType.IDENTIFIER, '', 0)), |
| 30 null); | 30 null); |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Build a default argument list based on the given [requiredParams] and | 33 * Add default argument list text and ranges based on the given [requiredParams] |
| 34 * [namedParams]. | 34 * and [namedParams]. |
| 35 */ | 35 */ |
| 36 String buildDefaultArgList(Iterable<ParameterElement> requiredParams, | 36 void addDefaultArgDetails( |
| 37 CompletionSuggestion suggestion, |
| 38 Iterable<ParameterElement> requiredParams, |
| 37 Iterable<ParameterElement> namedParams) { | 39 Iterable<ParameterElement> namedParams) { |
| 38 List<String> args = requiredParams.map((p) => p.name).toList(); | 40 StringBuffer sb = new StringBuffer(); |
| 39 List<String> requiredArgs = namedParams | 41 List<int> ranges = <int>[]; |
| 40 .where((p) => p.isRequired) | 42 |
| 41 .map((p) => '${p.name}: null') | 43 int offset; |
| 42 .toList(); | 44 |
| 43 args.addAll(requiredArgs); | 45 for (ParameterElement param in requiredParams) { |
| 44 return args.isEmpty ? null : args.join(', '); | 46 if (sb.isNotEmpty) { |
| 47 sb.write(', '); |
| 48 } |
| 49 offset = sb.length; |
| 50 String name = param.name; |
| 51 sb.write(name); |
| 52 ranges.addAll([offset, name.length]); |
| 53 } |
| 54 |
| 55 for (ParameterElement param in namedParams) { |
| 56 if (param.isRequired) { |
| 57 if (sb.isNotEmpty) { |
| 58 sb.write(', '); |
| 59 } |
| 60 String name = param.name; |
| 61 sb.write('$name: '); |
| 62 offset = sb.length; |
| 63 String defaultValue = _getDefaultValue(param); |
| 64 sb.write(defaultValue); |
| 65 ranges.addAll([offset, defaultValue.length]); |
| 66 } |
| 67 } |
| 68 |
| 69 suggestion.defaultArgumentListString = sb.isNotEmpty ? sb.toString() : null; |
| 70 suggestion.defaultArgumentListTextRanges = ranges.isNotEmpty ? ranges : null; |
| 45 } | 71 } |
| 46 | 72 |
| 47 /** | 73 /** |
| 48 * Create a new protocol Element for inclusion in a completion suggestion. | 74 * Create a new protocol Element for inclusion in a completion suggestion. |
| 49 */ | 75 */ |
| 50 protocol.Element createLocalElement( | 76 protocol.Element createLocalElement( |
| 51 Source source, protocol.ElementKind kind, SimpleIdentifier id, | 77 Source source, protocol.ElementKind kind, SimpleIdentifier id, |
| 52 {String parameters, | 78 {String parameters, |
| 53 TypeAnnotation returnType, | 79 TypeAnnotation returnType, |
| 54 bool isAbstract: false, | 80 bool isAbstract: false, |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 TypeArgumentList typeArgs = type.typeArguments; | 190 TypeArgumentList typeArgs = type.typeArguments; |
| 165 if (typeArgs != null) { | 191 if (typeArgs != null) { |
| 166 //TODO (danrubel) include type arguments | 192 //TODO (danrubel) include type arguments |
| 167 } | 193 } |
| 168 return name; | 194 return name; |
| 169 } else if (type is GenericFunctionType) { | 195 } else if (type is GenericFunctionType) { |
| 170 // TODO(brianwilkerson) Implement this. | 196 // TODO(brianwilkerson) Implement this. |
| 171 } | 197 } |
| 172 return DYNAMIC; | 198 return DYNAMIC; |
| 173 } | 199 } |
| 200 |
| 201 String _getDefaultValue(ParameterElement param) => 'null'; |
| OLD | NEW |