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

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

Issue 1925173002: Add a (public) utility method to clean-up code (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 months 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 1266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1277 } 1277 }
1278 } 1278 }
1279 return null; 1279 return null;
1280 } 1280 }
1281 1281
1282 /** 1282 /**
1283 * @return the [InvertedCondition] for the given logical expression. 1283 * @return the [InvertedCondition] for the given logical expression.
1284 */ 1284 */
1285 _InvertedCondition _invertCondition0(Expression expression) { 1285 _InvertedCondition _invertCondition0(Expression expression) {
1286 if (expression is BooleanLiteral) { 1286 if (expression is BooleanLiteral) {
1287 BooleanLiteral literal = expression; 1287 if (expression.value) {
1288 if (literal.value) {
1289 return _InvertedCondition._simple("false"); 1288 return _InvertedCondition._simple("false");
1290 } else { 1289 } else {
1291 return _InvertedCondition._simple("true"); 1290 return _InvertedCondition._simple("true");
1292 } 1291 }
1293 } 1292 } else if (expression is BinaryExpression) {
1294 if (expression is BinaryExpression) { 1293 TokenType operator = expression.operator.type;
1295 BinaryExpression binary = expression; 1294 Expression le = expression.leftOperand;
1296 TokenType operator = binary.operator.type; 1295 Expression re = expression.rightOperand;
1297 Expression le = binary.leftOperand;
1298 Expression re = binary.rightOperand;
1299 _InvertedCondition ls = _invertCondition0(le); 1296 _InvertedCondition ls = _invertCondition0(le);
1300 _InvertedCondition rs = _invertCondition0(re); 1297 _InvertedCondition rs = _invertCondition0(re);
1301 if (operator == TokenType.LT) { 1298 if (operator == TokenType.LT) {
1302 return _InvertedCondition._binary2(ls, " >= ", rs); 1299 return _InvertedCondition._binary2(ls, " >= ", rs);
1303 } 1300 }
1304 if (operator == TokenType.GT) { 1301 if (operator == TokenType.GT) {
1305 return _InvertedCondition._binary2(ls, " <= ", rs); 1302 return _InvertedCondition._binary2(ls, " <= ", rs);
1306 } 1303 }
1307 if (operator == TokenType.LT_EQ) { 1304 if (operator == TokenType.LT_EQ) {
1308 return _InvertedCondition._binary2(ls, " > ", rs); 1305 return _InvertedCondition._binary2(ls, " > ", rs);
1309 } 1306 }
1310 if (operator == TokenType.GT_EQ) { 1307 if (operator == TokenType.GT_EQ) {
1311 return _InvertedCondition._binary2(ls, " < ", rs); 1308 return _InvertedCondition._binary2(ls, " < ", rs);
1312 } 1309 }
1313 if (operator == TokenType.EQ_EQ) { 1310 if (operator == TokenType.EQ_EQ) {
1314 return _InvertedCondition._binary2(ls, " != ", rs); 1311 return _InvertedCondition._binary2(ls, " != ", rs);
1315 } 1312 }
1316 if (operator == TokenType.BANG_EQ) { 1313 if (operator == TokenType.BANG_EQ) {
1317 return _InvertedCondition._binary2(ls, " == ", rs); 1314 return _InvertedCondition._binary2(ls, " == ", rs);
1318 } 1315 }
1319 if (operator == TokenType.AMPERSAND_AMPERSAND) { 1316 if (operator == TokenType.AMPERSAND_AMPERSAND) {
1320 return _InvertedCondition._binary( 1317 return _InvertedCondition._binary(
1321 TokenType.BAR_BAR.precedence, ls, " || ", rs); 1318 TokenType.BAR_BAR.precedence, ls, " || ", rs);
1322 } 1319 }
1323 if (operator == TokenType.BAR_BAR) { 1320 if (operator == TokenType.BAR_BAR) {
1324 return _InvertedCondition._binary( 1321 return _InvertedCondition._binary(
1325 TokenType.AMPERSAND_AMPERSAND.precedence, ls, " && ", rs); 1322 TokenType.AMPERSAND_AMPERSAND.precedence, ls, " && ", rs);
1326 } 1323 }
1327 } 1324 } else if (expression is IsExpression) {
1328 if (expression is IsExpression) { 1325 String expressionSource = getNodeText(expression.expression);
1329 IsExpression isExpression = expression; 1326 String typeSource = getNodeText(expression.type);
1330 String expressionSource = getNodeText(isExpression.expression); 1327 if (expression.notOperator == null) {
1331 String typeSource = getNodeText(isExpression.type);
1332 if (isExpression.notOperator == null) {
1333 return _InvertedCondition._simple("$expressionSource is! $typeSource"); 1328 return _InvertedCondition._simple("$expressionSource is! $typeSource");
1334 } else { 1329 } else {
1335 return _InvertedCondition._simple("$expressionSource is $typeSource"); 1330 return _InvertedCondition._simple("$expressionSource is $typeSource");
1336 } 1331 }
1337 } 1332 } else if (expression is PrefixExpression) {
1338 if (expression is PrefixExpression) { 1333 TokenType operator = expression.operator.type;
1339 PrefixExpression prefixExpression = expression;
1340 TokenType operator = prefixExpression.operator.type;
1341 if (operator == TokenType.BANG) { 1334 if (operator == TokenType.BANG) {
1342 Expression operand = prefixExpression.operand; 1335 Expression operand = expression.operand.unParenthesized;
1343 while (operand is ParenthesizedExpression) {
1344 ParenthesizedExpression pe = operand as ParenthesizedExpression;
1345 operand = pe.expression;
1346 }
1347 return _InvertedCondition._simple(getNodeText(operand)); 1336 return _InvertedCondition._simple(getNodeText(operand));
1348 } 1337 }
1349 } 1338 } else if (expression is ParenthesizedExpression) {
1350 if (expression is ParenthesizedExpression) { 1339 return _invertCondition0(expression.unParenthesized);
1351 ParenthesizedExpression pe = expression;
1352 Expression innerExpression = pe.expression;
1353 while (innerExpression is ParenthesizedExpression) {
1354 innerExpression =
1355 (innerExpression as ParenthesizedExpression).expression;
1356 }
1357 return _invertCondition0(innerExpression);
1358 } 1340 }
1359 DartType type = expression.bestType; 1341 DartType type = expression.bestType;
1360 if (type.displayName == "bool") { 1342 if (type.displayName == "bool") {
1361 return _InvertedCondition._simple("!${getNodeText(expression)}"); 1343 return _InvertedCondition._simple("!${getNodeText(expression)}");
1362 } 1344 }
1363 return _InvertedCondition._simple(getNodeText(expression)); 1345 return _InvertedCondition._simple(getNodeText(expression));
1364 } 1346 }
1365 1347
1366 /** 1348 /**
1367 * Checks if [type] is visible at [targetOffset]. 1349 * Checks if [type] is visible at [targetOffset].
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1517 _InvertedCondition expr, int newOperatorPrecedence) { 1499 _InvertedCondition expr, int newOperatorPrecedence) {
1518 if (expr._precedence < newOperatorPrecedence) { 1500 if (expr._precedence < newOperatorPrecedence) {
1519 return "(${expr._source})"; 1501 return "(${expr._source})";
1520 } 1502 }
1521 return expr._source; 1503 return expr._source;
1522 } 1504 }
1523 1505
1524 static _InvertedCondition _simple(String source) => 1506 static _InvertedCondition _simple(String source) =>
1525 new _InvertedCondition(2147483647, source); 1507 new _InvertedCondition(2147483647, source);
1526 } 1508 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/services/correction/fix_internal.dart ('k') | pkg/analyzer/lib/dart/ast/ast.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698