| OLD | NEW |
| 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_experimental/analyzer.dart'; | 9 import 'package:analyzer_experimental/analyzer.dart'; |
| 10 import 'package:analyzer_experimental/src/generated/parser.dart'; | 10 import 'package:analyzer_experimental/src/generated/parser.dart'; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 final SourceWriter writer; | 145 final SourceWriter writer; |
| 146 | 146 |
| 147 /// Cached line info for calculating blank lines. | 147 /// Cached line info for calculating blank lines. |
| 148 LineInfo lineInfo; | 148 LineInfo lineInfo; |
| 149 | 149 |
| 150 /// Cached previous token for calculating preceding whitespace. | 150 /// Cached previous token for calculating preceding whitespace. |
| 151 Token previousToken; | 151 Token previousToken; |
| 152 | 152 |
| 153 /// A flag to indicate that a newline should be emitted before the next token. | 153 /// A flag to indicate that a newline should be emitted before the next token. |
| 154 bool needsNewline = false; | 154 bool needsNewline = false; |
| 155 | 155 |
| 156 /// A flag to indicate that user introduced newlines should be emitted before | 156 /// A flag to indicate that user introduced newlines should be emitted before |
| 157 /// the next token. | 157 /// the next token. |
| 158 bool preservePrecedingNewlines = false; | 158 bool preservePrecedingNewlines = false; |
| 159 | 159 |
| 160 /// Initialize a newly created visitor to write source code representing | 160 /// Initialize a newly created visitor to write source code representing |
| 161 /// the visited nodes to the given [writer]. | 161 /// the visited nodes to the given [writer]. |
| 162 SourceVisitor(FormatterOptions options, this.lineInfo) : | 162 SourceVisitor(FormatterOptions options, this.lineInfo) : |
| 163 writer = new SourceWriter(indentCount: options.initialIndentationLevel, | 163 writer = new SourceWriter(indentCount: options.initialIndentationLevel, |
| 164 lineSeparator: options.lineSeparator); | 164 lineSeparator: options.lineSeparator); |
| 165 | 165 |
| 166 visitAdjacentStrings(AdjacentStrings node) { | 166 visitAdjacentStrings(AdjacentStrings node) { |
| 167 visitNodes(node.strings, separatedBy: space); | 167 visitNodes(node.strings, separatedBy: space); |
| 168 } | 168 } |
| 169 | 169 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 visit(node.leftOperand); | 215 visit(node.leftOperand); |
| 216 space(); | 216 space(); |
| 217 token(node.operator); | 217 token(node.operator); |
| 218 space(); | 218 space(); |
| 219 visit(node.rightOperand); | 219 visit(node.rightOperand); |
| 220 } | 220 } |
| 221 | 221 |
| 222 visitBlock(Block node) { | 222 visitBlock(Block node) { |
| 223 token(node.leftBracket); | 223 token(node.leftBracket); |
| 224 indent(); | 224 indent(); |
| 225 | |
| 226 visitNodes(node.statements, precededBy: newlines, separatedBy: newlines); | 225 visitNodes(node.statements, precededBy: newlines, separatedBy: newlines); |
| 227 | |
| 228 unindent(); | 226 unindent(); |
| 229 newlines(); | 227 newlines(); |
| 230 token(node.rightBracket); | 228 token(node.rightBracket); |
| 231 } | 229 } |
| 232 | 230 |
| 233 visitBlockFunctionBody(BlockFunctionBody node) { | 231 visitBlockFunctionBody(BlockFunctionBody node) { |
| 234 visit(node.block); | 232 visit(node.block); |
| 235 } | 233 } |
| 236 | 234 |
| 237 visitBooleanLiteral(BooleanLiteral node) { | 235 visitBooleanLiteral(BooleanLiteral node) { |
| 238 token(node.literal); | 236 token(node.literal); |
| 239 } | 237 } |
| 240 | 238 |
| 241 visitBreakStatement(BreakStatement node) { | 239 visitBreakStatement(BreakStatement node) { |
| 242 token(node.keyword); | 240 token(node.keyword); |
| 243 visitNode(node.label, precededBy: space); | 241 visitNode(node.label, precededBy: space); |
| 244 token(node.semicolon); | 242 token(node.semicolon); |
| 245 } | 243 } |
| 246 | 244 |
| 247 visitCascadeExpression(CascadeExpression node) { | 245 visitCascadeExpression(CascadeExpression node) { |
| 248 visit(node.target); | 246 visit(node.target); |
| 249 visitNodes(node.cascadeSections); | 247 visitNodes(node.cascadeSections); |
| 250 } | 248 } |
| 251 | 249 |
| 252 visitCatchClause(CatchClause node) { | 250 visitCatchClause(CatchClause node) { |
| 253 | 251 |
| 254 token(node.onKeyword, precededBy: space, followedBy: space); | 252 token(node.onKeyword, precededBy: space, followedBy: space); |
| 255 visit(node.exceptionType); | 253 visit(node.exceptionType); |
| 256 | 254 |
| 257 if (node.catchKeyword != null) { | 255 if (node.catchKeyword != null) { |
| 258 if (node.exceptionType != null) { | 256 if (node.exceptionType != null) { |
| 259 space(); | 257 space(); |
| 260 } | 258 } |
| 261 token(node.catchKeyword); | 259 token(node.catchKeyword); |
| 262 space(); | 260 space(); |
| 263 token(node.leftParenthesis); | 261 token(node.leftParenthesis); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 276 modifier(node.abstractKeyword); | 274 modifier(node.abstractKeyword); |
| 277 token(node.classKeyword); | 275 token(node.classKeyword); |
| 278 space(); | 276 space(); |
| 279 visit(node.name); | 277 visit(node.name); |
| 280 visit(node.typeParameters); | 278 visit(node.typeParameters); |
| 281 visitNode(node.extendsClause, precededBy: space); | 279 visitNode(node.extendsClause, precededBy: space); |
| 282 visitNode(node.withClause, precededBy: space); | 280 visitNode(node.withClause, precededBy: space); |
| 283 visitNode(node.implementsClause, precededBy: space); | 281 visitNode(node.implementsClause, precededBy: space); |
| 284 space(); | 282 space(); |
| 285 token(node.leftBracket); | 283 token(node.leftBracket); |
| 286 | |
| 287 indent(); | 284 indent(); |
| 288 visitNodes(node.members, precededBy: newlines, separatedBy: newlines); | 285 visitNodes(node.members, precededBy: newlines, separatedBy: newlines); |
| 289 unindent(); | 286 unindent(); |
| 290 newlines(); | 287 newlines(); |
| 291 | |
| 292 token(node.rightBracket); | 288 token(node.rightBracket); |
| 293 } | 289 } |
| 294 | 290 |
| 295 visitClassTypeAlias(ClassTypeAlias node) { | 291 visitClassTypeAlias(ClassTypeAlias node) { |
| 296 token(node.keyword); | 292 token(node.keyword); |
| 297 space(); | 293 space(); |
| 298 visit(node.name); | 294 visit(node.name); |
| 299 visit(node.typeParameters); | 295 visit(node.typeParameters); |
| 300 space(); | 296 space(); |
| 301 token(node.equals); | 297 token(node.equals); |
| 302 space(); | 298 space(); |
| 303 if (node.abstractKeyword != null) { | 299 if (node.abstractKeyword != null) { |
| 304 token(node.abstractKeyword); | 300 token(node.abstractKeyword); |
| 305 space(); | 301 space(); |
| 306 } | 302 } |
| 307 visit(node.superclass); | 303 visit(node.superclass); |
| 308 visitNode(node.withClause, precededBy: space); | 304 visitNode(node.withClause, precededBy: space); |
| 309 visitNode(node.implementsClause, precededBy: space); | 305 visitNode(node.implementsClause, precededBy: space); |
| 310 token(node.semicolon); | 306 token(node.semicolon); |
| 311 } | 307 } |
| 312 | 308 |
| 313 visitComment(Comment node) => null; | 309 visitComment(Comment node) => null; |
| 314 | 310 |
| 315 visitCommentReference(CommentReference node) => null; | 311 visitCommentReference(CommentReference node) => null; |
| 316 | 312 |
| 317 visitCompilationUnit(CompilationUnit node) { | 313 visitCompilationUnit(CompilationUnit node) { |
| 318 | 314 |
| 319 // Cache EOF for leading whitespace calculation | 315 // Cache EOF for leading whitespace calculation |
| 320 var start = node.beginToken.previous; | 316 var start = node.beginToken.previous; |
| 321 if (start != null && start.type is TokenType_EOF) { | 317 if (start != null && start.type is TokenType_EOF) { |
| 322 previousToken = start; | 318 previousToken = start; |
| 323 } | 319 } |
| 324 | 320 |
| 325 var scriptTag = node.scriptTag; | 321 var scriptTag = node.scriptTag; |
| 326 var directives = node.directives; | 322 var directives = node.directives; |
| 327 visit(scriptTag); | 323 visit(scriptTag); |
| 328 | 324 |
| 329 preservePrecedingNewlines = true; | 325 preservePrecedingNewlines = true; |
| 330 visitNodes(directives, separatedBy: newlines); | 326 visitNodes(directives, separatedBy: newlines); |
| 331 | 327 |
| 332 preservePrecedingNewlines = true; | 328 preservePrecedingNewlines = true; |
| 333 visitNodes(node.declarations, separatedBy: newlines); | 329 visitNodes(node.declarations, separatedBy: newlines); |
| 334 | 330 |
| 335 // Handle trailing whitespace | 331 // Handle trailing whitespace |
| 336 preservePrecedingNewlines = true; | 332 preservePrecedingNewlines = true; |
| 337 token(node.endToken /* EOF */); | 333 token(node.endToken /* EOF */); |
| 338 } | 334 } |
| 339 | 335 |
| 340 visitConditionalExpression(ConditionalExpression node) { | 336 visitConditionalExpression(ConditionalExpression node) { |
| 341 visit(node.condition); | 337 visit(node.condition); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 353 modifier(node.externalKeyword); | 349 modifier(node.externalKeyword); |
| 354 modifier(node.constKeyword); | 350 modifier(node.constKeyword); |
| 355 modifier(node.factoryKeyword); | 351 modifier(node.factoryKeyword); |
| 356 visit(node.returnType); | 352 visit(node.returnType); |
| 357 token(node.period); | 353 token(node.period); |
| 358 visit(node.name); | 354 visit(node.name); |
| 359 visit(node.parameters); | 355 visit(node.parameters); |
| 360 token(node.separator /* = or : */, precededBy: space, followedBy: space); | 356 token(node.separator /* = or : */, precededBy: space, followedBy: space); |
| 361 visitNodes(node.initializers, separatedBy: commaSeperator); | 357 visitNodes(node.initializers, separatedBy: commaSeperator); |
| 362 visit(node.redirectedConstructor); | 358 visit(node.redirectedConstructor); |
| 363 | 359 |
| 364 visitPrefixedBody(space, node.body); | 360 visitPrefixedBody(space, node.body); |
| 365 } | 361 } |
| 366 | 362 |
| 367 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { | 363 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { |
| 368 token(node.keyword); | 364 token(node.keyword); |
| 369 token(node.period); | 365 token(node.period); |
| 370 visit(node.fieldName); | 366 visit(node.fieldName); |
| 371 space(); | 367 space(); |
| 372 token(node.equals); | 368 token(node.equals); |
| 373 space(); | 369 space(); |
| 374 visit(node.expression); | 370 visit(node.expression); |
| 375 } | 371 } |
| 376 | 372 |
| 377 visitConstructorName(ConstructorName node) { | 373 visitConstructorName(ConstructorName node) { |
| 378 visit(node.type); | 374 visit(node.type); |
| 379 token(node.period); | 375 token(node.period); |
| 380 visit(node.name); | 376 visit(node.name); |
| 381 } | 377 } |
| 382 | 378 |
| 383 visitContinueStatement(ContinueStatement node) { | 379 visitContinueStatement(ContinueStatement node) { |
| 384 token(node.keyword); | 380 token(node.keyword); |
| 385 visitNode(node.label, precededBy: space); | 381 visitNode(node.label, precededBy: space); |
| 386 token(node.semicolon); | 382 token(node.semicolon); |
| 387 } | 383 } |
| 388 | 384 |
| 389 visitDeclaredIdentifier(DeclaredIdentifier node) { | 385 visitDeclaredIdentifier(DeclaredIdentifier node) { |
| 390 token(node.keyword); | 386 modifier(node.keyword); |
| 391 space(); | 387 visitNode(node.type, followedBy: space); |
| 392 visit(node.type); | |
| 393 //TODO(pquitslund): avoiding visitSuffixed(..) but we can do better | |
| 394 if (node.type != null) { | |
| 395 space(); | |
| 396 } | |
| 397 visit(node.identifier); | 388 visit(node.identifier); |
| 398 } | 389 } |
| 399 | 390 |
| 400 visitDefaultFormalParameter(DefaultFormalParameter node) { | 391 visitDefaultFormalParameter(DefaultFormalParameter node) { |
| 401 visit(node.parameter); | 392 visit(node.parameter); |
| 402 if (node.separator != null) { | 393 if (node.separator != null) { |
| 403 // The '=' separator is preceded by a space | 394 // The '=' separator is preceded by a space |
| 404 if (node.separator.type == TokenType.EQ) { | 395 if (node.separator.type == TokenType.EQ) { |
| 405 space(); | 396 space(); |
| 406 } | 397 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 visit(node.superclass); | 451 visit(node.superclass); |
| 461 } | 452 } |
| 462 | 453 |
| 463 visitFieldDeclaration(FieldDeclaration node) { | 454 visitFieldDeclaration(FieldDeclaration node) { |
| 464 modifier(node.keyword); | 455 modifier(node.keyword); |
| 465 visit(node.fields); | 456 visit(node.fields); |
| 466 token(node.semicolon); | 457 token(node.semicolon); |
| 467 } | 458 } |
| 468 | 459 |
| 469 visitFieldFormalParameter(FieldFormalParameter node) { | 460 visitFieldFormalParameter(FieldFormalParameter node) { |
| 470 token(node.keyword); | 461 token(node.keyword, followedBy: space); |
| 471 space(); | 462 visitNode(node.type, followedBy: space); |
| 472 visit(node.type); | |
| 473 space(); | |
| 474 token(node.thisToken); | 463 token(node.thisToken); |
| 475 token(node.period); | 464 token(node.period); |
| 476 visit(node.identifier); | 465 visit(node.identifier); |
| 477 visit(node.parameters); | 466 visit(node.parameters); |
| 478 } | 467 } |
| 479 | 468 |
| 480 visitForEachStatement(ForEachStatement node) { | 469 visitForEachStatement(ForEachStatement node) { |
| 481 token(node.forKeyword); | 470 token(node.forKeyword); |
| 482 space(); | 471 space(); |
| 483 token(node.leftParenthesis); | 472 token(node.leftParenthesis); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 space(); | 581 space(); |
| 593 visit(node.thenStatement); | 582 visit(node.thenStatement); |
| 594 //visitPrefixed(' else ', node.elseStatement); | 583 //visitPrefixed(' else ', node.elseStatement); |
| 595 if (node.elseStatement != null) { | 584 if (node.elseStatement != null) { |
| 596 space(); | 585 space(); |
| 597 token(node.elseKeyword); | 586 token(node.elseKeyword); |
| 598 space(); | 587 space(); |
| 599 visit(node.elseStatement); | 588 visit(node.elseStatement); |
| 600 } | 589 } |
| 601 } | 590 } |
| 602 | 591 |
| 603 visitImplementsClause(ImplementsClause node) { | 592 visitImplementsClause(ImplementsClause node) { |
| 604 token(node.keyword); | 593 token(node.keyword); |
| 605 space(); | 594 space(); |
| 606 visitNodes(node.interfaces, separatedBy: commaSeperator); | 595 visitNodes(node.interfaces, separatedBy: commaSeperator); |
| 607 } | 596 } |
| 608 | 597 |
| 609 visitImportDirective(ImportDirective node) { | 598 visitImportDirective(ImportDirective node) { |
| 610 token(node.keyword); | 599 token(node.keyword); |
| 611 space(); | 600 space(); |
| 612 visit(node.uri); | 601 visit(node.uri); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 visitPartDirective(PartDirective node) { | 747 visitPartDirective(PartDirective node) { |
| 759 token(node.keyword); | 748 token(node.keyword); |
| 760 space(); | 749 space(); |
| 761 visit(node.uri); | 750 visit(node.uri); |
| 762 token(node.semicolon); | 751 token(node.semicolon); |
| 763 } | 752 } |
| 764 | 753 |
| 765 visitPartOfDirective(PartOfDirective node) { | 754 visitPartOfDirective(PartOfDirective node) { |
| 766 token(node.keyword); | 755 token(node.keyword); |
| 767 space(); | 756 space(); |
| 757 token(node.ofToken); |
| 758 space(); |
| 768 visit(node.libraryName); | 759 visit(node.libraryName); |
| 769 token(node.semicolon); | 760 token(node.semicolon); |
| 770 } | 761 } |
| 771 | 762 |
| 772 visitPostfixExpression(PostfixExpression node) { | 763 visitPostfixExpression(PostfixExpression node) { |
| 773 visit(node.operand); | 764 visit(node.operand); |
| 774 token(node.operator); | 765 token(node.operator); |
| 775 } | 766 } |
| 776 | 767 |
| 777 visitPrefixedIdentifier(PrefixedIdentifier node) { | 768 visitPrefixedIdentifier(PrefixedIdentifier node) { |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 990 | 981 |
| 991 /// Visit the given function [body], printing the [prefix] before if given | 982 /// Visit the given function [body], printing the [prefix] before if given |
| 992 /// body is not empty. | 983 /// body is not empty. |
| 993 visitPrefixedBody(prefix(), FunctionBody body) { | 984 visitPrefixedBody(prefix(), FunctionBody body) { |
| 994 if (body is! EmptyFunctionBody) { | 985 if (body is! EmptyFunctionBody) { |
| 995 prefix(); | 986 prefix(); |
| 996 } | 987 } |
| 997 visit(body); | 988 visit(body); |
| 998 } | 989 } |
| 999 | 990 |
| 1000 /// Visit a list of [nodes] if not null, optionally separated and/or preceded | 991 /// Visit a list of [nodes] if not null, optionally separated and/or preceded |
| 1001 /// and followed by the given functions. | 992 /// and followed by the given functions. |
| 1002 visitNodes(NodeList<ASTNode> nodes, {precededBy(): null, | 993 visitNodes(NodeList<ASTNode> nodes, {precededBy(): null, |
| 1003 separatedBy() : null, followedBy(): null}) { | 994 separatedBy() : null, followedBy(): null}) { |
| 1004 if (nodes != null) { | 995 if (nodes != null) { |
| 1005 var size = nodes.length; | 996 var size = nodes.length; |
| 1006 if (size > 0) { | 997 if (size > 0) { |
| 1007 if (precededBy != null) { | 998 if (precededBy != null) { |
| 1008 precededBy(); | 999 precededBy(); |
| 1009 } | 1000 } |
| 1010 for (var i = 0; i < size; i++) { | 1001 for (var i = 0; i < size; i++) { |
| 1011 if (i > 0 && separatedBy != null) { | 1002 if (i > 0 && separatedBy != null) { |
| 1012 separatedBy(); | 1003 separatedBy(); |
| 1013 } | 1004 } |
| 1014 nodes[i].accept(this); | 1005 nodes[i].accept(this); |
| 1015 } | 1006 } |
| 1016 if (followedBy != null) { | 1007 if (followedBy != null) { |
| 1017 followedBy(); | 1008 followedBy(); |
| 1018 } | 1009 } |
| 1019 } | 1010 } |
| 1020 } | 1011 } |
| 1021 } | 1012 } |
| 1022 | 1013 |
| 1023 /// Visit a [node], and if not null, optionally preceded or followed by the | 1014 /// Visit a [node], and if not null, optionally preceded or followed by the |
| 1024 /// specified functions. | 1015 /// specified functions. |
| 1025 visitNode(ASTNode node, {precededBy(): null, followedBy(): null}) { | 1016 visitNode(ASTNode node, {precededBy(): null, followedBy(): null}) { |
| 1026 if (node != null) { | 1017 if (node != null) { |
| 1027 if (precededBy != null) { | 1018 if (precededBy != null) { |
| 1028 precededBy(); | 1019 precededBy(); |
| 1029 } | 1020 } |
| 1030 node.accept(this); | 1021 node.accept(this); |
| 1031 if (followedBy != null) { | 1022 if (followedBy != null) { |
| 1032 followedBy(); | 1023 followedBy(); |
| 1033 } | 1024 } |
| 1034 } | 1025 } |
| 1035 } | 1026 } |
| 1036 | 1027 |
| 1037 | 1028 |
| 1038 /// Emit the given [modifier] if it's non null, followed by non-breaking | 1029 /// Emit the given [modifier] if it's non null, followed by non-breaking |
| 1039 /// whitespace. | 1030 /// whitespace. |
| 1040 modifier(Token modifier) { | 1031 modifier(Token modifier) { |
| 1041 token(modifier, followedBy: space); | 1032 token(modifier, followedBy: space); |
| 1042 } | 1033 } |
| 1043 | 1034 |
| 1044 | 1035 |
| 1045 /// Indicate that at least one newline should be emitted and possibly more | 1036 /// Indicate that at least one newline should be emitted and possibly more |
| 1046 /// if the source has them. | 1037 /// if the source has them. |
| 1047 newlines() { | 1038 newlines() { |
| 1048 preservePrecedingNewlines = true; | 1039 preservePrecedingNewlines = true; |
| 1049 needsNewline = true; | 1040 needsNewline = true; |
| 1050 } | 1041 } |
| 1051 | 1042 |
| 1052 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) { | 1043 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) { |
| 1053 if (token != null) { | 1044 if (token != null) { |
| 1054 if (needsNewline) { | 1045 if (needsNewline) { |
| 1055 minNewlines = max(1, minNewlines); | 1046 minNewlines = max(1, minNewlines); |
| 1056 } | 1047 } |
| 1057 if (preservePrecedingNewlines || minNewlines > 0) { | 1048 if (preservePrecedingNewlines || minNewlines > 0) { |
| 1058 var emitted = emitPrecedingNewlines(token, min: minNewlines); | 1049 var emitted = emitPrecedingNewlines(token, min: minNewlines); |
| 1059 preservePrecedingNewlines = false; | 1050 preservePrecedingNewlines = false; |
| 1060 if (emitted > 0) { | 1051 if (emitted > 0) { |
| 1061 needsNewline = false; | 1052 needsNewline = false; |
| 1062 } | 1053 } |
| 1063 } | 1054 } |
| 1064 if (precededBy !=null) { | 1055 if (precededBy !=null) { |
| 1065 precededBy(); | 1056 precededBy(); |
| 1066 } | 1057 } |
| 1067 append(token.lexeme); | 1058 append(token.lexeme); |
| 1068 if (followedBy != null) { | 1059 if (followedBy != null) { |
| 1069 followedBy(); | 1060 followedBy(); |
| 1070 } | 1061 } |
| 1071 previousToken = token; | 1062 previousToken = token; |
| 1072 } | 1063 } |
| 1073 } | 1064 } |
| 1074 | 1065 |
| 1075 commaSeperator() { | 1066 commaSeperator() { |
| 1076 comma(); | 1067 comma(); |
| 1077 space(); | 1068 space(); |
| 1078 } | 1069 } |
| 1079 | 1070 |
| 1080 comma() { | 1071 comma() { |
| 1081 append(','); | 1072 append(','); |
| 1082 } | 1073 } |
| 1083 | 1074 |
| 1084 /// Emit a non-breakable space. | 1075 /// Emit a non-breakable space. |
| 1085 space() { | 1076 space() { |
| 1086 //TODO(pquitslund): replace with a proper space token | 1077 //TODO(pquitslund): replace with a proper space token |
| 1087 append(' '); | 1078 append(' '); |
| 1088 } | 1079 } |
| 1089 | 1080 |
| 1090 /// Emit a breakable space | 1081 /// Emit a breakable space |
| 1091 breakableSpace() { | 1082 breakableSpace() { |
| 1092 //Implement | 1083 //Implement |
| 1093 } | 1084 } |
| 1094 | 1085 |
| 1095 /// Append the given [string] to the source writer if it's non-null. | 1086 /// Append the given [string] to the source writer if it's non-null. |
| 1096 append(String string) { | 1087 append(String string) { |
| 1097 if (string != null) { | 1088 if (string != null) { |
| 1098 writer.print(string); | 1089 writer.print(string); |
| 1099 } | 1090 } |
| 1100 } | 1091 } |
| 1101 | 1092 |
| 1102 /// Indent. | 1093 /// Indent. |
| 1103 indent() { | 1094 indent() { |
| 1104 writer.indent(); | 1095 writer.indent(); |
| 1105 } | 1096 } |
| 1106 | 1097 |
| 1107 /// Unindent | 1098 /// Unindent |
| 1108 unindent() { | 1099 unindent() { |
| 1109 writer.unindent(); | 1100 writer.unindent(); |
| 1110 } | 1101 } |
| 1111 | 1102 |
| 1112 /// Emit any detected newlines or a minimum as specified by [minNewlines]. | 1103 /// Emit any detected newlines or a minimum as specified by [minNewlines]. |
| 1113 int emitPrecedingNewlines(Token token, {min: 0}) { | 1104 int emitPrecedingNewlines(Token token, {min: 0}) { |
| 1114 var comment = token.precedingComments; | 1105 var comment = token.precedingComments; |
| 1115 var currentToken = comment != null ? comment : token; | 1106 var currentToken = comment != null ? comment : token; |
| 1116 var lines = max(min, countNewlinesBetween(previousToken, currentToken)); | 1107 var lines = max(min, countNewlinesBetween(previousToken, currentToken)); |
| 1117 writer.newlines(lines); | 1108 writer.newlines(lines); |
| 1118 while (comment != null) { | 1109 while (comment != null) { |
| 1119 append(comment.toString().trim()); | 1110 append(comment.toString().trim()); |
| 1120 writer.newline(); | 1111 writer.newline(); |
| 1121 comment = comment.next; | 1112 comment = comment.next; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1143 return 0; | 1134 return 0; |
| 1144 } | 1135 } |
| 1145 var lastLine = | 1136 var lastLine = |
| 1146 lineInfo.getLocation(last.offset).lineNumber; | 1137 lineInfo.getLocation(last.offset).lineNumber; |
| 1147 var currentLine = | 1138 var currentLine = |
| 1148 lineInfo.getLocation(current.offset).lineNumber; | 1139 lineInfo.getLocation(current.offset).lineNumber; |
| 1149 return currentLine - lastLine; | 1140 return currentLine - lastLine; |
| 1150 } | 1141 } |
| 1151 | 1142 |
| 1152 String toString() => writer.toString(); | 1143 String toString() => writer.toString(); |
| 1153 | 1144 |
| 1154 } | 1145 } |
| OLD | NEW |