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

Side by Side Diff: pkg/analyzer/lib/src/services/formatter_impl.dart

Issue 109423004: Formatter line continuation awareness hooks. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years 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
« no previous file with comments | « no previous file | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 formatter_impl; 5 library formatter_impl;
6 6
7 import 'dart:math'; 7 import 'dart:math';
8 8
9 import 'package:analyzer/analyzer.dart'; 9 import 'package:analyzer/analyzer.dart';
10 import 'package:analyzer/src/generated/java_core.dart' show CharSequence; 10 import 'package:analyzer/src/generated/java_core.dart' show CharSequence;
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 visit(node.arguments); 390 visit(node.arguments);
391 } 391 }
392 392
393 visitArgumentDefinitionTest(ArgumentDefinitionTest node) { 393 visitArgumentDefinitionTest(ArgumentDefinitionTest node) {
394 token(node.question); 394 token(node.question);
395 visit(node.identifier); 395 visit(node.identifier);
396 } 396 }
397 397
398 visitArgumentList(ArgumentList node) { 398 visitArgumentList(ArgumentList node) {
399 token(node.leftParenthesis); 399 token(node.leftParenthesis);
400 visitCommaSeparatedNodes(node.arguments); 400 allowContinuedLines((){
401 visitCommaSeparatedNodes(node.arguments);
402 });
401 token(node.rightParenthesis); 403 token(node.rightParenthesis);
402 } 404 }
403 405
404 visitAsExpression(AsExpression node) { 406 visitAsExpression(AsExpression node) {
405 visit(node.expression); 407 visit(node.expression);
406 space(); 408 space();
407 token(node.asOperator); 409 token(node.asOperator);
408 space(); 410 space();
409 visit(node.type); 411 visit(node.type);
410 } 412 }
411 413
412 visitAssertStatement(AssertStatement node) { 414 visitAssertStatement(AssertStatement node) {
413 token(node.keyword); 415 token(node.keyword);
414 space(); 416 space();
415 token(node.leftParenthesis); 417 token(node.leftParenthesis);
416 visit(node.condition); 418 visit(node.condition);
417 token(node.rightParenthesis); 419 token(node.rightParenthesis);
418 token(node.semicolon); 420 token(node.semicolon);
419 } 421 }
420 422
421 visitAssignmentExpression(AssignmentExpression node) { 423 visitAssignmentExpression(AssignmentExpression node) {
422 visit(node.leftHandSide); 424 visit(node.leftHandSide);
423 space(); 425 space();
424 token(node.operator); 426 token(node.operator);
425 space(); 427 allowContinuedLines((){
426 visit(node.rightHandSide); 428 space();
429 visit(node.rightHandSide);
430 });
427 } 431 }
428 432
429 visitBinaryExpression(BinaryExpression node) { 433 visitBinaryExpression(BinaryExpression node) {
430 visit(node.leftOperand); 434 visit(node.leftOperand);
431 space(); 435 space();
432 token(node.operator); 436 token(node.operator);
433 space(); 437 space();
434 visit(node.rightOperand); 438 visit(node.rightOperand);
435 } 439 }
436 440
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 space(); 489 space();
486 } 490 }
487 visit(node.body); 491 visit(node.body);
488 } 492 }
489 493
490 visitClassDeclaration(ClassDeclaration node) { 494 visitClassDeclaration(ClassDeclaration node) {
491 modifier(node.abstractKeyword); 495 modifier(node.abstractKeyword);
492 token(node.classKeyword); 496 token(node.classKeyword);
493 space(); 497 space();
494 visit(node.name); 498 visit(node.name);
495 visit(node.typeParameters); 499 allowContinuedLines((){
496 visitNode(node.extendsClause, precededBy: space); 500 visit(node.typeParameters);
497 visitNode(node.withClause, precededBy: space); 501 visitNode(node.extendsClause, precededBy: space);
498 visitNode(node.implementsClause, precededBy: space); 502 visitNode(node.withClause, precededBy: space);
499 space(); 503 visitNode(node.implementsClause, precededBy: space);
504 space();
505 });
500 token(node.leftBracket); 506 token(node.leftBracket);
501 indent(); 507 indent();
502 visitNodes(node.members, precededBy: newlines, separatedBy: newlines); 508 visitNodes(node.members, precededBy: newlines, separatedBy: newlines);
503 unindent(); 509 unindent();
504 newlines(); 510 newlines();
505 token(node.rightBracket); 511 token(node.rightBracket);
506 } 512 }
507 513
508 visitClassTypeAlias(ClassTypeAlias node) { 514 visitClassTypeAlias(ClassTypeAlias node) {
509 token(node.keyword); 515 token(node.keyword);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 token(node.endToken /* EOF */); 553 token(node.endToken /* EOF */);
548 554
549 // Be a good citizen, end with a NL 555 // Be a good citizen, end with a NL
550 ensureTrailingNewline(); 556 ensureTrailingNewline();
551 } 557 }
552 558
553 visitConditionalExpression(ConditionalExpression node) { 559 visitConditionalExpression(ConditionalExpression node) {
554 visit(node.condition); 560 visit(node.condition);
555 space(); 561 space();
556 token(node.question); 562 token(node.question);
557 space(); 563 allowContinuedLines((){
558 visit(node.thenExpression); 564 space();
559 space(); 565 visit(node.thenExpression);
560 token(node.colon); 566 space();
561 space(); 567 token(node.colon);
562 visit(node.elseExpression); 568 space();
569 visit(node.elseExpression);
570 });
563 } 571 }
564 572
565 visitConstructorDeclaration(ConstructorDeclaration node) { 573 visitConstructorDeclaration(ConstructorDeclaration node) {
566 modifier(node.externalKeyword); 574 modifier(node.externalKeyword);
567 modifier(node.constKeyword); 575 modifier(node.constKeyword);
568 modifier(node.factoryKeyword); 576 modifier(node.factoryKeyword);
569 visit(node.returnType); 577 visit(node.returnType);
570 token(node.period); 578 token(node.period);
571 visit(node.name); 579 visit(node.name);
572 visit(node.parameters); 580 visit(node.parameters);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 } 664 }
657 665
658 visitDoStatement(DoStatement node) { 666 visitDoStatement(DoStatement node) {
659 token(node.doKeyword); 667 token(node.doKeyword);
660 space(); 668 space();
661 visit(node.body); 669 visit(node.body);
662 space(); 670 space();
663 token(node.whileKeyword); 671 token(node.whileKeyword);
664 space(); 672 space();
665 token(node.leftParenthesis); 673 token(node.leftParenthesis);
666 visit(node.condition); 674 allowContinuedLines((){
667 token(node.rightParenthesis); 675 visit(node.condition);
676 token(node.rightParenthesis);
677 });
668 token(node.semicolon); 678 token(node.semicolon);
669 } 679 }
670 680
671 visitDoubleLiteral(DoubleLiteral node) { 681 visitDoubleLiteral(DoubleLiteral node) {
672 token(node.literal); 682 token(node.literal);
673 } 683 }
674 684
675 visitEmptyFunctionBody(EmptyFunctionBody node) { 685 visitEmptyFunctionBody(EmptyFunctionBody node) {
676 token(node.semicolon); 686 token(node.semicolon);
677 } 687 }
678 688
679 visitEmptyStatement(EmptyStatement node) { 689 visitEmptyStatement(EmptyStatement node) {
680 token(node.semicolon); 690 token(node.semicolon);
681 } 691 }
682 692
683 visitExportDirective(ExportDirective node) { 693 visitExportDirective(ExportDirective node) {
684 token(node.keyword); 694 token(node.keyword);
685 space(); 695 space();
686 visit(node.uri); 696 visit(node.uri);
687 visitNodes(node.combinators, precededBy: space, separatedBy: space); 697 allowContinuedLines((){
698 visitNodes(node.combinators, precededBy: space, separatedBy: space);
699 });
688 token(node.semicolon); 700 token(node.semicolon);
689 } 701 }
690 702
691 visitExpressionFunctionBody(ExpressionFunctionBody node) { 703 visitExpressionFunctionBody(ExpressionFunctionBody node) {
692 token(node.functionDefinition); 704 token(node.functionDefinition);
693 space(); 705 space();
694 visit(node.expression); 706 visit(node.expression);
695 token(node.semicolon); 707 token(node.semicolon);
696 } 708 }
697 709
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 842
831 visitHideCombinator(HideCombinator node) { 843 visitHideCombinator(HideCombinator node) {
832 token(node.keyword); 844 token(node.keyword);
833 space(); 845 space();
834 visitCommaSeparatedNodes(node.hiddenNames); 846 visitCommaSeparatedNodes(node.hiddenNames);
835 } 847 }
836 848
837 visitIfStatement(IfStatement node) { 849 visitIfStatement(IfStatement node) {
838 var hasElse = node.elseStatement != null; 850 var hasElse = node.elseStatement != null;
839 token(node.ifKeyword); 851 token(node.ifKeyword);
840 space(); 852 allowContinuedLines((){
841 token(node.leftParenthesis); 853 space();
842 visit(node.condition); 854 token(node.leftParenthesis);
843 token(node.rightParenthesis); 855 visit(node.condition);
856 token(node.rightParenthesis);
857 });
844 space(); 858 space();
845 if (hasElse) { 859 if (hasElse) {
846 printAsBlock(node.thenStatement); 860 printAsBlock(node.thenStatement);
847 space(); 861 space();
848 token(node.elseKeyword); 862 token(node.elseKeyword);
849 space(); 863 space();
850 printAsBlock(node.elseStatement); 864 printAsBlock(node.elseStatement);
851 } else { 865 } else {
852 visit(node.thenStatement); 866 visit(node.thenStatement);
853 } 867 }
854 } 868 }
855 869
856 visitImplementsClause(ImplementsClause node) { 870 visitImplementsClause(ImplementsClause node) {
857 token(node.keyword); 871 token(node.keyword);
858 space(); 872 space();
859 visitCommaSeparatedNodes(node.interfaces); 873 visitCommaSeparatedNodes(node.interfaces);
860 } 874 }
861 875
862 visitImportDirective(ImportDirective node) { 876 visitImportDirective(ImportDirective node) {
863 token(node.keyword); 877 token(node.keyword);
864 space(); 878 space();
865 visit(node.uri); 879 visit(node.uri);
866 token(node.asToken, precededBy: space, followedBy: space); 880 token(node.asToken, precededBy: space, followedBy: space);
867 visit(node.prefix); 881 allowContinuedLines((){
868 visitNodes(node.combinators, precededBy: space, separatedBy: space); 882 visit(node.prefix);
883 visitNodes(node.combinators, precededBy: space, separatedBy: space);
884 });
869 token(node.semicolon); 885 token(node.semicolon);
870 } 886 }
871 887
872 visitIndexExpression(IndexExpression node) { 888 visitIndexExpression(IndexExpression node) {
873 if (node.isCascaded) { 889 if (node.isCascaded) {
874 token(node.period); 890 token(node.period);
875 } else { 891 } else {
876 visit(node.target); 892 visit(node.target);
877 } 893 }
878 token(node.leftBracket); 894 token(node.leftBracket);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
1066 token(node.keyword); 1082 token(node.keyword);
1067 } 1083 }
1068 1084
1069 visitReturnStatement(ReturnStatement node) { 1085 visitReturnStatement(ReturnStatement node) {
1070 var expression = node.expression; 1086 var expression = node.expression;
1071 if (expression == null) { 1087 if (expression == null) {
1072 token(node.keyword); 1088 token(node.keyword);
1073 token(node.semicolon); 1089 token(node.semicolon);
1074 } else { 1090 } else {
1075 token(node.keyword); 1091 token(node.keyword);
1076 space(); 1092 allowContinuedLines((){
1077 expression.accept(this); 1093 space();
1078 token(node.semicolon); 1094 expression.accept(this);
1095 token(node.semicolon);
1096 });
1079 } 1097 }
1080 } 1098 }
1081 1099
1082 visitScriptTag(ScriptTag node) { 1100 visitScriptTag(ScriptTag node) {
1083 token(node.scriptTag); 1101 token(node.scriptTag);
1084 } 1102 }
1085 1103
1086 visitShowCombinator(ShowCombinator node) { 1104 visitShowCombinator(ShowCombinator node) {
1087 token(node.keyword); 1105 token(node.keyword);
1088 space(); 1106 space();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1202 token(node.leftBracket); 1220 token(node.leftBracket);
1203 visitCommaSeparatedNodes(node.typeParameters); 1221 visitCommaSeparatedNodes(node.typeParameters);
1204 token(node.rightBracket); 1222 token(node.rightBracket);
1205 } 1223 }
1206 1224
1207 visitVariableDeclaration(VariableDeclaration node) { 1225 visitVariableDeclaration(VariableDeclaration node) {
1208 visit(node.name); 1226 visit(node.name);
1209 if (node.initializer != null) { 1227 if (node.initializer != null) {
1210 space(); 1228 space();
1211 token(node.equals); 1229 token(node.equals);
1212 space(); 1230 var initializer = node.initializer;
1213 visit(node.initializer); 1231 if (initializer is! ListLiteral && initializer is! MapLiteral) {
1232 allowContinuedLines((){
1233 space();
1234 visit(initializer);
1235 });
1236 } else {
1237 space();
1238 visit(initializer);
1239 }
1214 } 1240 }
1215 } 1241 }
1216 1242
1217 visitVariableDeclarationList(VariableDeclarationList node) { 1243 visitVariableDeclarationList(VariableDeclarationList node) {
1218 modifier(node.keyword); 1244 modifier(node.keyword);
1219 visitNode(node.type, followedBy: space); 1245 visitNode(node.type, followedBy: space);
1220 visitCommaSeparatedNodes(node.variables); 1246 visitCommaSeparatedNodes(node.variables);
1221 } 1247 }
1222 1248
1223 visitVariableDeclarationStatement(VariableDeclarationStatement node) { 1249 visitVariableDeclarationStatement(VariableDeclarationStatement node) {
1224 visit(node.variables); 1250 visit(node.variables);
1225 token(node.semicolon); 1251 token(node.semicolon);
1226 } 1252 }
1227 1253
1228 visitWhileStatement(WhileStatement node) { 1254 visitWhileStatement(WhileStatement node) {
1229 token(node.keyword); 1255 token(node.keyword);
1230 space(); 1256 space();
1231 token(node.leftParenthesis); 1257 token(node.leftParenthesis);
1232 visit(node.condition); 1258 allowContinuedLines((){
1233 token(node.rightParenthesis); 1259 visit(node.condition);
1260 token(node.rightParenthesis);
1261 });
1234 if (node.body is! EmptyStatement) { 1262 if (node.body is! EmptyStatement) {
1235 space(); 1263 space();
1236 } 1264 }
1237 visit(node.body); 1265 visit(node.body);
1238 } 1266 }
1239 1267
1240 visitWithClause(WithClause node) { 1268 visitWithClause(WithClause node) {
1241 token(node.withKeyword); 1269 token(node.withKeyword);
1242 space(); 1270 space();
1243 visitCommaSeparatedNodes(node.mixinTypes); 1271 visitCommaSeparatedNodes(node.mixinTypes);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 if (precededBy != null) { 1337 if (precededBy != null) {
1310 precededBy(); 1338 precededBy();
1311 } 1339 }
1312 node.accept(this); 1340 node.accept(this);
1313 if (followedBy != null) { 1341 if (followedBy != null) {
1314 followedBy(); 1342 followedBy();
1315 } 1343 }
1316 } 1344 }
1317 } 1345 }
1318 1346
1347 /// Allow [code] to be continued across lines.
1348 allowContinuedLines(code()) {
1349 indent(2);
Brian Wilkerson 2013/12/13 22:38:03 Do we want to increase the indentation if allowCon
1350 code();
1351 unindent(2);
1352 }
1319 1353
1320 /// Emit the given [modifier] if it's non null, followed by non-breaking 1354 /// Emit the given [modifier] if it's non null, followed by non-breaking
1321 /// whitespace. 1355 /// whitespace.
1322 modifier(Token modifier) { 1356 modifier(Token modifier) {
1323 token(modifier, followedBy: space); 1357 token(modifier, followedBy: space);
1324 } 1358 }
1325 1359
1326 /// Indicate that at least one newline should be emitted and possibly more 1360 /// Indicate that at least one newline should be emitted and possibly more
1327 /// if the source has them. 1361 /// if the source has them.
1328 newlines() { 1362 newlines() {
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
1565 var lastLine = 1599 var lastLine =
1566 lineInfo.getLocation(lastOffset).lineNumber; 1600 lineInfo.getLocation(lastOffset).lineNumber;
1567 var currentLine = 1601 var currentLine =
1568 lineInfo.getLocation(currentOffset).lineNumber; 1602 lineInfo.getLocation(currentOffset).lineNumber;
1569 return currentLine - lastLine; 1603 return currentLine - lastLine;
1570 } 1604 }
1571 1605
1572 String toString() => writer.toString(); 1606 String toString() => writer.toString();
1573 1607
1574 } 1608 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698