| 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/plugin/protocol/protocol.dart' | 9 import 'package:analysis_server/plugin/protocol/protocol.dart' |
| 10 show SourceChange, SourceEdit; | 10 show SourceChange, SourceEdit; |
| (...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 | 657 |
| 658 /** | 658 /** |
| 659 * Returns names of elements that might conflict with a new local variable | 659 * Returns names of elements that might conflict with a new local variable |
| 660 * declared at [offset]. | 660 * declared at [offset]. |
| 661 */ | 661 */ |
| 662 Set<String> findPossibleLocalVariableConflicts(int offset) { | 662 Set<String> findPossibleLocalVariableConflicts(int offset) { |
| 663 Set<String> conflicts = new Set<String>(); | 663 Set<String> conflicts = new Set<String>(); |
| 664 AstNode enclosingNode = findNode(offset); | 664 AstNode enclosingNode = findNode(offset); |
| 665 Block enclosingBlock = enclosingNode.getAncestor((node) => node is Block); | 665 Block enclosingBlock = enclosingNode.getAncestor((node) => node is Block); |
| 666 if (enclosingBlock != null) { | 666 if (enclosingBlock != null) { |
| 667 _CollectReferencedUnprefixedNames visitor = new _CollectReferencedUnprefix
edNames(); | 667 _CollectReferencedUnprefixedNames visitor = |
| 668 new _CollectReferencedUnprefixedNames(); |
| 668 enclosingBlock.accept(visitor); | 669 enclosingBlock.accept(visitor); |
| 669 return visitor.names; | 670 return visitor.names; |
| 670 } | 671 } |
| 671 return conflicts; | 672 return conflicts; |
| 672 } | 673 } |
| 673 | 674 |
| 674 /** | 675 /** |
| 675 * Returns the actual type source of the given [Expression], may be `null` | 676 * Returns the actual type source of the given [Expression], may be `null` |
| 676 * if can not be resolved, should be treated as the `dynamic` type. | 677 * if can not be resolved, should be treated as the `dynamic` type. |
| 677 */ | 678 */ |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 } | 897 } |
| 897 lineNonWhitespace++; | 898 lineNonWhitespace++; |
| 898 } | 899 } |
| 899 return getText(lineStart, lineNonWhitespace - lineStart); | 900 return getText(lineStart, lineNonWhitespace - lineStart); |
| 900 } | 901 } |
| 901 | 902 |
| 902 /** | 903 /** |
| 903 * Returns a [SourceRange] that covers [range] and extends (if possible) to | 904 * Returns a [SourceRange] that covers [range] and extends (if possible) to |
| 904 * cover whole lines. | 905 * cover whole lines. |
| 905 */ | 906 */ |
| 906 SourceRange getLinesRange(SourceRange range) { | 907 SourceRange getLinesRange(SourceRange range, |
| 908 {bool skipLeadingEmptyLines: false}) { |
| 907 // start | 909 // start |
| 908 int startOffset = range.offset; | 910 int startOffset = range.offset; |
| 909 int startLineOffset = getLineContentStart(startOffset); | 911 int startLineOffset = getLineContentStart(startOffset); |
| 912 if (skipLeadingEmptyLines) { |
| 913 startLineOffset = skipEmptyLinesLeft(startLineOffset); |
| 914 } |
| 910 // end | 915 // end |
| 911 int endOffset = range.end; | 916 int endOffset = range.end; |
| 912 int afterEndLineOffset = getLineContentEnd(endOffset); | 917 int afterEndLineOffset = getLineContentEnd(endOffset); |
| 913 // range | 918 // range |
| 914 return rangeStartEnd(startLineOffset, afterEndLineOffset); | 919 return rangeStartEnd(startLineOffset, afterEndLineOffset); |
| 915 } | 920 } |
| 916 | 921 |
| 917 /** | 922 /** |
| 918 * Returns a [SourceRange] that covers all the given [Statement]s. | 923 * Returns a [SourceRange] that covers all the given [Statement]s. |
| 919 */ | 924 */ |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1214 * @return <code>true</code> if "selection" covers "node" and there are any no
n-whitespace tokens | 1219 * @return <code>true</code> if "selection" covers "node" and there are any no
n-whitespace tokens |
| 1215 * between "selection" and "node" start/end. | 1220 * between "selection" and "node" start/end. |
| 1216 */ | 1221 */ |
| 1217 bool selectionIncludesNonWhitespaceOutsideNode( | 1222 bool selectionIncludesNonWhitespaceOutsideNode( |
| 1218 SourceRange selection, AstNode node) { | 1223 SourceRange selection, AstNode node) { |
| 1219 return _selectionIncludesNonWhitespaceOutsideRange( | 1224 return _selectionIncludesNonWhitespaceOutsideRange( |
| 1220 selection, rangeNode(node)); | 1225 selection, rangeNode(node)); |
| 1221 } | 1226 } |
| 1222 | 1227 |
| 1223 /** | 1228 /** |
| 1229 * Skip spaces, tabs and EOLs on the left from [index]. |
| 1230 * |
| 1231 * If [index] is the start of a method, then in the most cases return the end |
| 1232 * of the previous not-whitespace line. |
| 1233 */ |
| 1234 int skipEmptyLinesLeft(int index) { |
| 1235 int lastLine = index; |
| 1236 while (index > 0) { |
| 1237 int c = _buffer.codeUnitAt(index - 1); |
| 1238 if (!isWhitespace(c)) { |
| 1239 return lastLine; |
| 1240 } |
| 1241 if (isEOL(c)) { |
| 1242 lastLine = index; |
| 1243 } |
| 1244 index--; |
| 1245 } |
| 1246 return 0; |
| 1247 } |
| 1248 |
| 1249 /** |
| 1224 * @return the [ImportElement] used to import given [Element] into [library]. | 1250 * @return the [ImportElement] used to import given [Element] into [library]. |
| 1225 * May be `null` if was not imported, i.e. declared in the same librar
y. | 1251 * May be `null` if was not imported, i.e. declared in the same librar
y. |
| 1226 */ | 1252 */ |
| 1227 ImportElement _getImportElement(Element element) { | 1253 ImportElement _getImportElement(Element element) { |
| 1228 for (ImportElement imp in _library.imports) { | 1254 for (ImportElement imp in _library.imports) { |
| 1229 Map<String, Element> definedNames = getImportNamespace(imp); | 1255 Map<String, Element> definedNames = getImportNamespace(imp); |
| 1230 if (definedNames.containsValue(element)) { | 1256 if (definedNames.containsValue(element)) { |
| 1231 return imp; | 1257 return imp; |
| 1232 } | 1258 } |
| 1233 } | 1259 } |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1475 _InvertedCondition expr, int newOperatorPrecedence) { | 1501 _InvertedCondition expr, int newOperatorPrecedence) { |
| 1476 if (expr._precedence < newOperatorPrecedence) { | 1502 if (expr._precedence < newOperatorPrecedence) { |
| 1477 return "(${expr._source})"; | 1503 return "(${expr._source})"; |
| 1478 } | 1504 } |
| 1479 return expr._source; | 1505 return expr._source; |
| 1480 } | 1506 } |
| 1481 | 1507 |
| 1482 static _InvertedCondition _simple(String source) => | 1508 static _InvertedCondition _simple(String source) => |
| 1483 new _InvertedCondition(2147483647, source); | 1509 new _InvertedCondition(2147483647, source); |
| 1484 } | 1510 } |
| OLD | NEW |