Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/src/protocol.dart' show SourceChange, | 9 import 'package:analysis_server/src/protocol.dart' show SourceChange, |
| 10 SourceEdit; | 10 SourceEdit; |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 return parent; | 527 return parent; |
| 528 } | 528 } |
| 529 } | 529 } |
| 530 return expression; | 530 return expression; |
| 531 } | 531 } |
| 532 | 532 |
| 533 | 533 |
| 534 class CorrectionUtils { | 534 class CorrectionUtils { |
| 535 final CompilationUnit unit; | 535 final CompilationUnit unit; |
| 536 | 536 |
| 537 /** | |
| 538 * The [ClassElement] the generated code is inserted to, so we can decide if | |
| 539 * a type parameter may or may not be used. | |
| 540 */ | |
| 541 ClassElement targetClassElement; | |
| 542 | |
| 537 LibraryElement _library; | 543 LibraryElement _library; |
| 538 String _buffer; | 544 String _buffer; |
| 539 String _endOfLine; | 545 String _endOfLine; |
| 540 | 546 |
| 541 CorrectionUtils(this.unit) { | 547 CorrectionUtils(this.unit) { |
| 542 CompilationUnitElement unitElement = unit.element; | 548 CompilationUnitElement unitElement = unit.element; |
| 543 this._library = unitElement.library; | 549 this._library = unitElement.library; |
| 544 this._buffer = unitElement.context.getContents(unitElement.source).data; | 550 this._buffer = unitElement.context.getContents(unitElement.source).data; |
| 545 } | 551 } |
| 546 | 552 |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 870 if (type == null || type.isDynamic) { | 876 if (type == null || type.isDynamic) { |
| 871 return name; | 877 return name; |
| 872 } | 878 } |
| 873 // function type | 879 // function type |
| 874 if (type is FunctionType) { | 880 if (type is FunctionType) { |
| 875 FunctionType functionType = type; | 881 FunctionType functionType = type; |
| 876 StringBuffer sb = new StringBuffer(); | 882 StringBuffer sb = new StringBuffer(); |
| 877 // return type | 883 // return type |
| 878 DartType returnType = functionType.returnType; | 884 DartType returnType = functionType.returnType; |
| 879 if (returnType != null && !returnType.isDynamic) { | 885 if (returnType != null && !returnType.isDynamic) { |
| 880 sb.write(getTypeSource(returnType, librariesToImport)); | 886 String returnTypeSource = getTypeSource(returnType, librariesToImport); |
| 887 sb.write(returnTypeSource); | |
| 881 sb.write(' '); | 888 sb.write(' '); |
| 882 } | 889 } |
| 883 // parameter name | 890 // parameter name |
| 884 sb.write(name); | 891 sb.write(name); |
| 885 // parameters | 892 // parameters |
| 886 sb.write('('); | 893 sb.write('('); |
| 887 List<ParameterElement> fParameters = functionType.parameters; | 894 List<ParameterElement> fParameters = functionType.parameters; |
| 888 for (int i = 0; i < fParameters.length; i++) { | 895 for (int i = 0; i < fParameters.length; i++) { |
| 889 ParameterElement fParameter = fParameters[i]; | 896 ParameterElement fParameter = fParameters[i]; |
| 890 if (i != 0) { | 897 if (i != 0) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 958 } | 965 } |
| 959 } | 966 } |
| 960 // append simple name | 967 // append simple name |
| 961 String name = element.displayName; | 968 String name = element.displayName; |
| 962 sb.write(name); | 969 sb.write(name); |
| 963 // may be type arguments | 970 // may be type arguments |
| 964 if (type is ParameterizedType) { | 971 if (type is ParameterizedType) { |
| 965 List<DartType> arguments = type.typeArguments; | 972 List<DartType> arguments = type.typeArguments; |
| 966 // check if has arguments | 973 // check if has arguments |
| 967 bool hasArguments = false; | 974 bool hasArguments = false; |
| 975 bool allArgumentsVisible = true; | |
| 968 for (DartType argument in arguments) { | 976 for (DartType argument in arguments) { |
| 969 if (!argument.isDynamic) { | 977 hasArguments = hasArguments || !argument.isDynamic; |
| 970 hasArguments = true; | 978 allArgumentsVisible = allArgumentsVisible && _isTypeVisible(argument); |
| 971 break; | |
| 972 } | |
| 973 } | 979 } |
| 974 // append type arguments | 980 // append type arguments |
| 975 if (hasArguments) { | 981 if (hasArguments && allArgumentsVisible) { |
| 976 sb.write("<"); | 982 sb.write("<"); |
| 977 for (int i = 0; i < arguments.length; i++) { | 983 for (int i = 0; i < arguments.length; i++) { |
| 978 DartType argument = arguments[i]; | 984 DartType argument = arguments[i]; |
| 979 if (i != 0) { | 985 if (i != 0) { |
| 980 sb.write(", "); | 986 sb.write(", "); |
| 981 } | 987 } |
| 982 String argumentSrc = getTypeSource(argument, librariesToImport); | 988 String argumentSrc = getTypeSource(argument, librariesToImport); |
| 983 sb.write(argumentSrc); | 989 sb.write(argumentSrc); |
| 984 } | 990 } |
| 985 sb.write(">"); | 991 sb.write(">"); |
| 986 } | 992 } |
| 987 } | 993 } |
| 988 // done | 994 // done |
| 989 return sb.toString(); | 995 return sb.toString(); |
| 990 } | 996 } |
| 991 | 997 |
| 992 /** | 998 /** |
| 999 * Checks if [type] is visible at [targetOffset]. | |
| 1000 */ | |
| 1001 bool _isTypeVisible(DartType type) { | |
| 1002 // TODO(scheglov) | |
|
Brian Wilkerson
2014/12/15 23:29:15
What's left to do?
| |
| 1003 if (type is TypeParameterType) { | |
| 1004 TypeParameterElement parameterElement = type.element; | |
| 1005 Element parameterClassElement = parameterElement.enclosingElement; | |
| 1006 return identical(parameterClassElement, targetClassElement); | |
| 1007 } | |
| 1008 return true; | |
| 1009 } | |
| 1010 | |
| 1011 /** | |
| 993 * Indents given source left or right. | 1012 * Indents given source left or right. |
| 994 */ | 1013 */ |
| 995 String indentSourceLeftRight(String source, bool right) { | 1014 String indentSourceLeftRight(String source, bool right) { |
| 996 StringBuffer sb = new StringBuffer(); | 1015 StringBuffer sb = new StringBuffer(); |
| 997 String indent = getIndent(1); | 1016 String indent = getIndent(1); |
| 998 String eol = endOfLine; | 1017 String eol = endOfLine; |
| 999 List<String> lines = source.split(eol); | 1018 List<String> lines = source.split(eol); |
| 1000 for (int i = 0; i < lines.length; i++) { | 1019 for (int i = 0; i < lines.length; i++) { |
| 1001 String line = lines[i]; | 1020 String line = lines[i]; |
| 1002 // last line, stop if empty | 1021 // last line, stop if empty |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1450 | 1469 |
| 1451 @override | 1470 @override |
| 1452 Object visitExpression(Expression node) { | 1471 Object visitExpression(Expression node) { |
| 1453 if (node is BinaryExpression && node.operator.type == groupOperatorType) { | 1472 if (node is BinaryExpression && node.operator.type == groupOperatorType) { |
| 1454 return super.visitNode(node); | 1473 return super.visitNode(node); |
| 1455 } | 1474 } |
| 1456 operands.add(node); | 1475 operands.add(node); |
| 1457 return null; | 1476 return null; |
| 1458 } | 1477 } |
| 1459 } | 1478 } |
| OLD | NEW |