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

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

Issue 649873007: Remove Math and most uses of PrintStringWriter (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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 | Annotate | Revision Log
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library services.util; 8 library services.util;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
11 import "dart:math" as math;
12
11 import 'package:analyzer/src/generated/java_core.dart' hide StringUtils; 13 import 'package:analyzer/src/generated/java_core.dart' hide StringUtils;
12 import 'package:analyzer/src/generated/ast.dart'; 14 import 'package:analyzer/src/generated/ast.dart';
13 import 'package:analyzer/src/generated/element.dart'; 15 import 'package:analyzer/src/generated/element.dart';
14 import 'package:analyzer/src/generated/engine.dart'; 16 import 'package:analyzer/src/generated/engine.dart';
15 import 'package:analyzer/src/generated/error.dart'; 17 import 'package:analyzer/src/generated/error.dart';
16 import 'package:analyzer/src/generated/resolver.dart'; 18 import 'package:analyzer/src/generated/resolver.dart';
17 import 'package:analyzer/src/generated/source.dart'; 19 import 'package:analyzer/src/generated/source.dart';
18 import 'package:analyzer/src/generated/scanner.dart'; 20 import 'package:analyzer/src/generated/scanner.dart';
21
19 import 'stubs.dart'; 22 import 'stubs.dart';
20 23
21 /** 24 /**
22 * Context for which assistance should be provided. 25 * Context for which assistance should be provided.
23 */ 26 */
24 class AssistContext { 27 class AssistContext {
25 final SearchEngine searchEngine; 28 final SearchEngine searchEngine;
26 29
27 final AnalysisContext analysisContext; 30 final AnalysisContext analysisContext;
28 31
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 return null; 372 return null;
370 } 373 }
371 // prepare parents 374 // prepare parents
372 List<List<AstNode>> parents = []; 375 List<List<AstNode>> parents = [];
373 for (AstNode node in nodes) { 376 for (AstNode node in nodes) {
374 parents.add(getParents(node)); 377 parents.add(getParents(node));
375 } 378 }
376 // find min length 379 // find min length
377 int minLength = 2147483647; 380 int minLength = 2147483647;
378 for (List<AstNode> parentList in parents) { 381 for (List<AstNode> parentList in parents) {
379 minLength = Math.min(minLength, parentList.length); 382 minLength = math.min(minLength, parentList.length);
380 } 383 }
381 // find deepest parent 384 // find deepest parent
382 int i = 0; 385 int i = 0;
383 for (; i < minLength; i++) { 386 for (; i < minLength; i++) {
384 if (!allListsEqual(parents, i)) { 387 if (!allListsEqual(parents, i)) {
385 break; 388 break;
386 } 389 }
387 } 390 }
388 return parents[0][i - 1]; 391 return parents[0][i - 1];
389 } 392 }
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 } 512 }
510 return null; 513 return null;
511 } 514 }
512 515
513 /** 516 /**
514 * Returns the name of the file which corresponds to the name of the class acc ording to the style 517 * Returns the name of the file which corresponds to the name of the class acc ording to the style
515 * guide. However class does not have to be in this file. 518 * guide. However class does not have to be in this file.
516 */ 519 */
517 static String getRecommentedFileNameForClass(String className) { 520 static String getRecommentedFileNameForClass(String className) {
518 int len = className.length; 521 int len = className.length;
519 JavaStringBuilder sb = new JavaStringBuilder(); 522 StringBuffer buffer = new StringBuffer();
520 bool prevWasUpper = false; 523 bool prevWasUpper = false;
521 for (int i = 0; i < len; i++) { 524 for (int i = 0; i < len; i++) {
522 int c = className.codeUnitAt(i); 525 int c = className.codeUnitAt(i);
523 if (Character.isUpperCase(c)) { 526 if (Character.isUpperCase(c)) {
524 bool nextIsUpper = i < len - 1 && Character.isUpperCase(className.codeUn itAt(i + 1)); 527 bool nextIsUpper = i < len - 1 && Character.isUpperCase(className.codeUn itAt(i + 1));
525 if (i == 0) { 528 if (i == 0) {
526 } else if (prevWasUpper) { 529 } else if (prevWasUpper) {
527 // HTTPServer 530 // HTTPServer
528 // ^ 531 // ^
529 if (!nextIsUpper) { 532 if (!nextIsUpper) {
530 sb.appendChar(0x5F); 533 buffer.writeCharCode(0x5F);
531 } 534 }
532 } else { 535 } else {
533 // HttpServer 536 // HttpServer
534 // ^ 537 // ^
535 sb.appendChar(0x5F); 538 buffer.writeCharCode(0x5F);
536 } 539 }
537 prevWasUpper = true; 540 prevWasUpper = true;
538 c = Character.toLowerCase(c); 541 c = Character.toLowerCase(c);
539 } else { 542 } else {
540 prevWasUpper = false; 543 prevWasUpper = false;
541 } 544 }
542 sb.appendChar(c); 545 buffer.writeCharCode(c);
543 } 546 }
544 sb.append(".dart"); 547 buffer.write(".dart");
545 String fileName = sb.toString(); 548 String fileName = buffer.toString();
546 return fileName; 549 return fileName;
547 } 550 }
548 551
549 /** 552 /**
550 * @return given [Statement] if not [Block], first child [Statement] if 553 * @return given [Statement] if not [Block], first child [Statement] if
551 * [Block], or <code>null</code> if more than one child. 554 * [Block], or <code>null</code> if more than one child.
552 */ 555 */
553 static Statement getSingleStatement(Statement statement) { 556 static Statement getSingleStatement(Statement statement) {
554 if (statement is Block) { 557 if (statement is Block) {
555 List<Statement> blockStatements = statement.statements; 558 List<Statement> blockStatements = statement.statements;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 } 594 }
592 return elements; 595 return elements;
593 } 596 }
594 597
595 /** 598 /**
596 * @return the possible names for variable with initializer of the given [Stri ngLiteral]. 599 * @return the possible names for variable with initializer of the given [Stri ngLiteral].
597 */ 600 */
598 static List<String> getVariableNameSuggestions(String text, Set<String> exclud ed) { 601 static List<String> getVariableNameSuggestions(String text, Set<String> exclud ed) {
599 // filter out everything except of letters and white spaces 602 // filter out everything except of letters and white spaces
600 { 603 {
601 JavaStringBuilder sb = new JavaStringBuilder(); 604 StringBuffer buffer = new StringBuffer();
602 for (int i = 0; i < text.length; i++) { 605 for (int i = 0; i < text.length; i++) {
603 int c = text.codeUnitAt(i); 606 int c = text.codeUnitAt(i);
604 if (Character.isLetter(c) || Character.isWhitespace(c)) { 607 if (Character.isLetter(c) || Character.isWhitespace(c)) {
605 sb.appendChar(c); 608 buffer.writeCharCode(c);
606 } 609 }
607 } 610 }
608 text = sb.toString(); 611 text = buffer.toString();
609 } 612 }
610 // make single camel-case text 613 // make single camel-case text
611 { 614 {
612 List<String> words = StringUtils.split(text); 615 List<String> words = StringUtils.split(text);
613 JavaStringBuilder sb = new JavaStringBuilder(); 616 StringBuffer buffer = new StringBuffer();
614 for (int i = 0; i < words.length; i++) { 617 for (int i = 0; i < words.length; i++) {
615 String word = words[i]; 618 String word = words[i];
616 if (i > 0) { 619 if (i > 0) {
617 word = StringUtils.capitalize(word); 620 word = StringUtils.capitalize(word);
618 } 621 }
619 sb.append(word); 622 buffer.write(word);
620 } 623 }
621 text = sb.toString(); 624 text = buffer.toString();
622 } 625 }
623 // split camel-case into separate suggested names 626 // split camel-case into separate suggested names
624 Set<String> res = new LinkedHashSet(); 627 Set<String> res = new LinkedHashSet();
625 _addAll(excluded, res, _getVariableNameSuggestions(text)); 628 _addAll(excluded, res, _getVariableNameSuggestions(text));
626 return new List.from(res); 629 return new List.from(res);
627 } 630 }
628 631
629 /** 632 /**
630 * @return the possible names for variable with given expected type and expres sion. 633 * @return the possible names for variable with given expected type and expres sion.
631 */ 634 */
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 String oldSource = getText3(range); 954 String oldSource = getText3(range);
952 return getIndentSource3(oldSource, oldIndent, newIndent); 955 return getIndentSource3(oldSource, oldIndent, newIndent);
953 } 956 }
954 957
955 /** 958 /**
956 * Indents given source left or right. 959 * Indents given source left or right.
957 * 960 *
958 * @return the source with changed indentation. 961 * @return the source with changed indentation.
959 */ 962 */
960 String getIndentSource2(String source, bool right) { 963 String getIndentSource2(String source, bool right) {
961 JavaStringBuilder sb = new JavaStringBuilder(); 964 StringBuffer buffer = new StringBuffer();
962 String indent = getIndent(1); 965 String indent = getIndent(1);
963 String eol = endOfLine; 966 String eol = endOfLine;
964 List<String> lines = StringUtils.splitByWholeSeparatorPreserveAllTokens(sour ce, eol); 967 List<String> lines = StringUtils.splitByWholeSeparatorPreserveAllTokens(sour ce, eol);
965 for (int i = 0; i < lines.length; i++) { 968 for (int i = 0; i < lines.length; i++) {
966 String line = lines[i]; 969 String line = lines[i];
967 // last line, stop if empty 970 // last line, stop if empty
968 if (i == lines.length - 1 && StringUtils.isEmpty(line)) { 971 if (i == lines.length - 1 && StringUtils.isEmpty(line)) {
969 break; 972 break;
970 } 973 }
971 // update line 974 // update line
972 if (right) { 975 if (right) {
973 line = "${indent}${line}"; 976 line = "${indent}${line}";
974 } else { 977 } else {
975 line = StringUtils.removeStart(line, indent); 978 line = StringUtils.removeStart(line, indent);
976 } 979 }
977 // append line 980 // append line
978 sb.append(line); 981 buffer.write(line);
979 sb.append(eol); 982 buffer.write(eol);
980 } 983 }
981 return sb.toString(); 984 return buffer.toString();
982 } 985 }
983 986
984 /** 987 /**
985 * @return the source with indentation changed from "oldIndent" to "newIndent" , keeping 988 * @return the source with indentation changed from "oldIndent" to "newIndent" , keeping
986 * indentation of the lines relative to each other. 989 * indentation of the lines relative to each other.
987 */ 990 */
988 String getIndentSource3(String source, String oldIndent, String newIndent) { 991 String getIndentSource3(String source, String oldIndent, String newIndent) {
989 // prepare STRING token ranges 992 // prepare STRING token ranges
990 List<SourceRange> lineRanges = []; 993 List<SourceRange> lineRanges = [];
991 for (Token token in TokenUtils.getTokens(source)) { 994 for (Token token in TokenUtils.getTokens(source)) {
992 if (token.type == TokenType.STRING) { 995 if (token.type == TokenType.STRING) {
993 lineRanges.add(SourceRangeFactory.rangeToken(token)); 996 lineRanges.add(SourceRangeFactory.rangeToken(token));
994 } 997 }
995 } 998 }
996 // re-indent lines 999 // re-indent lines
997 JavaStringBuilder sb = new JavaStringBuilder(); 1000 StringBuffer buffer = new StringBuffer();
998 String eol = endOfLine; 1001 String eol = endOfLine;
999 List<String> lines = StringUtils.splitByWholeSeparatorPreserveAllTokens(sour ce, eol); 1002 List<String> lines = StringUtils.splitByWholeSeparatorPreserveAllTokens(sour ce, eol);
1000 int lineOffset = 0; 1003 int lineOffset = 0;
1001 for (int i = 0; i < lines.length; i++) { 1004 for (int i = 0; i < lines.length; i++) {
1002 String line = lines[i]; 1005 String line = lines[i];
1003 // last line, stop if empty 1006 // last line, stop if empty
1004 if (i == lines.length - 1 && StringUtils.isEmpty(line)) { 1007 if (i == lines.length - 1 && StringUtils.isEmpty(line)) {
1005 break; 1008 break;
1006 } 1009 }
1007 // check if "offset" is in one of the String ranges 1010 // check if "offset" is in one of the String ranges
1008 bool inString = false; 1011 bool inString = false;
1009 for (SourceRange lineRange in lineRanges) { 1012 for (SourceRange lineRange in lineRanges) {
1010 inString = javaBooleanOr(inString, lineOffset > lineRange.offset && line Offset < lineRange.end); 1013 inString = javaBooleanOr(inString, lineOffset > lineRange.offset && line Offset < lineRange.end);
1011 if (lineOffset > lineRange.end) { 1014 if (lineOffset > lineRange.end) {
1012 break; 1015 break;
1013 } 1016 }
1014 } 1017 }
1015 lineOffset += line.length + eol.length; 1018 lineOffset += line.length + eol.length;
1016 // update line indent 1019 // update line indent
1017 if (!inString) { 1020 if (!inString) {
1018 line = "${newIndent}${StringUtils.removeStart(line, oldIndent)}"; 1021 line = "${newIndent}${StringUtils.removeStart(line, oldIndent)}";
1019 } 1022 }
1020 // append line 1023 // append line
1021 sb.append(line); 1024 buffer.write(line);
1022 sb.append(eol); 1025 buffer.write(eol);
1023 } 1026 }
1024 return sb.toString(); 1027 return buffer.toString();
1025 } 1028 }
1026 1029
1027 /** 1030 /**
1028 * @return [InsertDesc], description where to insert new library-related direc tive. 1031 * @return [InsertDesc], description where to insert new library-related direc tive.
1029 */ 1032 */
1030 CorrectionUtils_InsertDesc get insertDescImport { 1033 CorrectionUtils_InsertDesc get insertDescImport {
1031 // analyze directives 1034 // analyze directives
1032 Directive prevDirective = null; 1035 Directive prevDirective = null;
1033 for (Directive directive in unit.directives) { 1036 for (Directive directive in unit.directives) {
1034 if (directive is LibraryDirective || directive is ImportDirective || direc tive is ExportDirective) { 1037 if (directive is LibraryDirective || directive is ImportDirective || direc tive is ExportDirective) {
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 * @return the source for the parameter with the given type and name. 1304 * @return the source for the parameter with the given type and name.
1302 */ 1305 */
1303 String getParameterSource(DartType type, String name) { 1306 String getParameterSource(DartType type, String name) {
1304 // no type 1307 // no type
1305 if (type == null || type.isDynamic) { 1308 if (type == null || type.isDynamic) {
1306 return name; 1309 return name;
1307 } 1310 }
1308 // function type 1311 // function type
1309 if (type is FunctionType) { 1312 if (type is FunctionType) {
1310 FunctionType functionType = type; 1313 FunctionType functionType = type;
1311 JavaStringBuilder sb = new JavaStringBuilder(); 1314 StringBuffer buffer = new StringBuffer();
1312 // return type 1315 // return type
1313 DartType returnType = functionType.returnType; 1316 DartType returnType = functionType.returnType;
1314 if (returnType != null && !returnType.isDynamic) { 1317 if (returnType != null && !returnType.isDynamic) {
1315 sb.append(getTypeSource2(returnType)); 1318 buffer.write(getTypeSource2(returnType));
1316 sb.appendChar(0x20); 1319 buffer.writeCharCode(0x20);
1317 } 1320 }
1318 // parameter name 1321 // parameter name
1319 sb.append(name); 1322 buffer.write(name);
1320 // parameters 1323 // parameters
1321 sb.appendChar(0x28); 1324 buffer.writeCharCode(0x28);
1322 List<ParameterElement> fParameters = functionType.parameters; 1325 List<ParameterElement> fParameters = functionType.parameters;
1323 for (int i = 0; i < fParameters.length; i++) { 1326 for (int i = 0; i < fParameters.length; i++) {
1324 ParameterElement fParameter = fParameters[i]; 1327 ParameterElement fParameter = fParameters[i];
1325 if (i != 0) { 1328 if (i != 0) {
1326 sb.append(", "); 1329 buffer.write(", ");
1327 } 1330 }
1328 sb.append(getParameterSource(fParameter.type, fParameter.name)); 1331 buffer.write(getParameterSource(fParameter.type, fParameter.name));
1329 } 1332 }
1330 sb.appendChar(0x29); 1333 buffer.writeCharCode(0x29);
1331 // done 1334 // done
1332 return sb.toString(); 1335 return buffer.toString();
1333 } 1336 }
1334 // simple type 1337 // simple type
1335 return "${getTypeSource2(type)} ${name}"; 1338 return "${getTypeSource2(type)} ${name}";
1336 } 1339 }
1337 1340
1338 /** 1341 /**
1339 * @return the line prefix consisting of spaces and tabs on the left from the given offset. 1342 * @return the line prefix consisting of spaces and tabs on the left from the given offset.
1340 */ 1343 */
1341 String getPrefix(int endIndex) { 1344 String getPrefix(int endIndex) {
1342 int startIndex = getLineContentStart(endIndex); 1345 int startIndex = getLineContentStart(endIndex);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1376 if ("dynamic" == typeSource) { 1379 if ("dynamic" == typeSource) {
1377 return null; 1380 return null;
1378 } 1381 }
1379 return typeSource; 1382 return typeSource;
1380 } 1383 }
1381 1384
1382 /** 1385 /**
1383 * @return the source to reference the given [Type] in this [CompilationUnit]. 1386 * @return the source to reference the given [Type] in this [CompilationUnit].
1384 */ 1387 */
1385 String getTypeSource2(DartType type) { 1388 String getTypeSource2(DartType type) {
1386 JavaStringBuilder sb = new JavaStringBuilder(); 1389 StringBuffer buffer = new StringBuffer();
1387 // prepare element 1390 // prepare element
1388 Element element = type.element; 1391 Element element = type.element;
1389 if (element == null) { 1392 if (element == null) {
1390 String source = type.toString(); 1393 String source = type.toString();
1391 source = StringUtils.remove(source, "<dynamic>"); 1394 source = StringUtils.remove(source, "<dynamic>");
1392 source = StringUtils.remove(source, "<dynamic, dynamic>"); 1395 source = StringUtils.remove(source, "<dynamic, dynamic>");
1393 return source; 1396 return source;
1394 } 1397 }
1395 // append prefix 1398 // append prefix
1396 { 1399 {
1397 ImportElement imp = _getImportElement(element); 1400 ImportElement imp = _getImportElement(element);
1398 if (imp != null && imp.prefix != null) { 1401 if (imp != null && imp.prefix != null) {
1399 sb.append(imp.prefix.displayName); 1402 buffer.write(imp.prefix.displayName);
1400 sb.append("."); 1403 buffer.write(".");
1401 } 1404 }
1402 } 1405 }
1403 // append simple name 1406 // append simple name
1404 String name = element.displayName; 1407 String name = element.displayName;
1405 sb.append(name); 1408 buffer.write(name);
1406 // may be type arguments 1409 // may be type arguments
1407 if (type is InterfaceType) { 1410 if (type is InterfaceType) {
1408 InterfaceType interfaceType = type; 1411 InterfaceType interfaceType = type;
1409 List<DartType> arguments = interfaceType.typeArguments; 1412 List<DartType> arguments = interfaceType.typeArguments;
1410 // check if has arguments 1413 // check if has arguments
1411 bool hasArguments = false; 1414 bool hasArguments = false;
1412 for (DartType argument in arguments) { 1415 for (DartType argument in arguments) {
1413 if (!argument.isDynamic) { 1416 if (!argument.isDynamic) {
1414 hasArguments = true; 1417 hasArguments = true;
1415 break; 1418 break;
1416 } 1419 }
1417 } 1420 }
1418 // append type arguments 1421 // append type arguments
1419 if (hasArguments) { 1422 if (hasArguments) {
1420 sb.append("<"); 1423 buffer.write("<");
1421 for (int i = 0; i < arguments.length; i++) { 1424 for (int i = 0; i < arguments.length; i++) {
1422 DartType argument = arguments[i]; 1425 DartType argument = arguments[i];
1423 if (i != 0) { 1426 if (i != 0) {
1424 sb.append(", "); 1427 buffer.write(", ");
1425 } 1428 }
1426 sb.append(getTypeSource2(argument)); 1429 buffer.write(getTypeSource2(argument));
1427 } 1430 }
1428 sb.append(">"); 1431 buffer.write(">");
1429 } 1432 }
1430 } 1433 }
1431 // done 1434 // done
1432 return sb.toString(); 1435 return buffer.toString();
1433 } 1436 }
1434 1437
1435 /** 1438 /**
1436 * @return <code>true</code> if selection range contains only whitespace. 1439 * @return <code>true</code> if selection range contains only whitespace.
1437 */ 1440 */
1438 bool isJustWhitespace(SourceRange range) => getText3(range).trim().length == 0 ; 1441 bool isJustWhitespace(SourceRange range) => getText3(range).trim().length == 0 ;
1439 1442
1440 /** 1443 /**
1441 * @return <code>true</code> if selection range contains only whitespace or co mments 1444 * @return <code>true</code> if selection range contains only whitespace or co mments
1442 */ 1445 */
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
1752 return []; 1755 return [];
1753 } 1756 }
1754 } 1757 }
1755 1758
1756 /** 1759 /**
1757 * @return <code>true</code> if given [Token]s contain only single [Token] wit h given 1760 * @return <code>true</code> if given [Token]s contain only single [Token] wit h given
1758 * [TokenType]. 1761 * [TokenType].
1759 */ 1762 */
1760 static bool hasOnly(List<Token> tokens, TokenType type) => tokens.length == 1 && tokens[0].type == type; 1763 static bool hasOnly(List<Token> tokens, TokenType type) => tokens.length == 1 && tokens[0].type == type;
1761 } 1764 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/services/generated/completion.dart ('k') | pkg/analyzer/lib/src/generated/engine.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698