| 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 import 'dart:math'; | 5 import 'dart:math'; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol_server.dart' | 7 import 'package:analysis_server/src/protocol_server.dart' |
| 8 show doSourceChange_addElementEdit; | 8 show doSourceChange_addElementEdit; |
| 9 import 'package:analysis_server/src/services/correction/strings.dart'; | 9 import 'package:analysis_server/src/services/correction/strings.dart'; |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| (...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 if (_buffer.contains("\r\n")) { | 703 if (_buffer.contains("\r\n")) { |
| 704 _endOfLine = "\r\n"; | 704 _endOfLine = "\r\n"; |
| 705 } else { | 705 } else { |
| 706 _endOfLine = "\n"; | 706 _endOfLine = "\n"; |
| 707 } | 707 } |
| 708 } | 708 } |
| 709 return _endOfLine; | 709 return _endOfLine; |
| 710 } | 710 } |
| 711 | 711 |
| 712 /** | 712 /** |
| 713 * Returns an [Edit] that changes indentation of the source of the given | |
| 714 * [SourceRange] from [oldIndent] to [newIndent], keeping indentation of lines | |
| 715 * relative to each other. | |
| 716 */ | |
| 717 SourceEdit createIndentEdit( | |
| 718 SourceRange range, String oldIndent, String newIndent) { | |
| 719 String newSource = replaceSourceRangeIndent(range, oldIndent, newIndent); | |
| 720 return new SourceEdit(range.offset, range.length, newSource); | |
| 721 } | |
| 722 | |
| 723 /** | |
| 724 * Returns the [AstNode] that encloses the given offset. | 713 * Returns the [AstNode] that encloses the given offset. |
| 725 */ | 714 */ |
| 726 AstNode findNode(int offset) => new NodeLocator(offset).searchWithin(unit); | 715 AstNode findNode(int offset) => new NodeLocator(offset).searchWithin(unit); |
| 727 | 716 |
| 728 /** | 717 /** |
| 729 * Returns names of elements that might conflict with a new local variable | 718 * Returns names of elements that might conflict with a new local variable |
| 730 * declared at [offset]. | 719 * declared at [offset]. |
| 731 */ | 720 */ |
| 732 Set<String> findPossibleLocalVariableConflicts(int offset) { | 721 Set<String> findPossibleLocalVariableConflicts(int offset) { |
| 733 Set<String> conflicts = new Set<String>(); | 722 Set<String> conflicts = new Set<String>(); |
| 734 AstNode enclosingNode = findNode(offset); | 723 AstNode enclosingNode = findNode(offset); |
| 735 Block enclosingBlock = enclosingNode.getAncestor((node) => node is Block); | 724 Block enclosingBlock = enclosingNode.getAncestor((node) => node is Block); |
| 736 if (enclosingBlock != null) { | 725 if (enclosingBlock != null) { |
| 737 _CollectReferencedUnprefixedNames visitor = | 726 _CollectReferencedUnprefixedNames visitor = |
| 738 new _CollectReferencedUnprefixedNames(); | 727 new _CollectReferencedUnprefixedNames(); |
| 739 enclosingBlock.accept(visitor); | 728 enclosingBlock.accept(visitor); |
| 740 return visitor.names; | 729 return visitor.names; |
| 741 } | 730 } |
| 742 return conflicts; | 731 return conflicts; |
| 743 } | 732 } |
| 744 | 733 |
| 745 /** | 734 /** |
| 746 * Returns the actual type source of the given [Expression], may be `null` | |
| 747 * if can not be resolved, should be treated as the `dynamic` type. | |
| 748 */ | |
| 749 String getExpressionTypeSource( | |
| 750 Expression expression, Set<Source> librariesToImport) { | |
| 751 if (expression == null) { | |
| 752 return null; | |
| 753 } | |
| 754 DartType type = expression.bestType; | |
| 755 if (type.isDynamic) { | |
| 756 return null; | |
| 757 } | |
| 758 return getTypeSource(type, librariesToImport); | |
| 759 } | |
| 760 | |
| 761 /** | |
| 762 * Returns the indentation with the given level. | 735 * Returns the indentation with the given level. |
| 763 */ | 736 */ |
| 764 String getIndent(int level) => repeat(' ', level); | 737 String getIndent(int level) => repeat(' ', level); |
| 765 | 738 |
| 766 /** | 739 /** |
| 767 * Returns a [InsertDesc] describing where to insert a new library-related | |
| 768 * directive. | |
| 769 */ | |
| 770 CorrectionUtils_InsertDesc getInsertDescImport() { | |
| 771 // analyze directives | |
| 772 Directive prevDirective = null; | |
| 773 for (Directive directive in unit.directives) { | |
| 774 if (directive is LibraryDirective || | |
| 775 directive is ImportDirective || | |
| 776 directive is ExportDirective) { | |
| 777 prevDirective = directive; | |
| 778 } | |
| 779 } | |
| 780 // insert after last library-related directive | |
| 781 if (prevDirective != null) { | |
| 782 CorrectionUtils_InsertDesc result = new CorrectionUtils_InsertDesc(); | |
| 783 result.offset = prevDirective.end; | |
| 784 String eol = endOfLine; | |
| 785 if (prevDirective is LibraryDirective) { | |
| 786 result.prefix = "$eol$eol"; | |
| 787 } else { | |
| 788 result.prefix = eol; | |
| 789 } | |
| 790 return result; | |
| 791 } | |
| 792 // no directives, use "top" location | |
| 793 return getInsertDescTop(); | |
| 794 } | |
| 795 | |
| 796 /** | |
| 797 * Returns a [InsertDesc] describing where to insert a new 'part' directive. | |
| 798 */ | |
| 799 CorrectionUtils_InsertDesc getInsertDescPart() { | |
| 800 // analyze directives | |
| 801 Directive prevDirective = null; | |
| 802 for (Directive directive in unit.directives) { | |
| 803 prevDirective = directive; | |
| 804 } | |
| 805 // insert after last directive | |
| 806 if (prevDirective != null) { | |
| 807 CorrectionUtils_InsertDesc result = new CorrectionUtils_InsertDesc(); | |
| 808 result.offset = prevDirective.end; | |
| 809 String eol = endOfLine; | |
| 810 if (prevDirective is PartDirective) { | |
| 811 result.prefix = eol; | |
| 812 } else { | |
| 813 result.prefix = "$eol$eol"; | |
| 814 } | |
| 815 return result; | |
| 816 } | |
| 817 // no directives, use "top" location | |
| 818 return getInsertDescTop(); | |
| 819 } | |
| 820 | |
| 821 /** | |
| 822 * Returns a [InsertDesc] describing where to insert a new directive or a | 740 * Returns a [InsertDesc] describing where to insert a new directive or a |
| 823 * top-level declaration at the top of the file. | 741 * top-level declaration at the top of the file. |
| 824 */ | 742 */ |
| 825 CorrectionUtils_InsertDesc getInsertDescTop() { | 743 CorrectionUtils_InsertDesc getInsertDescTop() { |
| 826 // skip leading line comments | 744 // skip leading line comments |
| 827 int offset = 0; | 745 int offset = 0; |
| 828 bool insertEmptyLineBefore = false; | 746 bool insertEmptyLineBefore = false; |
| 829 bool insertEmptyLineAfter = false; | 747 bool insertEmptyLineAfter = false; |
| 830 String source = _buffer; | 748 String source = _buffer; |
| 831 // skip hash-bang | 749 // skip hash-bang |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 } | 943 } |
| 1026 | 944 |
| 1027 /** | 945 /** |
| 1028 * Returns the text of the given [AstNode] in the unit. | 946 * Returns the text of the given [AstNode] in the unit. |
| 1029 */ | 947 */ |
| 1030 String getNodeText(AstNode node) { | 948 String getNodeText(AstNode node) { |
| 1031 return getText(node.offset, node.length); | 949 return getText(node.offset, node.length); |
| 1032 } | 950 } |
| 1033 | 951 |
| 1034 /** | 952 /** |
| 1035 * @return the source for the parameter with the given type and name. | |
| 1036 */ | |
| 1037 String getParameterSource( | |
| 1038 DartType type, String name, Set<Source> librariesToImport) { | |
| 1039 // no type | |
| 1040 if (type == null || type.isDynamic) { | |
| 1041 return name; | |
| 1042 } | |
| 1043 // function type | |
| 1044 if (type is FunctionType && type.element.isSynthetic) { | |
| 1045 FunctionType functionType = type; | |
| 1046 StringBuffer sb = new StringBuffer(); | |
| 1047 // return type | |
| 1048 DartType returnType = functionType.returnType; | |
| 1049 if (returnType != null && !returnType.isDynamic) { | |
| 1050 String returnTypeSource = getTypeSource(returnType, librariesToImport); | |
| 1051 sb.write(returnTypeSource); | |
| 1052 sb.write(' '); | |
| 1053 } | |
| 1054 // parameter name | |
| 1055 sb.write(name); | |
| 1056 // parameters | |
| 1057 sb.write('('); | |
| 1058 List<ParameterElement> fParameters = functionType.parameters; | |
| 1059 for (int i = 0; i < fParameters.length; i++) { | |
| 1060 ParameterElement fParameter = fParameters[i]; | |
| 1061 if (i != 0) { | |
| 1062 sb.write(", "); | |
| 1063 } | |
| 1064 sb.write(getParameterSource( | |
| 1065 fParameter.type, fParameter.name, librariesToImport)); | |
| 1066 } | |
| 1067 sb.write(')'); | |
| 1068 // done | |
| 1069 return sb.toString(); | |
| 1070 } | |
| 1071 // simple type | |
| 1072 String typeSource = getTypeSource(type, librariesToImport); | |
| 1073 return '$typeSource $name'; | |
| 1074 } | |
| 1075 | |
| 1076 /** | |
| 1077 * Returns the line prefix consisting of spaces and tabs on the left from the | 953 * Returns the line prefix consisting of spaces and tabs on the left from the |
| 1078 * given offset. | 954 * given offset. |
| 1079 */ | 955 */ |
| 1080 String getPrefix(int endIndex) { | 956 String getPrefix(int endIndex) { |
| 1081 int startIndex = getLineContentStart(endIndex); | 957 int startIndex = getLineContentStart(endIndex); |
| 1082 return _buffer.substring(startIndex, endIndex); | 958 return _buffer.substring(startIndex, endIndex); |
| 1083 } | 959 } |
| 1084 | 960 |
| 1085 /** | 961 /** |
| 1086 * Returns the text of the given range in the unit. | 962 * Returns the text of the given range in the unit. |
| (...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1646 _InvertedCondition expr, int newOperatorPrecedence) { | 1522 _InvertedCondition expr, int newOperatorPrecedence) { |
| 1647 if (expr._precedence < newOperatorPrecedence) { | 1523 if (expr._precedence < newOperatorPrecedence) { |
| 1648 return "(${expr._source})"; | 1524 return "(${expr._source})"; |
| 1649 } | 1525 } |
| 1650 return expr._source; | 1526 return expr._source; |
| 1651 } | 1527 } |
| 1652 | 1528 |
| 1653 static _InvertedCondition _simple(String source) => | 1529 static _InvertedCondition _simple(String source) => |
| 1654 new _InvertedCondition(2147483647, source); | 1530 new _InvertedCondition(2147483647, source); |
| 1655 } | 1531 } |
| OLD | NEW |