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/ide_options.dart'; | 10 import 'package:analysis_server/src/ide_options.dart'; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 64 } |
65 String name = param.name; | 65 String name = param.name; |
66 sb.write('$name: '); | 66 sb.write('$name: '); |
67 offset = sb.length; | 67 offset = sb.length; |
68 String defaultValue = _getDefaultValue(param); | 68 String defaultValue = _getDefaultValue(param); |
69 sb.write(defaultValue); | 69 sb.write(defaultValue); |
70 ranges.addAll([offset, defaultValue.length]); | 70 ranges.addAll([offset, defaultValue.length]); |
71 } | 71 } |
72 } | 72 } |
73 | 73 |
74 //TODO(pq): generalize and unify with _getDefaultValue | |
75 if (options?.generateFlutterWidgetChildrenBoilerPlate == true) { | 74 if (options?.generateFlutterWidgetChildrenBoilerPlate == true) { |
76 if (element is ConstructorElement) { | 75 if (element is ConstructorElement) { |
77 ConstructorElement constructorElement = element; | 76 if (isFlutterWidget(element.enclosingElement)) { |
78 ClassElement classElement = constructorElement.enclosingElement; | 77 for (ParameterElement param in element.parameters) { |
79 if (isFlutterWidget(classElement)) { | |
80 for (ParameterElement param in constructorElement.parameters) { | |
81 if (param.name == 'children') { | 78 if (param.name == 'children') { |
82 DartType type = param.type; | 79 String defaultValue = getDefaultStringParameterValue(param); |
83 if (type is InterfaceType && isDartList(type)) { | 80 if (sb.isNotEmpty) { |
84 InterfaceType interfaceType = type; | 81 sb.write(', '); |
85 List<DartType> typeArguments = interfaceType.typeArguments; | |
86 if (typeArguments.length == 1) { | |
87 if (sb.isNotEmpty) { | |
88 sb.write(', '); | |
89 } | |
90 offset = sb.length; | |
91 sb.write('children: <${typeArguments.first.name}>[]'); | |
92 } | |
93 } | 82 } |
| 83 sb.write('children: '); |
| 84 offset = sb.length; |
| 85 sb.write(defaultValue); |
| 86 ranges.addAll([offset, defaultValue.length]); |
94 } | 87 } |
95 } | 88 } |
96 } | 89 } |
97 } | 90 } |
98 } | 91 } |
99 | 92 |
100 suggestion.defaultArgumentListString = sb.isNotEmpty ? sb.toString() : null; | 93 suggestion.defaultArgumentListString = sb.isNotEmpty ? sb.toString() : null; |
101 suggestion.defaultArgumentListTextRanges = ranges.isNotEmpty ? ranges : null; | 94 suggestion.defaultArgumentListTextRanges = ranges.isNotEmpty ? ranges : null; |
102 } | 95 } |
103 | 96 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 if (classId != null) { | 170 if (classId != null) { |
178 String className = classId.name; | 171 String className = classId.name; |
179 if (className != null && className.length > 0) { | 172 if (className != null && className.length > 0) { |
180 suggestion.declaringType = className; | 173 suggestion.declaringType = className; |
181 } | 174 } |
182 } | 175 } |
183 } | 176 } |
184 return suggestion; | 177 return suggestion; |
185 } | 178 } |
186 | 179 |
| 180 String getDefaultStringParameterValue(ParameterElement param) { |
| 181 DartType type = param.type; |
| 182 if (type is InterfaceType && isDartList(type)) { |
| 183 List<DartType> typeArguments = type.typeArguments; |
| 184 StringBuffer sb = new StringBuffer(); |
| 185 if (typeArguments.length == 1) { |
| 186 DartType typeArg = typeArguments.first; |
| 187 if (!typeArg.isDynamic) { |
| 188 sb.write('<${typeArg.name}>'); |
| 189 } |
| 190 sb.write('[]'); |
| 191 return sb.toString(); |
| 192 } |
| 193 } |
| 194 return null; |
| 195 } |
| 196 |
187 bool isDartList(DartType type) { | 197 bool isDartList(DartType type) { |
188 ClassElement element = type.element; | 198 ClassElement element = type.element; |
189 if (element != null) { | 199 if (element != null) { |
190 return element.name == "List" && element.library.isDartCore; | 200 return element.name == "List" && element.library.isDartCore; |
191 } | 201 } |
192 return false; | 202 return false; |
193 } | 203 } |
194 | 204 |
195 /** | 205 /** |
196 * Return `true` if the @deprecated annotation is present on the given [node]. | 206 * Return `true` if the @deprecated annotation is present on the given [node]. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 //TODO (danrubel) include type arguments | 241 //TODO (danrubel) include type arguments |
232 } | 242 } |
233 return name; | 243 return name; |
234 } else if (type is GenericFunctionType) { | 244 } else if (type is GenericFunctionType) { |
235 // TODO(brianwilkerson) Implement this. | 245 // TODO(brianwilkerson) Implement this. |
236 } | 246 } |
237 return DYNAMIC; | 247 return DYNAMIC; |
238 } | 248 } |
239 | 249 |
240 String _getDefaultValue(ParameterElement param) => 'null'; | 250 String _getDefaultValue(ParameterElement param) => 'null'; |
OLD | NEW |