| 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 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 directive is ExportDirective) { | 719 directive is ExportDirective) { |
| 720 prevDirective = directive; | 720 prevDirective = directive; |
| 721 } | 721 } |
| 722 } | 722 } |
| 723 // insert after last library-related directive | 723 // insert after last library-related directive |
| 724 if (prevDirective != null) { | 724 if (prevDirective != null) { |
| 725 CorrectionUtils_InsertDesc result = new CorrectionUtils_InsertDesc(); | 725 CorrectionUtils_InsertDesc result = new CorrectionUtils_InsertDesc(); |
| 726 result.offset = prevDirective.end; | 726 result.offset = prevDirective.end; |
| 727 String eol = endOfLine; | 727 String eol = endOfLine; |
| 728 if (prevDirective is LibraryDirective) { | 728 if (prevDirective is LibraryDirective) { |
| 729 result.prefix = "${eol}${eol}"; | 729 result.prefix = "$eol$eol"; |
| 730 } else { | 730 } else { |
| 731 result.prefix = eol; | 731 result.prefix = eol; |
| 732 } | 732 } |
| 733 return result; | 733 return result; |
| 734 } | 734 } |
| 735 // no directives, use "top" location | 735 // no directives, use "top" location |
| 736 return getInsertDescTop(); | 736 return getInsertDescTop(); |
| 737 } | 737 } |
| 738 | 738 |
| 739 /** | 739 /** |
| 740 * Returns a [InsertDesc] describing where to insert a new 'part' directive. | 740 * Returns a [InsertDesc] describing where to insert a new 'part' directive. |
| 741 */ | 741 */ |
| 742 CorrectionUtils_InsertDesc getInsertDescPart() { | 742 CorrectionUtils_InsertDesc getInsertDescPart() { |
| 743 // analyze directives | 743 // analyze directives |
| 744 Directive prevDirective = null; | 744 Directive prevDirective = null; |
| 745 for (Directive directive in unit.directives) { | 745 for (Directive directive in unit.directives) { |
| 746 prevDirective = directive; | 746 prevDirective = directive; |
| 747 } | 747 } |
| 748 // insert after last directive | 748 // insert after last directive |
| 749 if (prevDirective != null) { | 749 if (prevDirective != null) { |
| 750 CorrectionUtils_InsertDesc result = new CorrectionUtils_InsertDesc(); | 750 CorrectionUtils_InsertDesc result = new CorrectionUtils_InsertDesc(); |
| 751 result.offset = prevDirective.end; | 751 result.offset = prevDirective.end; |
| 752 String eol = endOfLine; | 752 String eol = endOfLine; |
| 753 if (prevDirective is PartDirective) { | 753 if (prevDirective is PartDirective) { |
| 754 result.prefix = eol; | 754 result.prefix = eol; |
| 755 } else { | 755 } else { |
| 756 result.prefix = "${eol}${eol}"; | 756 result.prefix = "$eol$eol"; |
| 757 } | 757 } |
| 758 return result; | 758 return result; |
| 759 } | 759 } |
| 760 // no directives, use "top" location | 760 // no directives, use "top" location |
| 761 return getInsertDescTop(); | 761 return getInsertDescTop(); |
| 762 } | 762 } |
| 763 | 763 |
| 764 /** | 764 /** |
| 765 * Returns a [InsertDesc] describing where to insert a new directive or a | 765 * Returns a [InsertDesc] describing where to insert a new directive or a |
| 766 * top-level declaration at the top of the file. | 766 * top-level declaration at the top of the file. |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1126 String eol = endOfLine; | 1126 String eol = endOfLine; |
| 1127 List<String> lines = source.split(eol); | 1127 List<String> lines = source.split(eol); |
| 1128 for (int i = 0; i < lines.length; i++) { | 1128 for (int i = 0; i < lines.length; i++) { |
| 1129 String line = lines[i]; | 1129 String line = lines[i]; |
| 1130 // last line, stop if empty | 1130 // last line, stop if empty |
| 1131 if (i == lines.length - 1 && isEmpty(line)) { | 1131 if (i == lines.length - 1 && isEmpty(line)) { |
| 1132 break; | 1132 break; |
| 1133 } | 1133 } |
| 1134 // update line | 1134 // update line |
| 1135 if (right) { | 1135 if (right) { |
| 1136 line = "${indent}${line}"; | 1136 line = "$indent$line"; |
| 1137 } else { | 1137 } else { |
| 1138 line = removeStart(line, indent); | 1138 line = removeStart(line, indent); |
| 1139 } | 1139 } |
| 1140 // append line | 1140 // append line |
| 1141 sb.write(line); | 1141 sb.write(line); |
| 1142 sb.write(eol); | 1142 sb.write(eol); |
| 1143 } | 1143 } |
| 1144 return sb.toString(); | 1144 return sb.toString(); |
| 1145 } | 1145 } |
| 1146 | 1146 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1197 if (lineOffset > lineRange.offset && lineOffset < lineRange.end) { | 1197 if (lineOffset > lineRange.offset && lineOffset < lineRange.end) { |
| 1198 inString = true; | 1198 inString = true; |
| 1199 } | 1199 } |
| 1200 if (lineOffset > lineRange.end) { | 1200 if (lineOffset > lineRange.end) { |
| 1201 break; | 1201 break; |
| 1202 } | 1202 } |
| 1203 } | 1203 } |
| 1204 lineOffset += line.length + eol.length; | 1204 lineOffset += line.length + eol.length; |
| 1205 // update line indent | 1205 // update line indent |
| 1206 if (!inString) { | 1206 if (!inString) { |
| 1207 line = "${newIndent}${removeStart(line, oldIndent)}"; | 1207 line = "$newIndent${removeStart(line, oldIndent)}"; |
| 1208 } | 1208 } |
| 1209 // append line | 1209 // append line |
| 1210 sb.write(line); | 1210 sb.write(line); |
| 1211 sb.write(eol); | 1211 sb.write(eol); |
| 1212 } | 1212 } |
| 1213 return sb.toString(); | 1213 return sb.toString(); |
| 1214 } | 1214 } |
| 1215 | 1215 |
| 1216 /** | 1216 /** |
| 1217 * Returns the source of the given [SourceRange] with indentation changed | 1217 * Returns the source of the given [SourceRange] with indentation changed |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1293 return _InvertedCondition._binary( | 1293 return _InvertedCondition._binary( |
| 1294 TokenType.AMPERSAND_AMPERSAND.precedence, ls, " && ", rs); | 1294 TokenType.AMPERSAND_AMPERSAND.precedence, ls, " && ", rs); |
| 1295 } | 1295 } |
| 1296 } | 1296 } |
| 1297 if (expression is IsExpression) { | 1297 if (expression is IsExpression) { |
| 1298 IsExpression isExpression = expression; | 1298 IsExpression isExpression = expression; |
| 1299 String expressionSource = getNodeText(isExpression.expression); | 1299 String expressionSource = getNodeText(isExpression.expression); |
| 1300 String typeSource = getNodeText(isExpression.type); | 1300 String typeSource = getNodeText(isExpression.type); |
| 1301 if (isExpression.notOperator == null) { | 1301 if (isExpression.notOperator == null) { |
| 1302 return _InvertedCondition | 1302 return _InvertedCondition |
| 1303 ._simple("${expressionSource} is! ${typeSource}"); | 1303 ._simple("$expressionSource is! $typeSource"); |
| 1304 } else { | 1304 } else { |
| 1305 return _InvertedCondition | 1305 return _InvertedCondition |
| 1306 ._simple("${expressionSource} is ${typeSource}"); | 1306 ._simple("$expressionSource is $typeSource"); |
| 1307 } | 1307 } |
| 1308 } | 1308 } |
| 1309 if (expression is PrefixExpression) { | 1309 if (expression is PrefixExpression) { |
| 1310 PrefixExpression prefixExpression = expression; | 1310 PrefixExpression prefixExpression = expression; |
| 1311 TokenType operator = prefixExpression.operator.type; | 1311 TokenType operator = prefixExpression.operator.type; |
| 1312 if (operator == TokenType.BANG) { | 1312 if (operator == TokenType.BANG) { |
| 1313 Expression operand = prefixExpression.operand; | 1313 Expression operand = prefixExpression.operand; |
| 1314 while (operand is ParenthesizedExpression) { | 1314 while (operand is ParenthesizedExpression) { |
| 1315 ParenthesizedExpression pe = operand as ParenthesizedExpression; | 1315 ParenthesizedExpression pe = operand as ParenthesizedExpression; |
| 1316 operand = pe.expression; | 1316 operand = pe.expression; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1453 String src = _parenthesizeIfRequired(left, precedence) + | 1453 String src = _parenthesizeIfRequired(left, precedence) + |
| 1454 operation + | 1454 operation + |
| 1455 _parenthesizeIfRequired(right, precedence); | 1455 _parenthesizeIfRequired(right, precedence); |
| 1456 return new _InvertedCondition(precedence, src); | 1456 return new _InvertedCondition(precedence, src); |
| 1457 } | 1457 } |
| 1458 | 1458 |
| 1459 static _InvertedCondition _binary2( | 1459 static _InvertedCondition _binary2( |
| 1460 _InvertedCondition left, String operation, _InvertedCondition right) { | 1460 _InvertedCondition left, String operation, _InvertedCondition right) { |
| 1461 // TODO(scheglov) consider merging with "_binary()" after testing | 1461 // TODO(scheglov) consider merging with "_binary()" after testing |
| 1462 return new _InvertedCondition( | 1462 return new _InvertedCondition( |
| 1463 1 << 20, "${left._source}${operation}${right._source}"); | 1463 1 << 20, "${left._source}$operation${right._source}"); |
| 1464 } | 1464 } |
| 1465 | 1465 |
| 1466 /** | 1466 /** |
| 1467 * Adds enclosing parenthesis if the precedence of the [_InvertedCondition] if
less than the | 1467 * Adds enclosing parenthesis if the precedence of the [_InvertedCondition] if
less than the |
| 1468 * precedence of the expression we are going it to use in. | 1468 * precedence of the expression we are going it to use in. |
| 1469 */ | 1469 */ |
| 1470 static String _parenthesizeIfRequired( | 1470 static String _parenthesizeIfRequired( |
| 1471 _InvertedCondition expr, int newOperatorPrecedence) { | 1471 _InvertedCondition expr, int newOperatorPrecedence) { |
| 1472 if (expr._precedence < newOperatorPrecedence) { | 1472 if (expr._precedence < newOperatorPrecedence) { |
| 1473 return "(${expr._source})"; | 1473 return "(${expr._source})"; |
| 1474 } | 1474 } |
| 1475 return expr._source; | 1475 return expr._source; |
| 1476 } | 1476 } |
| 1477 | 1477 |
| 1478 static _InvertedCondition _simple(String source) => | 1478 static _InvertedCondition _simple(String source) => |
| 1479 new _InvertedCondition(2147483647, source); | 1479 new _InvertedCondition(2147483647, source); |
| 1480 } | 1480 } |
| OLD | NEW |