Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: pkg/analysis_server/lib/src/services/correction/util.dart

Issue 2532393008: Add libraries in corrections using SourceInfo instance of LibraryElement. (Closed)
Patch Set: Replace SourceInfo with Source. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 library services.src.correction.util; 5 library services.src.correction.util;
6 6
7 import 'dart:math'; 7 import 'dart:math';
8 8
9 import 'package:analysis_server/plugin/protocol/protocol.dart' 9 import 'package:analysis_server/plugin/protocol/protocol.dart'
10 show SourceChange, SourceEdit; 10 show SourceChange, SourceEdit;
(...skipping 11 matching lines...) Expand all
22 import 'package:analyzer/src/dart/scanner/scanner.dart'; 22 import 'package:analyzer/src/dart/scanner/scanner.dart';
23 import 'package:analyzer/src/generated/engine.dart'; 23 import 'package:analyzer/src/generated/engine.dart';
24 import 'package:analyzer/src/generated/resolver.dart'; 24 import 'package:analyzer/src/generated/resolver.dart';
25 import 'package:analyzer/src/generated/source.dart'; 25 import 'package:analyzer/src/generated/source.dart';
26 import 'package:path/path.dart'; 26 import 'package:path/path.dart';
27 27
28 /** 28 /**
29 * Adds edits to the given [change] that ensure that all the [libraries] are 29 * Adds edits to the given [change] that ensure that all the [libraries] are
30 * imported into the given [targetLibrary]. 30 * imported into the given [targetLibrary].
31 */ 31 */
32 void addLibraryImports(SourceChange change, LibraryElement targetLibrary, 32 void addLibraryImports(
33 Set<LibraryElement> libraries) { 33 SourceChange change, LibraryElement targetLibrary, Set<Source> libraries) {
34 CorrectionUtils libUtils; 34 CorrectionUtils libUtils;
35 try { 35 try {
36 CompilationUnitElement unitElement = targetLibrary.definingCompilationUnit; 36 CompilationUnitElement unitElement = targetLibrary.definingCompilationUnit;
37 CompilationUnit unitAst = getParsedUnit(unitElement); 37 CompilationUnit unitAst = getParsedUnit(unitElement);
38 libUtils = new CorrectionUtils(unitAst); 38 libUtils = new CorrectionUtils(unitAst);
39 } catch (e) { 39 } catch (e) {
40 throw new CancelCorrectionException(exception: e); 40 throw new CancelCorrectionException(exception: e);
41 } 41 }
42 String eol = libUtils.endOfLine; 42 String eol = libUtils.endOfLine;
43 // Prepare information about existing imports. 43 // Prepare information about existing imports.
44 LibraryDirective libraryDirective; 44 LibraryDirective libraryDirective;
45 List<_ImportDirectiveInfo> importDirectives = <_ImportDirectiveInfo>[]; 45 List<_ImportDirectiveInfo> importDirectives = <_ImportDirectiveInfo>[];
46 for (Directive directive in libUtils.unit.directives) { 46 for (Directive directive in libUtils.unit.directives) {
47 if (directive is LibraryDirective) { 47 if (directive is LibraryDirective) {
48 libraryDirective = directive; 48 libraryDirective = directive;
49 } else if (directive is ImportDirective) { 49 } else if (directive is ImportDirective) {
50 importDirectives.add(new _ImportDirectiveInfo( 50 importDirectives.add(new _ImportDirectiveInfo(
51 directive.uriContent, directive.offset, directive.end)); 51 directive.uriContent, directive.offset, directive.end));
52 } 52 }
53 } 53 }
54 54
55 // Prepare all URIs to import. 55 // Prepare all URIs to import.
56 List<String> uriList = libraries 56 List<String> uriList = libraries
57 .map((library) => getLibrarySourceUri(targetLibrary, library.source)) 57 .map((library) => getLibrarySourceUri(targetLibrary, library))
58 .toList(); 58 .toList();
59 uriList.sort((a, b) => a.compareTo(b)); 59 uriList.sort((a, b) => a.compareTo(b));
60 60
61 // Insert imports: between existing imports. 61 // Insert imports: between existing imports.
62 if (importDirectives.isNotEmpty) { 62 if (importDirectives.isNotEmpty) {
63 bool isFirstPackage = true; 63 bool isFirstPackage = true;
64 for (String importUri in uriList) { 64 for (String importUri in uriList) {
65 bool inserted = false; 65 bool inserted = false;
66 bool isPackage = importUri.startsWith('package:'); 66 bool isPackage = importUri.startsWith('package:');
67 bool isAfterDart = false; 67 bool isAfterDart = false;
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 Map<String, Element> getImportNamespace(ImportElement imp) { 360 Map<String, Element> getImportNamespace(ImportElement imp) {
361 NamespaceBuilder builder = new NamespaceBuilder(); 361 NamespaceBuilder builder = new NamespaceBuilder();
362 Namespace namespace = builder.createImportNamespaceForDirective(imp); 362 Namespace namespace = builder.createImportNamespaceForDirective(imp);
363 return namespace.definedNames; 363 return namespace.definedNames;
364 } 364 }
365 365
366 /** 366 /**
367 * Computes the best URI to import [what] into [from]. 367 * Computes the best URI to import [what] into [from].
368 */ 368 */
369 String getLibrarySourceUri(LibraryElement from, Source what) { 369 String getLibrarySourceUri(LibraryElement from, Source what) {
370 String whatFile = what.fullName; 370 String whatPath = what.fullName;
371 // check if an absolute URI (such as 'dart:' or 'package:') 371 // check if an absolute URI (such as 'dart:' or 'package:')
372 Uri whatUri = what.uri; 372 Uri whatUri = what.uri;
373 String whatUriScheme = whatUri.scheme; 373 String whatUriScheme = whatUri.scheme;
374 if (whatUriScheme != '' && whatUriScheme != 'file') { 374 if (whatUriScheme != '' && whatUriScheme != 'file') {
375 return whatUri.toString(); 375 return whatUri.toString();
376 } 376 }
377 // compute a relative URI 377 // compute a relative URI
378 String fromFolder = dirname(from.source.fullName); 378 String fromFolder = dirname(from.source.fullName);
379 String relativeFile = relative(whatFile, from: fromFolder); 379 String relativeFile = relative(whatPath, from: fromFolder);
380 return split(relativeFile).join('/'); 380 return split(relativeFile).join('/');
381 } 381 }
382 382
383 /** 383 /**
384 * Returns the line prefix from the given source, i.e. basically just a 384 * Returns the line prefix from the given source, i.e. basically just a
385 * whitespace prefix of the given [String]. 385 * whitespace prefix of the given [String].
386 */ 386 */
387 String getLinePrefix(String line) { 387 String getLinePrefix(String line) {
388 int index = 0; 388 int index = 0;
389 while (index < line.length) { 389 while (index < line.length) {
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 return visitor.names; 758 return visitor.names;
759 } 759 }
760 return conflicts; 760 return conflicts;
761 } 761 }
762 762
763 /** 763 /**
764 * Returns the actual type source of the given [Expression], may be `null` 764 * Returns the actual type source of the given [Expression], may be `null`
765 * if can not be resolved, should be treated as the `dynamic` type. 765 * if can not be resolved, should be treated as the `dynamic` type.
766 */ 766 */
767 String getExpressionTypeSource( 767 String getExpressionTypeSource(
768 Expression expression, Set<LibraryElement> librariesToImport) { 768 Expression expression, Set<Source> librariesToImport) {
769 if (expression == null) { 769 if (expression == null) {
770 return null; 770 return null;
771 } 771 }
772 DartType type = expression.bestType; 772 DartType type = expression.bestType;
773 if (type.isDynamic) { 773 if (type.isDynamic) {
774 return null; 774 return null;
775 } 775 }
776 return getTypeSource(type, librariesToImport); 776 return getTypeSource(type, librariesToImport);
777 } 777 }
778 778
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
1047 * Returns the text of the given [AstNode] in the unit. 1047 * Returns the text of the given [AstNode] in the unit.
1048 */ 1048 */
1049 String getNodeText(AstNode node) { 1049 String getNodeText(AstNode node) {
1050 return getText(node.offset, node.length); 1050 return getText(node.offset, node.length);
1051 } 1051 }
1052 1052
1053 /** 1053 /**
1054 * @return the source for the parameter with the given type and name. 1054 * @return the source for the parameter with the given type and name.
1055 */ 1055 */
1056 String getParameterSource( 1056 String getParameterSource(
1057 DartType type, String name, Set<LibraryElement> librariesToImport) { 1057 DartType type, String name, Set<Source> librariesToImport) {
1058 // no type 1058 // no type
1059 if (type == null || type.isDynamic) { 1059 if (type == null || type.isDynamic) {
1060 return name; 1060 return name;
1061 } 1061 }
1062 // function type 1062 // function type
1063 if (type is FunctionType && type.element.isSynthetic) { 1063 if (type is FunctionType && type.element.isSynthetic) {
1064 FunctionType functionType = type; 1064 FunctionType functionType = type;
1065 StringBuffer sb = new StringBuffer(); 1065 StringBuffer sb = new StringBuffer();
1066 // return type 1066 // return type
1067 DartType returnType = functionType.returnType; 1067 DartType returnType = functionType.returnType;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1114 String getText(int offset, int length) { 1114 String getText(int offset, int length) {
1115 return _buffer.substring(offset, offset + length); 1115 return _buffer.substring(offset, offset + length);
1116 } 1116 }
1117 1117
1118 /** 1118 /**
1119 * Returns the source to reference [type] in this [CompilationUnit]. 1119 * Returns the source to reference [type] in this [CompilationUnit].
1120 * 1120 *
1121 * Fills [librariesToImport] with [LibraryElement]s whose elements are 1121 * Fills [librariesToImport] with [LibraryElement]s whose elements are
1122 * used by the generated source, but not imported. 1122 * used by the generated source, but not imported.
1123 */ 1123 */
1124 String getTypeSource(DartType type, Set<LibraryElement> librariesToImport, 1124 String getTypeSource(DartType type, Set<Source> librariesToImport,
1125 {StringBuffer parametersBuffer}) { 1125 {StringBuffer parametersBuffer}) {
1126 StringBuffer sb = new StringBuffer(); 1126 StringBuffer sb = new StringBuffer();
1127 // type parameter 1127 // type parameter
1128 if (!_isTypeVisible(type)) { 1128 if (!_isTypeVisible(type)) {
1129 return 'dynamic'; 1129 return 'dynamic';
1130 } 1130 }
1131 // just a Function, not FunctionTypeAliasElement 1131 // just a Function, not FunctionTypeAliasElement
1132 if (type is FunctionType && type.element is! FunctionTypeAliasElement) { 1132 if (type is FunctionType && type.element is! FunctionTypeAliasElement) {
1133 if (parametersBuffer == null) { 1133 if (parametersBuffer == null) {
1134 return "Function"; 1134 return "Function";
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1166 return null; 1166 return null;
1167 } 1167 }
1168 // ensure import 1168 // ensure import
1169 ImportElement importElement = _getImportElement(element); 1169 ImportElement importElement = _getImportElement(element);
1170 if (importElement != null) { 1170 if (importElement != null) {
1171 if (importElement.prefix != null) { 1171 if (importElement.prefix != null) {
1172 sb.write(importElement.prefix.displayName); 1172 sb.write(importElement.prefix.displayName);
1173 sb.write("."); 1173 sb.write(".");
1174 } 1174 }
1175 } else { 1175 } else {
1176 librariesToImport.add(library); 1176 librariesToImport.add(library.source);
1177 } 1177 }
1178 } 1178 }
1179 // append simple name 1179 // append simple name
1180 String name = element.displayName; 1180 String name = element.displayName;
1181 sb.write(name); 1181 sb.write(name);
1182 // may be type arguments 1182 // may be type arguments
1183 if (type is ParameterizedType) { 1183 if (type is ParameterizedType) {
1184 List<DartType> arguments = type.typeArguments; 1184 List<DartType> arguments = type.typeArguments;
1185 // check if has arguments 1185 // check if has arguments
1186 bool hasArguments = false; 1186 bool hasArguments = false;
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1661 _InvertedCondition expr, int newOperatorPrecedence) { 1661 _InvertedCondition expr, int newOperatorPrecedence) {
1662 if (expr._precedence < newOperatorPrecedence) { 1662 if (expr._precedence < newOperatorPrecedence) {
1663 return "(${expr._source})"; 1663 return "(${expr._source})";
1664 } 1664 }
1665 return expr._source; 1665 return expr._source;
1666 } 1666 }
1667 1667
1668 static _InvertedCondition _simple(String source) => 1668 static _InvertedCondition _simple(String source) =>
1669 new _InvertedCondition(2147483647, source); 1669 new _InvertedCondition(2147483647, source);
1670 } 1670 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698