Chromium Code Reviews| 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 ClassElement classElement = element.enclosingElement; |
|
danrubel
2017/03/24 13:16:19
It appears that "element" is not modified in this
pquitslund
2017/03/24 15:56:23
Done.
| |
| 78 ClassElement classElement = constructorElement.enclosingElement; | |
| 79 if (isFlutterWidget(classElement)) { | 77 if (isFlutterWidget(classElement)) { |
| 80 for (ParameterElement param in constructorElement.parameters) { | 78 for (ParameterElement param in element.parameters) { |
| 81 if (param.name == 'children') { | 79 if (param.name == 'children') { |
| 82 DartType type = param.type; | 80 String defaultValue = getDefaultStringParameterValue(param); |
| 83 if (type is InterfaceType && isDartList(type)) { | 81 if (sb.isNotEmpty) { |
| 84 InterfaceType interfaceType = type; | 82 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 } | 83 } |
| 84 sb.write('children: '); | |
| 85 offset = sb.length; | |
| 86 sb.write(defaultValue); | |
|
Brian Wilkerson
2017/03/23 23:34:48
'defaultValue' can be null here.
pquitslund
2017/03/24 15:56:23
Right. I was thinking this was OK since the `toSt
Brian Wilkerson
2017/03/24 16:04:32
Given a widget class with a constructor of the for
| |
| 87 ranges.addAll([offset, defaultValue.length]); | |
| 94 } | 88 } |
| 95 } | 89 } |
| 96 } | 90 } |
| 97 } | 91 } |
| 98 } | 92 } |
| 99 | 93 |
| 100 suggestion.defaultArgumentListString = sb.isNotEmpty ? sb.toString() : null; | 94 suggestion.defaultArgumentListString = sb.isNotEmpty ? sb.toString() : null; |
| 101 suggestion.defaultArgumentListTextRanges = ranges.isNotEmpty ? ranges : null; | 95 suggestion.defaultArgumentListTextRanges = ranges.isNotEmpty ? ranges : null; |
| 102 } | 96 } |
| 103 | 97 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 if (classId != null) { | 171 if (classId != null) { |
| 178 String className = classId.name; | 172 String className = classId.name; |
| 179 if (className != null && className.length > 0) { | 173 if (className != null && className.length > 0) { |
| 180 suggestion.declaringType = className; | 174 suggestion.declaringType = className; |
| 181 } | 175 } |
| 182 } | 176 } |
| 183 } | 177 } |
| 184 return suggestion; | 178 return suggestion; |
| 185 } | 179 } |
| 186 | 180 |
| 181 String getDefaultStringParameterValue(ParameterElement param) { | |
|
Brian Wilkerson
2017/03/23 23:34:47
Consider passing in a StringSink here rather than
pquitslund
2017/03/24 15:56:23
Interesting idea. As mentioned above, I don't thi
| |
| 182 DartType type = param.type; | |
| 183 if (type is InterfaceType && isDartList(type)) { | |
| 184 List<DartType> typeArguments = type.typeArguments; | |
| 185 StringBuffer sb = new StringBuffer(); | |
| 186 if (typeArguments.length == 1) { | |
| 187 DartType typeArg = typeArguments.first; | |
| 188 if (!typeArg.isDynamic) { | |
| 189 sb.write('<${typeArg.name}>'); | |
| 190 } | |
| 191 sb.write('[]'); | |
| 192 return sb.toString(); | |
| 193 } | |
| 194 } | |
| 195 return null; | |
| 196 } | |
| 197 | |
| 187 bool isDartList(DartType type) { | 198 bool isDartList(DartType type) { |
| 188 ClassElement element = type.element; | 199 ClassElement element = type.element; |
| 189 if (element != null) { | 200 if (element != null) { |
| 190 return element.name == "List" && element.library.isDartCore; | 201 return element.name == "List" && element.library.isDartCore; |
| 191 } | 202 } |
| 192 return false; | 203 return false; |
| 193 } | 204 } |
| 194 | 205 |
| 195 /** | 206 /** |
| 196 * Return `true` if the @deprecated annotation is present on the given [node]. | 207 * 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 | 242 //TODO (danrubel) include type arguments |
| 232 } | 243 } |
| 233 return name; | 244 return name; |
| 234 } else if (type is GenericFunctionType) { | 245 } else if (type is GenericFunctionType) { |
| 235 // TODO(brianwilkerson) Implement this. | 246 // TODO(brianwilkerson) Implement this. |
| 236 } | 247 } |
| 237 return DYNAMIC; | 248 return DYNAMIC; |
| 238 } | 249 } |
| 239 | 250 |
| 240 String _getDefaultValue(ParameterElement param) => 'null'; | 251 String _getDefaultValue(ParameterElement param) => 'null'; |
| OLD | NEW |