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 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
(...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1074 } | 1074 } |
1075 | 1075 |
1076 // Prepare all URIs to import. | 1076 // Prepare all URIs to import. |
1077 List<String> uriList = libraries | 1077 List<String> uriList = libraries |
1078 .map((library) => _getLibrarySourceUri(targetLibrary, library)) | 1078 .map((library) => _getLibrarySourceUri(targetLibrary, library)) |
1079 .toList(); | 1079 .toList(); |
1080 uriList.sort((a, b) => a.compareTo(b)); | 1080 uriList.sort((a, b) => a.compareTo(b)); |
1081 | 1081 |
1082 // Insert imports: between existing imports. | 1082 // Insert imports: between existing imports. |
1083 if (importDirectives.isNotEmpty) { | 1083 if (importDirectives.isNotEmpty) { |
1084 bool isFirstPackage = true; | |
1085 for (String importUri in uriList) { | 1084 for (String importUri in uriList) { |
| 1085 bool isDart = importUri.startsWith('dart:'); |
| 1086 bool isPackage = importUri.startsWith('package:'); |
1086 bool inserted = false; | 1087 bool inserted = false; |
1087 bool isPackage = importUri.startsWith('package:'); | 1088 |
1088 bool isAfterDart = false; | 1089 void insert( |
| 1090 {ImportDirective prev, |
| 1091 ImportDirective next, |
| 1092 String uri, |
| 1093 bool trailingNewLine: false}) { |
| 1094 if (prev != null) { |
| 1095 addInsertion(prev.end, (EditBuilder builder) { |
| 1096 builder.writeln(); |
| 1097 builder.write("import '"); |
| 1098 builder.write(uri); |
| 1099 builder.write("';"); |
| 1100 }); |
| 1101 } else { |
| 1102 addInsertion(next.offset, (EditBuilder builder) { |
| 1103 builder.write("import '"); |
| 1104 builder.write(uri); |
| 1105 builder.writeln("';"); |
| 1106 if (trailingNewLine) { |
| 1107 builder.writeln(); |
| 1108 } |
| 1109 }); |
| 1110 } |
| 1111 inserted = true; |
| 1112 } |
| 1113 |
| 1114 ImportDirective lastExisting; |
| 1115 ImportDirective lastExistingDart; |
| 1116 ImportDirective lastExistingPackage; |
| 1117 bool isLastExistingDart = false; |
| 1118 bool isLastExistingPackage = false; |
1089 for (ImportDirective existingImport in importDirectives) { | 1119 for (ImportDirective existingImport in importDirectives) { |
1090 if (existingImport.uriContent.startsWith('dart:')) { | 1120 String existingUri = existingImport.uriContent; |
1091 isAfterDart = true; | 1121 |
| 1122 bool isExistingDart = existingUri.startsWith('dart:'); |
| 1123 bool isExistingPackage = existingUri.startsWith('package:'); |
| 1124 bool isExistingRelative = !existingUri.contains(':'); |
| 1125 |
| 1126 bool isNewBeforeExisting = importUri.compareTo(existingUri) < 0; |
| 1127 |
| 1128 if (isDart) { |
| 1129 if (!isExistingDart || isNewBeforeExisting) { |
| 1130 insert( |
| 1131 prev: lastExistingDart, |
| 1132 next: existingImport, |
| 1133 uri: importUri, |
| 1134 trailingNewLine: !isExistingDart); |
| 1135 break; |
| 1136 } |
| 1137 } else if (isPackage) { |
| 1138 if (isExistingRelative || isNewBeforeExisting) { |
| 1139 insert( |
| 1140 prev: lastExistingPackage, |
| 1141 next: existingImport, |
| 1142 uri: importUri, |
| 1143 trailingNewLine: isExistingRelative); |
| 1144 break; |
| 1145 } |
| 1146 } else { |
| 1147 if (!isExistingDart && !isExistingPackage && isNewBeforeExisting) { |
| 1148 insert(next: existingImport, uri: importUri); |
| 1149 break; |
| 1150 } |
1092 } | 1151 } |
1093 if (existingImport.uriContent.startsWith('package:')) { | 1152 |
1094 isFirstPackage = false; | 1153 lastExisting = existingImport; |
| 1154 if (isExistingDart) { |
| 1155 lastExistingDart = existingImport; |
| 1156 } else if (isExistingPackage) { |
| 1157 lastExistingPackage = existingImport; |
1095 } | 1158 } |
1096 if (importUri.compareTo(existingImport.uriContent) < 0) { | 1159 isLastExistingDart = isExistingDart; |
1097 addInsertion(existingImport.offset, (EditBuilder builder) { | 1160 isLastExistingPackage = isExistingPackage; |
1098 builder.write("import '"); | |
1099 builder.write(importUri); | |
1100 builder.writeln("';"); | |
1101 }); | |
1102 inserted = true; | |
1103 break; | |
1104 } | |
1105 } | 1161 } |
1106 if (!inserted) { | 1162 if (!inserted) { |
1107 addInsertion(importDirectives.last.end, (EditBuilder builder) { | 1163 addInsertion(lastExisting.end, (EditBuilder builder) { |
1108 if (isPackage && isFirstPackage && isAfterDart) { | 1164 if (isPackage) { |
1109 builder.writeln(); | 1165 if (isLastExistingDart) { |
| 1166 builder.writeln(); |
| 1167 } |
| 1168 } else { |
| 1169 if (isLastExistingDart || isLastExistingPackage) { |
| 1170 builder.writeln(); |
| 1171 } |
1110 } | 1172 } |
1111 builder.writeln(); | 1173 builder.writeln(); |
1112 builder.write("import '"); | 1174 builder.write("import '"); |
1113 builder.write(importUri); | 1175 builder.write(importUri); |
1114 builder.write("';"); | 1176 builder.write("';"); |
1115 }); | 1177 }); |
1116 } | 1178 } |
1117 if (isPackage) { | |
1118 isFirstPackage = false; | |
1119 } | |
1120 } | 1179 } |
1121 return; | 1180 return; |
1122 } | 1181 } |
1123 | 1182 |
1124 // Insert imports: after the library directive. | 1183 // Insert imports: after the library directive. |
1125 if (libraryDirective != null) { | 1184 if (libraryDirective != null) { |
1126 for (int i = 0; i < uriList.length; i++) { | 1185 for (int i = 0; i < uriList.length; i++) { |
1127 String importUri = uriList[i]; | 1186 String importUri = uriList[i]; |
1128 addInsertion(libraryDirective.end, (EditBuilder builder) { | 1187 addInsertion(libraryDirective.end, (EditBuilder builder) { |
1129 if (i == 0) { | 1188 if (i == 0) { |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1342 } | 1401 } |
1343 } | 1402 } |
1344 | 1403 |
1345 class _InsertionDescription { | 1404 class _InsertionDescription { |
1346 final int offset; | 1405 final int offset; |
1347 final bool insertEmptyLineBefore; | 1406 final bool insertEmptyLineBefore; |
1348 final bool insertEmptyLineAfter; | 1407 final bool insertEmptyLineAfter; |
1349 _InsertionDescription( | 1408 _InsertionDescription( |
1350 this.offset, this.insertEmptyLineBefore, this.insertEmptyLineAfter); | 1409 this.offset, this.insertEmptyLineBefore, this.insertEmptyLineAfter); |
1351 } | 1410 } |
OLD | NEW |