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 import 'package:analyzer/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
6 import 'package:analyzer/dart/element/element.dart'; | 6 import 'package:analyzer/dart/element/element.dart'; |
7 import 'package:analyzer/dart/element/type.dart'; | 7 import 'package:analyzer/dart/element/type.dart'; |
8 import 'package:analyzer/src/generated/source.dart'; | 8 import 'package:analyzer/src/generated/source.dart'; |
| 9 import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dar
t'; |
9 | 10 |
10 const _FLUTTER_WIDGET_NAME = "Widget"; | 11 const _FLUTTER_WIDGET_NAME = "Widget"; |
11 const _FLUTTER_WIDGET_URI = "package:flutter/src/widgets/framework.dart"; | 12 const _FLUTTER_WIDGET_URI = "package:flutter/src/widgets/framework.dart"; |
12 | 13 |
13 void convertFlutterChildToChildren( | 14 void convertFlutterChildToChildren( |
14 InstanceCreationExpression childArg, | 15 InstanceCreationExpression childArg, |
15 NamedExpression namedExp, | 16 NamedExpression namedExp, |
16 String eol, | 17 String eol, |
17 Function getNodeText, | 18 Function getNodeText, |
18 Function getLinePrefix, | 19 Function getLinePrefix, |
(...skipping 27 matching lines...) Expand all Loading... |
46 } else { | 47 } else { |
47 _addInsertEdit(listLoc, '<Widget>['); | 48 _addInsertEdit(listLoc, '<Widget>['); |
48 } | 49 } |
49 String newChildArgSrc = childArgSrc.replaceAll( | 50 String newChildArgSrc = childArgSrc.replaceAll( |
50 new RegExp("^$indentOld", multiLine: true), "$indentNew"); | 51 new RegExp("^$indentOld", multiLine: true), "$indentNew"); |
51 newChildArgSrc = "$prefix$newChildArgSrc,$eol$indentOld]"; | 52 newChildArgSrc = "$prefix$newChildArgSrc,$eol$indentOld]"; |
52 _addReplaceEdit(rangeNode(childArg), newChildArgSrc); | 53 _addReplaceEdit(rangeNode(childArg), newChildArgSrc); |
53 } | 54 } |
54 } | 55 } |
55 | 56 |
| 57 void convertFlutterChildToChildren2( |
| 58 DartFileEditBuilder builder, |
| 59 InstanceCreationExpression childArg, |
| 60 NamedExpression namedExp, |
| 61 String eol, |
| 62 Function getNodeText, |
| 63 Function getLinePrefix, |
| 64 Function getIndent, |
| 65 Function getText, |
| 66 Function rangeNode) { |
| 67 int childLoc = namedExp.offset + 'child'.length; |
| 68 builder.addSimpleInsertion(childLoc, 'ren'); |
| 69 int listLoc = childArg.offset; |
| 70 String childArgSrc = getNodeText(childArg); |
| 71 if (!childArgSrc.contains(eol)) { |
| 72 builder.addSimpleInsertion(listLoc, '<Widget>['); |
| 73 builder.addSimpleInsertion(listLoc + childArg.length, ']'); |
| 74 } else { |
| 75 int newlineLoc = childArgSrc.lastIndexOf(eol); |
| 76 if (newlineLoc == childArgSrc.length) { |
| 77 newlineLoc -= 1; |
| 78 } |
| 79 String indentOld = getLinePrefix(childArg.offset + 1 + newlineLoc); |
| 80 String indentNew = '$indentOld${getIndent(1)}'; |
| 81 // The separator includes 'child:' but that has no newlines. |
| 82 String separator = |
| 83 getText(namedExp.offset, childArg.offset - namedExp.offset); |
| 84 String prefix = separator.contains(eol) ? "" : "$eol$indentNew"; |
| 85 if (prefix.isEmpty) { |
| 86 builder.addSimpleInsertion( |
| 87 namedExp.offset + 'child:'.length, ' <Widget>['); |
| 88 builder.addDeletion(new SourceRange(childArg.offset - 2, 2)); |
| 89 } else { |
| 90 builder.addSimpleInsertion(listLoc, '<Widget>['); |
| 91 } |
| 92 String newChildArgSrc = childArgSrc.replaceAll( |
| 93 new RegExp("^$indentOld", multiLine: true), "$indentNew"); |
| 94 newChildArgSrc = "$prefix$newChildArgSrc,$eol$indentOld]"; |
| 95 builder.addSimpleReplacement(rangeNode(childArg), newChildArgSrc); |
| 96 } |
| 97 } |
| 98 |
56 /** | 99 /** |
57 * Return the named expression representing the 'child' argument of the given | 100 * Return the named expression representing the 'child' argument of the given |
58 * [newExpr], or null if none. | 101 * [newExpr], or null if none. |
59 */ | 102 */ |
60 NamedExpression findChildArgument(InstanceCreationExpression newExpr) => | 103 NamedExpression findChildArgument(InstanceCreationExpression newExpr) => |
61 newExpr.argumentList.arguments.firstWhere( | 104 newExpr.argumentList.arguments.firstWhere( |
62 (arg) => arg is NamedExpression && arg.name.label.name == 'child', | 105 (arg) => arg is NamedExpression && arg.name.label.name == 'child', |
63 orElse: () => null); | 106 orElse: () => null); |
64 | 107 |
65 /** | 108 /** |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 orElse: () => null); | 214 orElse: () => null); |
172 if (superType == null) { | 215 if (superType == null) { |
173 return false; | 216 return false; |
174 } | 217 } |
175 Uri uri = superType.element?.source?.uri; | 218 Uri uri = superType.element?.source?.uri; |
176 if (uri.toString() != _FLUTTER_WIDGET_URI) { | 219 if (uri.toString() != _FLUTTER_WIDGET_URI) { |
177 return false; | 220 return false; |
178 } | 221 } |
179 return true; | 222 return true; |
180 } | 223 } |
OLD | NEW |