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

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

Issue 23301021: Formatter checkpoint (String literal elimination). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer_experimental/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_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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 visitList(node.strings, ' '); 167 visitNodes(node.strings, separatedBy: space);
168 } 168 }
169 169
170 visitAnnotation(Annotation node) { 170 visitAnnotation(Annotation node) {
171 token(node.atSign); 171 token(node.atSign);
172 visit(node.name); 172 visit(node.name);
173 visitPrefixed('.', node.constructorName); 173 token(node.period);
174 visit(node.constructorName);
174 visit(node.arguments); 175 visit(node.arguments);
175 } 176 }
176 177
177 visitArgumentDefinitionTest(ArgumentDefinitionTest node) { 178 visitArgumentDefinitionTest(ArgumentDefinitionTest node) {
178 token(node.question); 179 token(node.question);
179 visit(node.identifier); 180 visit(node.identifier);
180 } 181 }
181 182
182 visitArgumentList(ArgumentList node) { 183 visitArgumentList(ArgumentList node) {
183 token(node.leftParenthesis); 184 token(node.leftParenthesis);
184 visitList(node.arguments, ', '); 185 visitNodes(node.arguments, separatedBy: commaSeperator);
185 token(node.rightParenthesis); 186 token(node.rightParenthesis);
186 } 187 }
187 188
188 visitAsExpression(AsExpression node) { 189 visitAsExpression(AsExpression node) {
189 visit(node.expression); 190 visit(node.expression);
190 space(); 191 space();
191 token(node.asOperator); 192 token(node.asOperator);
192 space(); 193 space();
193 visit(node.type); 194 visit(node.type);
194 } 195 }
(...skipping 18 matching lines...) Expand all
213 visitBinaryExpression(BinaryExpression node) { 214 visitBinaryExpression(BinaryExpression node) {
214 visit(node.leftOperand); 215 visit(node.leftOperand);
215 space(); 216 space();
216 token(node.operator); 217 token(node.operator);
217 space(); 218 space();
218 visit(node.rightOperand); 219 visit(node.rightOperand);
219 } 220 }
220 221
221 visitBlock(Block node) { 222 visitBlock(Block node) {
222 token(node.leftBracket); 223 token(node.leftBracket);
223 needsNewline = true;
224 indent(); 224 indent();
225 225
226 for (var stmt in node.statements) { 226 for (var stmt in node.statements) {
227 newlines();
227 visit(stmt); 228 visit(stmt);
228 } 229 }
Brian Wilkerson 2013/08/23 19:25:48 Should this (eventually) be visitNodes(node.sta
pquitslund 2013/08/23 20:16:08 No time like now! :) Thanks for the catch.
229 230
230 unindent(); 231 unindent();
231 preservePrecedingNewlines = true; 232 newlines();
232 needsNewline = true;
233 token(node.rightBracket); 233 token(node.rightBracket);
234 } 234 }
235 235
236 visitBlockFunctionBody(BlockFunctionBody node) { 236 visitBlockFunctionBody(BlockFunctionBody node) {
237 visit(node.block); 237 visit(node.block);
238 } 238 }
239 239
240 visitBooleanLiteral(BooleanLiteral node) { 240 visitBooleanLiteral(BooleanLiteral node) {
241 token(node.literal); 241 token(node.literal);
242 } 242 }
243 243
244 visitBreakStatement(BreakStatement node) { 244 visitBreakStatement(BreakStatement node) {
245 preservePrecedingNewlines = true;
246 token(node.keyword); 245 token(node.keyword);
247 visitPrefixed(' ', node.label); 246 visitNode(node.label, precededBy: space);
248 token(node.semicolon); 247 token(node.semicolon);
249 needsNewline = true;
250 } 248 }
251 249
252 visitCascadeExpression(CascadeExpression node) { 250 visitCascadeExpression(CascadeExpression node) {
253 visit(node.target); 251 visit(node.target);
254 visitList(node.cascadeSections); 252 visitNodes(node.cascadeSections);
255 } 253 }
256 254
257 visitCatchClause(CatchClause node) { 255 visitCatchClause(CatchClause node) {
258 visitPrefixed('on ', node.exceptionType); 256
257 token(node.onKeyword, precededBy: space, followedBy: space);
258 visit(node.exceptionType);
259
259 if (node.catchKeyword != null) { 260 if (node.catchKeyword != null) {
260 if (node.exceptionType != null) { 261 if (node.exceptionType != null) {
261 space(); 262 space();
262 } 263 }
263 token(node.catchKeyword); 264 token(node.catchKeyword);
264 space(); 265 space();
265 token(node.leftParenthesis); 266 token(node.leftParenthesis);
266 visit(node.exceptionParameter); 267 visit(node.exceptionParameter);
267 visitPrefixed(', ', node.stackTraceParameter); 268 token(node.comma, followedBy: space);
269 visit(node.stackTraceParameter);
268 token(node.rightParenthesis); 270 token(node.rightParenthesis);
269 space(); 271 space();
270 } else { 272 } else {
271 space(); 273 space();
272 } 274 }
273 visit(node.body); 275 visit(node.body);
274 needsNewline = true;
275 } 276 }
276 277
277 visitClassDeclaration(ClassDeclaration node) { 278 visitClassDeclaration(ClassDeclaration node) {
278 preservePrecedingNewlines = true;
279 modifier(node.abstractKeyword); 279 modifier(node.abstractKeyword);
280 token(node.classKeyword); 280 token(node.classKeyword);
281 space(); 281 space();
282 visit(node.name); 282 visit(node.name);
283 visit(node.typeParameters); 283 visit(node.typeParameters);
284 visitPrefixed(' ', node.extendsClause); 284 visitNode(node.extendsClause, precededBy: space);
285 visitPrefixed(' ', node.withClause); 285 visitNode(node.withClause, precededBy: space);
286 visitPrefixed(' ', node.implementsClause); 286 visitNode(node.implementsClause, precededBy: space);
287 space(); 287 space();
288 token(node.leftBracket); 288 token(node.leftBracket);
289
289 indent(); 290 indent();
290 291 visitNodes(node.members, precededBy: newlines, separatedBy: newlines);
291 for (var i = 0; i < node.members.length; i++) {
292 visit(node.members[i]);
293 }
294
295 unindent(); 292 unindent();
296 293 newlines();
297 emitPrecedingNewlines(node.rightBracket, min: 1); 294
298 token(node.rightBracket); 295 token(node.rightBracket);
299 } 296 }
300 297
301 visitClassTypeAlias(ClassTypeAlias node) { 298 visitClassTypeAlias(ClassTypeAlias node) {
302 token(node.keyword); 299 token(node.keyword);
303 space(); 300 space();
304 visit(node.name); 301 visit(node.name);
305 visit(node.typeParameters); 302 visit(node.typeParameters);
306 space(); 303 space();
307 token(node.equals); 304 token(node.equals);
308 space(); 305 space();
309 if (node.abstractKeyword != null) { 306 if (node.abstractKeyword != null) {
310 token(node.abstractKeyword); 307 token(node.abstractKeyword);
311 space(); 308 space();
312 } 309 }
313 visit(node.superclass); 310 visit(node.superclass);
314 visitPrefixed(' ', node.withClause); 311 visitNode(node.withClause, precededBy: space);
315 visitPrefixed(' ', node.implementsClause); 312 visitNode(node.implementsClause, precededBy: space);
316 token(node.semicolon); 313 token(node.semicolon);
317 } 314 }
318 315
319 visitComment(Comment node) => null; 316 visitComment(Comment node) => null;
320 317
321 visitCommentReference(CommentReference node) => null; 318 visitCommentReference(CommentReference node) => null;
322 319
323 visitCompilationUnit(CompilationUnit node) { 320 visitCompilationUnit(CompilationUnit node) {
324 321
325 // Cache EOF for leading whitespace calculation 322 // Cache EOF for leading whitespace calculation
326 var start = node.beginToken.previous; 323 var start = node.beginToken.previous;
327 if (start != null && start.type is TokenType_EOF) { 324 if (start != null && start.type is TokenType_EOF) {
328 previousToken = start; 325 previousToken = start;
329 } 326 }
330 327
331 var scriptTag = node.scriptTag; 328 var scriptTag = node.scriptTag;
332 var directives = node.directives; 329 var directives = node.directives;
333 visit(scriptTag); 330 visit(scriptTag);
334 visitList(directives); 331
335 visitList(node.declarations); 332 preservePrecedingNewlines = true;
333 visitNodes(directives, separatedBy: newlines);
334
335 preservePrecedingNewlines = true;
336 visitNodes(node.declarations, separatedBy: newlines);
336 337
337 // Handle trailing whitespace 338 // Handle trailing whitespace
338 preservePrecedingNewlines = true; 339 preservePrecedingNewlines = true;
339 token(node.endToken /* EOF */); 340 token(node.endToken /* EOF */);
340 } 341 }
341 342
342 visitConditionalExpression(ConditionalExpression node) { 343 visitConditionalExpression(ConditionalExpression node) {
343 visit(node.condition); 344 visit(node.condition);
344 space(); 345 space();
345 token(node.question); 346 token(node.question);
346 space(); 347 space();
347 visit(node.thenExpression); 348 visit(node.thenExpression);
348 space(); 349 space();
349 token(node.colon); 350 token(node.colon);
350 space(); 351 space();
351 visit(node.elseExpression); 352 visit(node.elseExpression);
352 } 353 }
353 354
354 visitConstructorDeclaration(ConstructorDeclaration node) { 355 visitConstructorDeclaration(ConstructorDeclaration node) {
355 modifier(node.externalKeyword); 356 modifier(node.externalKeyword);
356 modifier(node.constKeyword); 357 modifier(node.constKeyword);
357 modifier(node.factoryKeyword); 358 modifier(node.factoryKeyword);
358 visit(node.returnType); 359 visit(node.returnType);
359 visitPrefixed('.', node.name); 360 token(node.period);
361 visit(node.name);
362 node.period;
Brian Wilkerson 2013/08/23 19:25:48 Remove this line?
pquitslund 2013/08/23 20:16:08 Done.
360 visit(node.parameters); 363 visit(node.parameters);
361 visitPrefixedList(' : ', node.initializers, ', '); 364 token(node.separator /* = or : */, precededBy: space, followedBy: space);
362 visitPrefixed(' = ', node.redirectedConstructor); 365 visitNodes(node.initializers, separatedBy: commaSeperator);
363 visitPrefixedBody(' ', node.body); 366 visit(node.redirectedConstructor);
367
368 visitPrefixedBody(space, node.body);
364 } 369 }
365 370
366 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { 371 visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
367 token(node.keyword); 372 token(node.keyword);
368 token(node.period); 373 token(node.period);
369 visit(node.fieldName); 374 visit(node.fieldName);
370 space(); 375 space();
371 token(node.equals); 376 token(node.equals);
372 space(); 377 space();
373 visit(node.expression); 378 visit(node.expression);
374 } 379 }
375 380
376 visitConstructorName(ConstructorName node) { 381 visitConstructorName(ConstructorName node) {
377 visit(node.type); 382 visit(node.type);
378 visitPrefixed('.', node.name); 383 token(node.period);
384 visit(node.name);
379 } 385 }
380 386
381 visitContinueStatement(ContinueStatement node) { 387 visitContinueStatement(ContinueStatement node) {
382 token(node.keyword); 388 token(node.keyword);
383 visitPrefixed(' ', node.label); 389 visitNode(node.label, precededBy: space);
384 token(node.semicolon); 390 token(node.semicolon);
385 } 391 }
386 392
387 visitDeclaredIdentifier(DeclaredIdentifier node) { 393 visitDeclaredIdentifier(DeclaredIdentifier node) {
388 token(node.keyword); 394 token(node.keyword);
389 space(); 395 space();
390 visit(node.type); 396 visit(node.type);
391 //TODO(pquitslund): avoiding visitSuffixed(..) but we can do better 397 //TODO(pquitslund): avoiding visitSuffixed(..) but we can do better
392 if (node.type != null) { 398 if (node.type != null) {
393 space(); 399 space();
394 } 400 }
395 visit(node.identifier); 401 visit(node.identifier);
396 } 402 }
397 403
398 visitDefaultFormalParameter(DefaultFormalParameter node) { 404 visitDefaultFormalParameter(DefaultFormalParameter node) {
399 visit(node.parameter); 405 visit(node.parameter);
400 if (node.separator != null) { 406 if (node.separator != null) {
401 space(); 407 // The '=' separator is preceded by a space
408 if (node.separator.type == TokenType.EQ) {
409 space();
410 }
402 token(node.separator); 411 token(node.separator);
403 visitPrefixed(' ', node.defaultValue); 412 visitNode(node.defaultValue, precededBy: space);
404 } 413 }
405 } 414 }
406 415
407 visitDoStatement(DoStatement node) { 416 visitDoStatement(DoStatement node) {
408 token(node.doKeyword); 417 token(node.doKeyword);
409 space(); 418 space();
410 visit(node.body); 419 visit(node.body);
411 space(); 420 space();
412 token(node.whileKeyword); 421 token(node.whileKeyword);
413 space(); 422 space();
(...skipping 12 matching lines...) Expand all
426 } 435 }
427 436
428 visitEmptyStatement(EmptyStatement node) { 437 visitEmptyStatement(EmptyStatement node) {
429 token(node.semicolon); 438 token(node.semicolon);
430 } 439 }
431 440
432 visitExportDirective(ExportDirective node) { 441 visitExportDirective(ExportDirective node) {
433 token(node.keyword); 442 token(node.keyword);
434 space(); 443 space();
435 visit(node.uri); 444 visit(node.uri);
436 visitPrefixedList(' ', node.combinators, ' '); 445 visitNodes(node.combinators, precededBy: space, separatedBy: space);
437 token(node.semicolon); 446 token(node.semicolon);
438 } 447 }
439 448
440 visitExpressionFunctionBody(ExpressionFunctionBody node) { 449 visitExpressionFunctionBody(ExpressionFunctionBody node) {
441 token(node.functionDefinition); 450 token(node.functionDefinition);
442 space(); 451 space();
443 visit(node.expression); 452 visit(node.expression);
444 token(node.semicolon); 453 token(node.semicolon);
445 } 454 }
446 455
447 visitExpressionStatement(ExpressionStatement node) { 456 visitExpressionStatement(ExpressionStatement node) {
448 visit(node.expression); 457 visit(node.expression);
449 token(node.semicolon); 458 token(node.semicolon);
450 } 459 }
451 460
452 visitExtendsClause(ExtendsClause node) { 461 visitExtendsClause(ExtendsClause node) {
453 token(node.keyword); 462 token(node.keyword);
454 space(); 463 space();
455 visit(node.superclass); 464 visit(node.superclass);
456 } 465 }
457 466
458 visitFieldDeclaration(FieldDeclaration node) { 467 visitFieldDeclaration(FieldDeclaration node) {
459 needsNewline = true;
460 preservePrecedingNewlines = true;
461 modifier(node.keyword); 468 modifier(node.keyword);
462 visit(node.fields); 469 visit(node.fields);
463 token(node.semicolon); 470 token(node.semicolon);
464 } 471 }
465 472
466 visitFieldFormalParameter(FieldFormalParameter node) { 473 visitFieldFormalParameter(FieldFormalParameter node) {
467 token(node.keyword); 474 token(node.keyword);
468 space(); 475 space();
469 visit(node.type); 476 visit(node.type);
470 space(); 477 space();
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 if (groupEnd != null) { 519 if (groupEnd != null) {
513 append(groupEnd); 520 append(groupEnd);
514 } 521 }
515 token(node.rightParenthesis); 522 token(node.rightParenthesis);
516 } 523 }
517 524
518 visitForStatement(ForStatement node) { 525 visitForStatement(ForStatement node) {
519 token(node.forKeyword); 526 token(node.forKeyword);
520 space(); 527 space();
521 token(node.leftParenthesis); 528 token(node.leftParenthesis);
522 var initialization = node.initialization; 529 if (node.initialization != null) {
523 if (initialization != null) { 530 visit(node.initialization);
524 visit(initialization);
525 } else { 531 } else {
526 visit(node.variables); 532 visit(node.variables);
527 } 533 }
528 token(node.leftSeparator); 534 token(node.leftSeparator);
529 visitPrefixed(' ', node.condition); 535 space();
Brian Wilkerson 2013/08/23 19:25:48 Do we always want a space, or only then there is a
pquitslund 2013/08/23 20:16:08 Great question. My gut is always. Happy to be sw
536 visit(node.condition);
530 token(node.rightSeparator); 537 token(node.rightSeparator);
531 visitPrefixedList(' ', node.updaters, ', '); 538 visitNodes(node.updaters, precededBy: space, separatedBy: space);
532 token(node.leftParenthesis); 539 token(node.rightParenthesis);
533 space(); 540 space();
534 visit(node.body); 541 visit(node.body);
535 } 542 }
536 543
537 visitFunctionDeclaration(FunctionDeclaration node) { 544 visitFunctionDeclaration(FunctionDeclaration node) {
538 needsNewline = true; 545 visitNode(node.returnType, followedBy: space);
539 preservePrecedingNewlines = true;
540 visitSuffixed(node.returnType, ' ');
541 token(node.propertyKeyword, followedBy: space); 546 token(node.propertyKeyword, followedBy: space);
542 visit(node.name); 547 visit(node.name);
543 visit(node.functionExpression); 548 visit(node.functionExpression);
544 } 549 }
545 550
546 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) { 551 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) {
547 visit(node.functionDeclaration); 552 visit(node.functionDeclaration);
548 // TODO(pquitslund): fix and handle in function body 553 // TODO(pquitslund): fix and handle in function body
549 append(';'); 554 append(';');
550 } 555 }
551 556
552 visitFunctionExpression(FunctionExpression node) { 557 visitFunctionExpression(FunctionExpression node) {
553 visit(node.parameters); 558 visit(node.parameters);
554 space(); 559 space();
555 visit(node.body); 560 visit(node.body);
556 } 561 }
557 562
558 visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { 563 visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
559 visit(node.function); 564 visit(node.function);
560 visit(node.argumentList); 565 visit(node.argumentList);
561 } 566 }
562 567
563 visitFunctionTypeAlias(FunctionTypeAlias node) { 568 visitFunctionTypeAlias(FunctionTypeAlias node) {
564 token(node.keyword); 569 token(node.keyword);
565 space(); 570 space();
566 visitSuffixed(node.returnType, ' '); 571 visitNode(node.returnType, followedBy: space);
567 visit(node.name); 572 visit(node.name);
568 visit(node.typeParameters); 573 visit(node.typeParameters);
569 visit(node.parameters); 574 visit(node.parameters);
570 token(node.semicolon); 575 token(node.semicolon);
571 } 576 }
572 577
573 visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { 578 visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) {
574 visitSuffixed(node.returnType, ' '); 579 visitNode(node.returnType, followedBy: space);
575 visit(node.identifier); 580 visit(node.identifier);
576 visit(node.parameters); 581 visit(node.parameters);
577 } 582 }
578 583
579 visitHideCombinator(HideCombinator node) { 584 visitHideCombinator(HideCombinator node) {
580 token(node.keyword); 585 token(node.keyword);
581 space(); 586 space();
582 visitList(node.hiddenNames, ', '); 587 visitNodes(node.hiddenNames, separatedBy: commaSeperator);
583 } 588 }
584 589
585 visitIfStatement(IfStatement node) { 590 visitIfStatement(IfStatement node) {
586 preservePrecedingNewlines = true;
587 token(node.ifKeyword); 591 token(node.ifKeyword);
588 space(); 592 space();
589 token(node.leftParenthesis); 593 token(node.leftParenthesis);
590 visit(node.condition); 594 visit(node.condition);
591 token(node.rightParenthesis); 595 token(node.rightParenthesis);
592 space(); 596 space();
593 visit(node.thenStatement); 597 visit(node.thenStatement);
594 //visitPrefixed(' else ', node.elseStatement); 598 //visitPrefixed(' else ', node.elseStatement);
595 if (node.elseStatement != null) { 599 if (node.elseStatement != null) {
596 space(); 600 space();
597 token(node.elseKeyword); 601 token(node.elseKeyword);
598 space(); 602 space();
599 visit(node.elseStatement); 603 visit(node.elseStatement);
600 } 604 }
601 needsNewline = true;
602 } 605 }
603 606
604 visitImplementsClause(ImplementsClause node) { 607 visitImplementsClause(ImplementsClause node) {
605 token(node.keyword); 608 token(node.keyword);
606 space(); 609 space();
607 visitList(node.interfaces, ', '); 610 visitNodes(node.interfaces, separatedBy: commaSeperator);
608 } 611 }
609 612
610 visitImportDirective(ImportDirective node) { 613 visitImportDirective(ImportDirective node) {
611 preservePrecedingNewlines = true;
612 token(node.keyword); 614 token(node.keyword);
613 space(); 615 space();
614 visit(node.uri); 616 visit(node.uri);
615 visitPrefixed(' as ', node.prefix); 617 token(node.asToken, precededBy: space, followedBy: space);
616 visitPrefixedList(' ', node.combinators, ' '); 618 visit(node.prefix);
619 visitNodes(node.combinators, precededBy: space, separatedBy: space);
617 token(node.semicolon); 620 token(node.semicolon);
618 needsNewline = true;
619 } 621 }
620 622
621 visitIndexExpression(IndexExpression node) { 623 visitIndexExpression(IndexExpression node) {
622 if (node.isCascaded) { 624 if (node.isCascaded) {
623 token(node.period); 625 token(node.period);
624 } else { 626 } else {
625 visit(node.target); 627 visit(node.target);
626 } 628 }
627 token(node.leftBracket); 629 token(node.leftBracket);
628 visit(node.index); 630 visit(node.index);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 space(); 665 space();
664 visit(node.type); 666 visit(node.type);
665 } 667 }
666 668
667 visitLabel(Label node) { 669 visitLabel(Label node) {
668 visit(node.label); 670 visit(node.label);
669 token(node.colon); 671 token(node.colon);
670 } 672 }
671 673
672 visitLabeledStatement(LabeledStatement node) { 674 visitLabeledStatement(LabeledStatement node) {
673 visitSuffixedList(node.labels, ' ', ' '); 675 visitNodes(node.labels, separatedBy: space, followedBy: space);
674 visit(node.statement); 676 visit(node.statement);
675 } 677 }
676 678
677 visitLibraryDirective(LibraryDirective node) { 679 visitLibraryDirective(LibraryDirective node) {
678 token(node.keyword); 680 token(node.keyword);
679 space(); 681 space();
680 visit(node.name); 682 visit(node.name);
681 token(node.semicolon); 683 token(node.semicolon);
682 } 684 }
683 685
684 visitLibraryIdentifier(LibraryIdentifier node) { 686 visitLibraryIdentifier(LibraryIdentifier node) {
685 append(node.name); 687 append(node.name);
686 } 688 }
687 689
688 visitListLiteral(ListLiteral node) { 690 visitListLiteral(ListLiteral node) {
689 if (node.modifier != null) { 691 modifier(node.modifier);
690 token(node.modifier);
691 space();
692 }
693 visit(node.typeArguments); 692 visit(node.typeArguments);
694 token(node.leftBracket); 693 token(node.leftBracket);
695 visitList(node.elements, ', '); 694 visitNodes(node.elements, separatedBy: commaSeperator);
696 token(node.rightBracket); 695 token(node.rightBracket);
697 } 696 }
698 697
699 visitMapLiteral(MapLiteral node) { 698 visitMapLiteral(MapLiteral node) {
700 modifier(node.modifier); 699 modifier(node.modifier);
701 visitSuffixed(node.typeArguments, ' '); 700 visitNode(node.typeArguments, followedBy: space);
702 token(node.leftBracket); 701 token(node.leftBracket);
703 visitList(node.entries, ', '); 702 visitNodes(node.entries, separatedBy: commaSeperator);
704 token(node.rightBracket); 703 token(node.rightBracket);
705 } 704 }
706 705
707 visitMapLiteralEntry(MapLiteralEntry node) { 706 visitMapLiteralEntry(MapLiteralEntry node) {
708 visit(node.key); 707 visit(node.key);
709 space(); 708 space();
710 token(node.separator); 709 token(node.separator);
711 space(); 710 space();
712 visit(node.value); 711 visit(node.value);
713 } 712 }
714 713
715 visitMethodDeclaration(MethodDeclaration node) { 714 visitMethodDeclaration(MethodDeclaration node) {
716 needsNewline = true;
717 preservePrecedingNewlines = true;
718 modifier(node.externalKeyword); 715 modifier(node.externalKeyword);
719 modifier(node.modifierKeyword); 716 modifier(node.modifierKeyword);
720 visitSuffixed(node.returnType, ' '); 717 visitNode(node.returnType, followedBy: space);
721 modifier(node.propertyKeyword); 718 modifier(node.propertyKeyword);
722 modifier(node.operatorKeyword); 719 modifier(node.operatorKeyword);
723 visit(node.name); 720 visit(node.name);
724 if (!node.isGetter) { 721 if (!node.isGetter) {
725 visit(node.parameters); 722 visit(node.parameters);
726 } 723 }
727 visitPrefixedBody(' ', node.body); 724 visitPrefixedBody(space, node.body);
728 } 725 }
729 726
730 visitMethodInvocation(MethodInvocation node) { 727 visitMethodInvocation(MethodInvocation node) {
731 if (node.isCascaded) { 728 visit(node.target);
732 token(node.period); 729 token(node.period);
733 } else {
734 visitSuffixed(node.target, '.');
735 }
736 visit(node.methodName); 730 visit(node.methodName);
737 visit(node.argumentList); 731 visit(node.argumentList);
738 } 732 }
739 733
740 visitNamedExpression(NamedExpression node) { 734 visitNamedExpression(NamedExpression node) {
741 visit(node.name); 735 visit(node.name);
742 visitPrefixed(' ', node.expression); 736 visitNode(node.expression, precededBy: space);
743 } 737 }
744 738
745 visitNativeClause(NativeClause node) { 739 visitNativeClause(NativeClause node) {
746 token(node.keyword); 740 token(node.keyword);
747 space(); 741 space();
748 visit(node.name); 742 visit(node.name);
749 } 743 }
750 744
751 visitNativeFunctionBody(NativeFunctionBody node) { 745 visitNativeFunctionBody(NativeFunctionBody node) {
752 token(node.nativeToken); 746 token(node.nativeToken);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 token(node.operator); 794 token(node.operator);
801 } else { 795 } else {
802 visit(node.target); 796 visit(node.target);
803 token(node.operator); 797 token(node.operator);
804 } 798 }
805 visit(node.propertyName); 799 visit(node.propertyName);
806 } 800 }
807 801
808 visitRedirectingConstructorInvocation(RedirectingConstructorInvocation node) { 802 visitRedirectingConstructorInvocation(RedirectingConstructorInvocation node) {
809 token(node.keyword); 803 token(node.keyword);
810 visitPrefixed('.', node.constructorName); 804 token(node.period);
805 visit(node.constructorName);
811 visit(node.argumentList); 806 visit(node.argumentList);
812 } 807 }
813 808
814 visitRethrowExpression(RethrowExpression node) { 809 visitRethrowExpression(RethrowExpression node) {
815 token(node.keyword); 810 token(node.keyword);
816 } 811 }
817 812
818 visitReturnStatement(ReturnStatement node) { 813 visitReturnStatement(ReturnStatement node) {
819 preservePrecedingNewlines = true;
820 var expression = node.expression; 814 var expression = node.expression;
821 if (expression == null) { 815 if (expression == null) {
822 token(node.keyword); 816 token(node.keyword);
823 token(node.semicolon); 817 token(node.semicolon);
824 } else { 818 } else {
825 token(node.keyword); 819 token(node.keyword);
826 space(); 820 space();
827 expression.accept(this); 821 expression.accept(this);
828 token(node.semicolon); 822 token(node.semicolon);
829 } 823 }
830 } 824 }
831 825
832 visitScriptTag(ScriptTag node) { 826 visitScriptTag(ScriptTag node) {
833 token(node.scriptTag); 827 token(node.scriptTag);
834 } 828 }
835 829
836 visitShowCombinator(ShowCombinator node) { 830 visitShowCombinator(ShowCombinator node) {
837 token(node.keyword); 831 token(node.keyword);
838 space(); 832 space();
839 visitList(node.shownNames, ', '); 833 visitNodes(node.shownNames, separatedBy: commaSeperator);
840 } 834 }
841 835
842 visitSimpleFormalParameter(SimpleFormalParameter node) { 836 visitSimpleFormalParameter(SimpleFormalParameter node) {
843 modifier(node.keyword); 837 modifier(node.keyword);
844 visitSuffixed(node.type, ' '); 838 visitNode(node.type, followedBy: space);
845 visit(node.identifier); 839 visit(node.identifier);
846 } 840 }
847 841
848 visitSimpleIdentifier(SimpleIdentifier node) { 842 visitSimpleIdentifier(SimpleIdentifier node) {
849 token(node.token); 843 token(node.token);
850 } 844 }
851 845
852 visitSimpleStringLiteral(SimpleStringLiteral node) { 846 visitSimpleStringLiteral(SimpleStringLiteral node) {
853 token(node.literal); 847 token(node.literal);
854 } 848 }
855 849
856 visitStringInterpolation(StringInterpolation node) { 850 visitStringInterpolation(StringInterpolation node) {
857 visitList(node.elements); 851 visitNodes(node.elements);
858 } 852 }
859 853
860 visitSuperConstructorInvocation(SuperConstructorInvocation node) { 854 visitSuperConstructorInvocation(SuperConstructorInvocation node) {
861 token(node.keyword); 855 token(node.keyword);
862 visitPrefixed('.', node.constructorName); 856 token(node.period);
857 visit(node.constructorName);
863 visit(node.argumentList); 858 visit(node.argumentList);
864 } 859 }
865 860
866 visitSuperExpression(SuperExpression node) { 861 visitSuperExpression(SuperExpression node) {
867 token(node.keyword); 862 token(node.keyword);
868 } 863 }
869 864
870 visitSwitchCase(SwitchCase node) { 865 visitSwitchCase(SwitchCase node) {
871 preservePrecedingNewlines = true; 866 visitNodes(node.labels, separatedBy: space, followedBy: space);
872 visitSuffixedList(node.labels, ' ', ' ');
873 token(node.keyword); 867 token(node.keyword);
874 space(); 868 space();
875 visit(node.expression); 869 visit(node.expression);
876 token(node.colon); 870 token(node.colon);
871 newlines();
877 indent(); 872 indent();
878 needsNewline = true; 873 visitNodes(node.statements, separatedBy: newlines);
879 visitList(node.statements);
880 unindent(); 874 unindent();
881 } 875 }
882 876
883 visitSwitchDefault(SwitchDefault node) { 877 visitSwitchDefault(SwitchDefault node) {
884 preservePrecedingNewlines = true; 878 visitNodes(node.labels, separatedBy: space, followedBy: space);
885 visitSuffixedList(node.labels, ' ', ' ');
886 token(node.keyword); 879 token(node.keyword);
887 token(node.colon); 880 token(node.colon);
888 space(); 881 space();
889 visitList(node.statements, ' '); 882 visitNodes(node.statements, separatedBy: space);
890 } 883 }
891 884
892 visitSwitchStatement(SwitchStatement node) { 885 visitSwitchStatement(SwitchStatement node) {
893 token(node.keyword); 886 token(node.keyword);
894 space(); 887 space();
895 token(node.leftParenthesis); 888 token(node.leftParenthesis);
896 visit(node.expression); 889 visit(node.expression);
897 token(node.rightParenthesis); 890 token(node.rightParenthesis);
898 space(); 891 space();
899 token(node.leftBracket); 892 token(node.leftBracket);
900 indent(); 893 indent();
901 visitList(node.members); 894 newlines();
895 visitNodes(node.members, separatedBy: newlines, followedBy: newlines);
902 unindent(); 896 unindent();
903 token(node.rightBracket); 897 token(node.rightBracket);
904 needsNewline = true;
905 } 898 }
906 899
907 visitSymbolLiteral(SymbolLiteral node) { 900 visitSymbolLiteral(SymbolLiteral node) {
908 // No-op ? 901 // No-op ?
909 } 902 }
910 903
911 visitThisExpression(ThisExpression node) { 904 visitThisExpression(ThisExpression node) {
912 token(node.keyword); 905 token(node.keyword);
913 } 906 }
914 907
915 visitThrowExpression(ThrowExpression node) { 908 visitThrowExpression(ThrowExpression node) {
916 token(node.keyword); 909 token(node.keyword);
917 space(); 910 space();
918 visit(node.expression); 911 visit(node.expression);
919 } 912 }
920 913
921 visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { 914 visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
922 preservePrecedingNewlines = true;
923 visit(node.variables); 915 visit(node.variables);
924 token(node.semicolon); 916 token(node.semicolon);
925 } 917 }
926 918
927 visitTryStatement(TryStatement node) { 919 visitTryStatement(TryStatement node) {
928 preservePrecedingNewlines = true;
929 token(node.tryKeyword); 920 token(node.tryKeyword);
930 space(); 921 space();
931 visit(node.body); 922 visit(node.body);
932 visitPrefixedList(' ', node.catchClauses, ' '); 923 visitNodes(node.catchClauses, precededBy: space, separatedBy: space);
933 visitPrefixed(' finally ', node.finallyClause); 924 token(node.finallyKeyword, precededBy: space, followedBy: space);
925 visit(node.finallyClause);
934 } 926 }
935 927
936 visitTypeArgumentList(TypeArgumentList node) { 928 visitTypeArgumentList(TypeArgumentList node) {
937 token(node.leftBracket); 929 token(node.leftBracket);
938 visitList(node.arguments, ', '); 930 visitNodes(node.arguments, separatedBy: commaSeperator);
939 token(node.rightBracket); 931 token(node.rightBracket);
940 } 932 }
941 933
942 visitTypeName(TypeName node) { 934 visitTypeName(TypeName node) {
943 visit(node.name); 935 visit(node.name);
944 visit(node.typeArguments); 936 visit(node.typeArguments);
945 } 937 }
946 938
947 visitTypeParameter(TypeParameter node) { 939 visitTypeParameter(TypeParameter node) {
948 visit(node.name); 940 visit(node.name);
949 visitPrefixed(' extends ', node.bound); 941 token(node.keyword /* extends */, precededBy: space, followedBy: space);
942 visit(node.bound);
950 } 943 }
951 944
952 visitTypeParameterList(TypeParameterList node) { 945 visitTypeParameterList(TypeParameterList node) {
953 token(node.leftBracket); 946 token(node.leftBracket);
954 visitList(node.typeParameters, ', '); 947 visitNodes(node.typeParameters, separatedBy: commaSeperator);
955 token(node.rightBracket); 948 token(node.rightBracket);
956 } 949 }
957 950
958 visitVariableDeclaration(VariableDeclaration node) { 951 visitVariableDeclaration(VariableDeclaration node) {
959 visit(node.name); 952 visit(node.name);
960 if (node.initializer != null) { 953 if (node.initializer != null) {
961 space(); 954 space();
962 token(node.equals); 955 token(node.equals);
963 space(); 956 space();
964 visit(node.initializer); 957 visit(node.initializer);
965 } 958 }
966 } 959 }
967 960
968 visitVariableDeclarationList(VariableDeclarationList node) { 961 visitVariableDeclarationList(VariableDeclarationList node) {
969 token(node.keyword); 962 modifier(node.keyword);
970 space(); 963 visitNode(node.type, followedBy: space);
971 visitSuffixed(node.type, ' '); 964 visitNodes(node.variables, separatedBy: commaSeperator);
972 visitList(node.variables, ', ');
973 } 965 }
974 966
975 visitVariableDeclarationStatement(VariableDeclarationStatement node) { 967 visitVariableDeclarationStatement(VariableDeclarationStatement node) {
976 visit(node.variables); 968 visit(node.variables);
977 token(node.semicolon); 969 token(node.semicolon);
978 needsNewline = true;
979 } 970 }
980 971
981 visitWhileStatement(WhileStatement node) { 972 visitWhileStatement(WhileStatement node) {
982 token(node.keyword); 973 token(node.keyword);
983 space(); 974 space();
984 token(node.leftParenthesis); 975 token(node.leftParenthesis);
985 visit(node.condition); 976 visit(node.condition);
986 token(node.rightParenthesis); 977 token(node.rightParenthesis);
987 space(); 978 space();
988 visit(node.body); 979 visit(node.body);
989 } 980 }
990 981
991 visitWithClause(WithClause node) { 982 visitWithClause(WithClause node) {
992 token(node.withKeyword); 983 token(node.withKeyword);
993 space(); 984 space();
994 visitList(node.mixinTypes, ', '); 985 visitNodes(node.mixinTypes, separatedBy: commaSeperator);
995 } 986 }
996 987
997 /// Safely visit the given [node]. 988 /// Safely visit the given [node].
998 visit(ASTNode node) { 989 visit(ASTNode node) {
999 if (node != null) { 990 if (node != null) {
1000 node.accept(this); 991 node.accept(this);
1001 } 992 }
1002 } 993 }
1003 994
1004 /// Safely visit the given [node], printing the [suffix] after the node if it
1005 /// is non-null.
1006 visitSuffixed(ASTNode node, String suffix) {
1007 if (node != null) {
1008 node.accept(this);
1009 append(suffix);
1010 }
1011 }
1012
1013 /// Safely visit the given [node], printing the [prefix] before the node if
1014 /// it is non-null.
1015 visitPrefixed(String prefix, ASTNode node) {
1016 if (node != null) {
1017 append(prefix);
1018 node.accept(this);
1019 }
1020 }
1021
1022 /// Visit the given function [body], printing the [prefix] before if given 995 /// Visit the given function [body], printing the [prefix] before if given
1023 /// body is not empty. 996 /// body is not empty.
1024 visitPrefixedBody(String prefix, FunctionBody body) { 997 visitPrefixedBody(prefix(), FunctionBody body) {
1025 if (body is! EmptyFunctionBody) { 998 if (body is! EmptyFunctionBody) {
1026 append(prefix); 999 prefix();
1027 } 1000 }
1028 visit(body); 1001 visit(body);
1029 } 1002 }
1030 1003
1031 /// Print a list of [nodes], optionally separated by the given [separator]. 1004 /// Visit a list of [nodes] if not null, optionally separated and/or preceded
1032 visitList(NodeList<ASTNode> nodes, [String separator = '']) { 1005 /// and followed by the given functions.
1006 visitNodes(NodeList<ASTNode> nodes, {precededBy(): null,
1007 separatedBy() : null, followedBy(): null}) {
1033 if (nodes != null) { 1008 if (nodes != null) {
1034 var size = nodes.length; 1009 var size = nodes.length;
1035 for (var i = 0; i < size; i++) { 1010 if (size > 0) {
1036 if (i > 0) { 1011 if (precededBy != null) {
1037 append(separator); 1012 precededBy();
1038 } 1013 }
1039 nodes[i].accept(this); 1014 for (var i = 0; i < size; i++) {
1015 if (i > 0 && separatedBy != null) {
1016 separatedBy();
1017 }
1018 nodes[i].accept(this);
1019 }
1020 if (followedBy != null) {
1021 followedBy();
1022 }
1023 }
1024 }
1025 }
1026
1027 /// Visit a [node], and if not null, optionally preceded or followed by the
1028 /// specified functions.
1029 visitNode(ASTNode node, {precededBy(): null, followedBy(): null}) {
1030 if (node != null) {
1031 if (precededBy != null) {
1032 precededBy();
1033 }
1034 node.accept(this);
1035 if (followedBy != null) {
1036 followedBy();
1040 } 1037 }
1041 } 1038 }
1042 } 1039 }
1043 1040
1044 /// Print a list of [nodes], separated by the given [separator].
1045 visitSuffixedList(NodeList<ASTNode> nodes, String separator, String suffix) {
1046 if (nodes != null) {
1047 var size = nodes.length;
1048 if (size > 0) {
1049 for (var i = 0; i < size; i++) {
1050 if (i > 0) {
1051 append(separator);
1052 }
1053 nodes[i].accept(this);
1054 }
1055 append(suffix);
1056 }
1057 }
1058 }
1059
1060 /// Print a list of [nodes], separated by the given [separator].
1061 visitPrefixedList(String prefix, NodeList<ASTNode> nodes,
1062 [String separator = null]) {
1063 if (nodes != null) {
1064 var size = nodes.length;
1065 if (size > 0) {
1066 append(prefix);
1067 for (var i = 0; i < size; i++) {
1068 if (i > 0 && separator != null) {
1069 append(separator);
1070 }
1071 nodes[i].accept(this);
1072 }
1073 }
1074 }
1075 }
1076 1041
1077 /// Emit the given [modifier] if it's non null, followed by non-breaking 1042 /// Emit the given [modifier] if it's non null, followed by non-breaking
1078 /// whitespace. 1043 /// whitespace.
1079 modifier(Token modifier) { 1044 modifier(Token modifier) {
1080 token(modifier, followedBy: space); 1045 token(modifier, followedBy: space);
1081 } 1046 }
1082 1047
1083 token(Token token, {followedBy(), int minNewlines: 0}) { 1048
1049 /// Indicate that at least one newline should be emitted and possibly more
1050 /// if the source has them.
1051 newlines() {
1052 preservePrecedingNewlines = true;
1053 needsNewline = true;
1054 }
1055
1056 token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) {
1084 if (token != null) { 1057 if (token != null) {
1085 if (needsNewline) { 1058 if (needsNewline) {
1086 minNewlines = max(1, minNewlines); 1059 minNewlines = max(1, minNewlines);
1087 } 1060 }
1088 if (preservePrecedingNewlines || minNewlines > 0) { 1061 if (preservePrecedingNewlines || minNewlines > 0) {
1089 var emitted = emitPrecedingNewlines(token, min: minNewlines); 1062 var emitted = emitPrecedingNewlines(token, min: minNewlines);
1090 preservePrecedingNewlines = false; 1063 preservePrecedingNewlines = false;
1091 if (emitted > 0) { 1064 if (emitted > 0) {
1092 needsNewline = false; 1065 needsNewline = false;
1093 } 1066 }
1094 } 1067 }
1068 if (precededBy !=null) {
1069 precededBy();
1070 }
1095 append(token.lexeme); 1071 append(token.lexeme);
1096 if (followedBy != null) { 1072 if (followedBy != null) {
1097 followedBy(); 1073 followedBy();
1098 } 1074 }
1099 previousToken = token; 1075 previousToken = token;
1100 } 1076 }
1101 } 1077 }
1102 1078
1079 commaSeperator() {
1080 comma();
1081 space();
1082 }
1083
1084 comma() {
1085 append(',');
1086 }
1087
1103 /// Emit a non-breakable space. 1088 /// Emit a non-breakable space.
1104 space() { 1089 space() {
1105 //TODO(pquitslund): replace with a proper space token 1090 //TODO(pquitslund): replace with a proper space token
1106 append(' '); 1091 append(' ');
1107 } 1092 }
1108 1093
1109 /// Emit a breakable space 1094 /// Emit a breakable space
1110 breakableSpace() { 1095 breakableSpace() {
1111 //Implement 1096 //Implement
1112 } 1097 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 var lastLine = 1149 var lastLine =
1165 lineInfo.getLocation(last.offset).lineNumber; 1150 lineInfo.getLocation(last.offset).lineNumber;
1166 var currentLine = 1151 var currentLine =
1167 lineInfo.getLocation(current.offset).lineNumber; 1152 lineInfo.getLocation(current.offset).lineNumber;
1168 return currentLine - lastLine; 1153 return currentLine - lastLine;
1169 } 1154 }
1170 1155
1171 String toString() => writer.toString(); 1156 String toString() => writer.toString();
1172 1157
1173 } 1158 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer_experimental/test/services/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698