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

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

Issue 23044004: Formatter cleanup. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 4 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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 /// the visited nodes to the given [writer]. 154 /// the visited nodes to the given [writer].
155 SourceVisitor(FormatterOptions options, this.lineInfo) : 155 SourceVisitor(FormatterOptions options, this.lineInfo) :
156 writer = new SourceWriter(indentCount: options.initialIndentationLevel, 156 writer = new SourceWriter(indentCount: options.initialIndentationLevel,
157 lineSeparator: options.lineSeparator); 157 lineSeparator: options.lineSeparator);
158 158
159 visitAdjacentStrings(AdjacentStrings node) { 159 visitAdjacentStrings(AdjacentStrings node) {
160 visitList(node.strings, ' '); 160 visitList(node.strings, ' ');
161 } 161 }
162 162
163 visitAnnotation(Annotation node) { 163 visitAnnotation(Annotation node) {
164 writer.print('@'); 164 emitToken(node.atSign);
165 visit(node.name); 165 visit(node.name);
166 visitPrefixed('.', node.constructorName); 166 visitPrefixed('.', node.constructorName);
Brian Wilkerson 2013/08/13 20:03:24 This might want to be if (node.constructorName !
167 visit(node.arguments); 167 visit(node.arguments);
168 } 168 }
169 169
170 visitArgumentDefinitionTest(ArgumentDefinitionTest node) { 170 visitArgumentDefinitionTest(ArgumentDefinitionTest node) {
171 writer.print('?'); 171 emitToken(node.question);
172 visit(node.identifier); 172 visit(node.identifier);
173 } 173 }
174 174
175 visitArgumentList(ArgumentList node) { 175 visitArgumentList(ArgumentList node) {
176 writer.print('('); 176 emitToken(node.leftParenthesis);
177 visitList(node.arguments, ', '); 177 visitList(node.arguments, ', ');
178 writer.print(')'); 178 emitToken(node.rightParenthesis);
179 } 179 }
180 180
181 visitAsExpression(AsExpression node) { 181 visitAsExpression(AsExpression node) {
182 visit(node.expression); 182 visit(node.expression);
183 writer.print(' as '); 183 emitToken(node.asOperator, prefix: ' ', suffix: ' ');
184 visit(node.type); 184 visit(node.type);
185 } 185 }
186 186
187 visitAssertStatement(AssertStatement node) { 187 visitAssertStatement(AssertStatement node) {
188 writer.print('assert ('); 188 emitToken(node.keyword, suffix: ' (');
Brian Wilkerson 2013/08/13 20:03:24 This might want to be emitToken(node.keyword);
pquitslund 2013/08/13 20:08:02 Not done but point taken. I think that *is* the d
189 visit(node.condition); 189 visit(node.condition);
190 writer.print(');'); 190 emitToken(node.semicolon, prefix: ')');
191 } 191 }
192 192
193 visitAssignmentExpression(AssignmentExpression node) { 193 visitAssignmentExpression(AssignmentExpression node) {
194 visit(node.leftHandSide); 194 visit(node.leftHandSide);
195 writer.print(' '); 195 emitToken(node.operator, prefix: ' ', suffix: ' ');
196 writer.print(node.operator.lexeme);
197 writer.print(' ');
198 visit(node.rightHandSide); 196 visit(node.rightHandSide);
199 } 197 }
200 198
201 visitBinaryExpression(BinaryExpression node) { 199 visitBinaryExpression(BinaryExpression node) {
202 visit(node.leftOperand); 200 visit(node.leftOperand);
203 writer.print(' '); 201 emitToken(node.operator, prefix: ' ', suffix: ' ');
204 writer.print(node.operator.lexeme);
205 writer.print(' ');
206 visit(node.rightOperand); 202 visit(node.rightOperand);
207 } 203 }
208 204
209 visitBlock(Block node) { 205 visitBlock(Block node) {
210 // writer.print('{');
211 emitToken(node.leftBracket); 206 emitToken(node.leftBracket);
212 writer.indent(); 207 indent();
213 208
214 for (var stmt in node.statements) { 209 for (var stmt in node.statements) {
215 //writer.newline();
216 visit(stmt); 210 visit(stmt);
217 } 211 }
218 212
219 writer.unindent(); 213 unindent();
220 writer.newline(); 214 newline();
221 writer.print('}'); 215 print('}');
216 //TODO(pquitslund): make this work
217 // emitToken(node.rightBracket);
222 previousToken = node.rightBracket; 218 previousToken = node.rightBracket;
223 } 219 }
224 220
225 visitBlockFunctionBody(BlockFunctionBody node) { 221 visitBlockFunctionBody(BlockFunctionBody node) {
226 visit(node.block); 222 visit(node.block);
227 } 223 }
228 224
229 visitBooleanLiteral(BooleanLiteral node) { 225 visitBooleanLiteral(BooleanLiteral node) {
230 writer.print(node.literal.lexeme); 226 print(node.literal.lexeme);
Brian Wilkerson 2013/08/13 20:03:24 Why not "emitToken(node.literal)"?
pquitslund 2013/08/13 20:08:02 Because I missed it! :) A few I'm holdng off on t
231 } 227 }
232 228
233 visitBreakStatement(BreakStatement node) { 229 visitBreakStatement(BreakStatement node) {
234 emitToken(node.keyword); 230 emitToken(node.keyword);
235 visitPrefixed(' ', node.label); 231 visitPrefixed(' ', node.label);
236 writer.print(';'); 232 emitToken(node.semicolon);
237 } 233 }
238 234
239 visitCascadeExpression(CascadeExpression node) { 235 visitCascadeExpression(CascadeExpression node) {
240 visit(node.target); 236 visit(node.target);
241 visitList(node.cascadeSections); 237 visitList(node.cascadeSections);
242 } 238 }
243 239
244 visitCatchClause(CatchClause node) { 240 visitCatchClause(CatchClause node) {
245 visitPrefixed('on ', node.exceptionType); 241 visitPrefixed('on ', node.exceptionType);
246 if (node.catchKeyword != null) { 242 if (node.catchKeyword != null) {
247 if (node.exceptionType != null) { 243 if (node.exceptionType != null) {
248 writer.print(' '); 244 print(' ');
249 } 245 }
250 writer.print('catch ('); 246 print('catch (');
251 visit(node.exceptionParameter); 247 visit(node.exceptionParameter);
252 visitPrefixed(', ', node.stackTraceParameter); 248 visitPrefixed(', ', node.stackTraceParameter);
253 writer.print(') '); 249 print(') ');
254 } else { 250 } else {
255 writer.print(' '); 251 print(' ');
256 } 252 }
257 visit(node.body); 253 visit(node.body);
258 writer.newline(); 254 newline();
259 } 255 }
260 256
261 visitClassDeclaration(ClassDeclaration node) { 257 visitClassDeclaration(ClassDeclaration node) {
262 emitToken(node.abstractKeyword, ' '); 258 emitToken(node.abstractKeyword, suffix: ' ');
263 emitToken(node.classKeyword, ' '); 259 emitToken(node.classKeyword, suffix: ' ');
264 visit(node.name); 260 visit(node.name);
265 visit(node.typeParameters); 261 visit(node.typeParameters);
266 visitPrefixed(' ', node.extendsClause); 262 visitPrefixed(' ', node.extendsClause);
267 visitPrefixed(' ', node.withClause); 263 visitPrefixed(' ', node.withClause);
268 visitPrefixed(' ', node.implementsClause); 264 visitPrefixed(' ', node.implementsClause);
269 // writer.print(' {'); 265 emitToken(node.leftBracket, prefix: ' ');
270 // writer.print(' '); 266 indent();
271 // emit(node.leftBracket);
272 emitPrefixedToken(' ', node.leftBracket);
273 writer.indent();
274 267
275 for (var i = 0; i < node.members.length; i++) { 268 for (var i = 0; i < node.members.length; i++) {
276 visit(node.members[i]); 269 visit(node.members[i]);
277 } 270 }
278 271
279 writer.unindent(); 272 unindent();
280 273
281 emit(node.rightBracket, min: 1); 274 emitToken(node.rightBracket, minNewlines: 1);
282 } 275 }
283 276
284 visitClassTypeAlias(ClassTypeAlias node) { 277 visitClassTypeAlias(ClassTypeAlias node) {
285 writer.print('typedef '); 278 emitToken(node.keyword, suffix: ' ');
286 visit(node.name); 279 visit(node.name);
287 visit(node.typeParameters); 280 visit(node.typeParameters);
288 writer.print(' = '); 281 print(' = ');
289 if (node.abstractKeyword != null) { 282 if (node.abstractKeyword != null) {
290 writer.print('abstract '); 283 print('abstract ');
291 } 284 }
292 visit(node.superclass); 285 visit(node.superclass);
293 visitPrefixed(' ', node.withClause); 286 visitPrefixed(' ', node.withClause);
294 visitPrefixed(' ', node.implementsClause); 287 visitPrefixed(' ', node.implementsClause);
295 writer.print(';'); 288 emitToken(node.semicolon);
296 } 289 }
297 290
298 visitComment(Comment node) => null; 291 visitComment(Comment node) => null;
299 292
300 visitCommentReference(CommentReference node) => null; 293 visitCommentReference(CommentReference node) => null;
301 294
302 visitCompilationUnit(CompilationUnit node) { 295 visitCompilationUnit(CompilationUnit node) {
303 var scriptTag = node.scriptTag; 296 var scriptTag = node.scriptTag;
304 var directives = node.directives; 297 var directives = node.directives;
305 visit(scriptTag); 298 visit(scriptTag);
306 var prefix = scriptTag == null ? '' : ' '; 299 var prefix = scriptTag == null ? '' : ' ';
307 visitPrefixedList(prefix, directives, ' '); 300 visitPrefixedList(prefix, directives, ' ');
308 //prefix = scriptTag == null && directives.isEmpty ? '' : ' '; 301 //prefix = scriptTag == null && directives.isEmpty ? '' : ' ';
309 prefix = ''; 302 prefix = '';
310 visitPrefixedList(prefix, node.declarations); 303 visitPrefixedList(prefix, node.declarations);
311 304
312 //TODO(pquitslund): move this? 305 //TODO(pquitslund): move this?
313 writer.newline(); 306 newline();
314 } 307 }
315 308
316 visitConditionalExpression(ConditionalExpression node) { 309 visitConditionalExpression(ConditionalExpression node) {
317 visit(node.condition); 310 visit(node.condition);
318 writer.print(' ? '); 311 print(' ? ');
319 visit(node.thenExpression); 312 visit(node.thenExpression);
320 writer.print(' : '); 313 print(' : ');
321 visit(node.elseExpression); 314 visit(node.elseExpression);
322 } 315 }
323 316
324 visitConstructorDeclaration(ConstructorDeclaration node) { 317 visitConstructorDeclaration(ConstructorDeclaration node) {
325 emitToken(node.externalKeyword, ' '); 318 emitToken(node.externalKeyword, suffix: ' ');
326 emitToken(node.constKeyword, ' '); 319 emitToken(node.constKeyword, suffix: ' ');
327 emitToken(node.factoryKeyword, ' '); 320 emitToken(node.factoryKeyword, suffix: ' ');
328 visit(node.returnType); 321 visit(node.returnType);
329 visitPrefixed('.', node.name); 322 visitPrefixed('.', node.name);
330 visit(node.parameters); 323 visit(node.parameters);
331 visitPrefixedList(' : ', node.initializers, ', '); 324 visitPrefixedList(' : ', node.initializers, ', ');
332 visitPrefixed(' = ', node.redirectedConstructor); 325 visitPrefixed(' = ', node.redirectedConstructor);
333 visitPrefixedBody(' ', node.body); 326 visitPrefixedBody(' ', node.body);
334 } 327 }
335 328
336 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { 329 visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
337 emitToken(node.keyword, '.'); 330 emitToken(node.keyword, suffix: '.');
338 visit(node.fieldName); 331 visit(node.fieldName);
339 writer.print(' = '); 332 print(' = ');
340 visit(node.expression); 333 visit(node.expression);
341 } 334 }
342 335
343 visitConstructorName(ConstructorName node) { 336 visitConstructorName(ConstructorName node) {
344 visit(node.type); 337 visit(node.type);
345 visitPrefixed('.', node.name); 338 visitPrefixed('.', node.name);
346 } 339 }
347 340
348 visitContinueStatement(ContinueStatement node) { 341 visitContinueStatement(ContinueStatement node) {
349 writer.print('continue'); 342 emitToken(node.keyword, suffix: ' ');
Brian Wilkerson 2013/08/13 20:03:24 This looks like it would put two spaces between 'c
pquitslund 2013/08/13 20:08:02 Good catch! Fixed.
350 visitPrefixed(' ', node.label); 343 visitPrefixed(' ', node.label);
351 writer.print(';'); 344 emitToken(node.semicolon);
352 } 345 }
353 346
354 visitDeclaredIdentifier(DeclaredIdentifier node) { 347 visitDeclaredIdentifier(DeclaredIdentifier node) {
355 emitToken(node.keyword, ' '); 348 emitToken(node.keyword, suffix: ' ');
356 visitSuffixed(node.type, ' '); 349 visitSuffixed(node.type, ' ');
357 visit(node.identifier); 350 visit(node.identifier);
358 } 351 }
359 352
360 visitDefaultFormalParameter(DefaultFormalParameter node) { 353 visitDefaultFormalParameter(DefaultFormalParameter node) {
361 visit(node.parameter); 354 visit(node.parameter);
362 if (node.separator != null) { 355 if (node.separator != null) {
363 writer.print(' '); 356 print(' ');
364 writer.print(node.separator.lexeme); 357 print(node.separator.lexeme);
365 visitPrefixed(' ', node.defaultValue); 358 visitPrefixed(' ', node.defaultValue);
366 } 359 }
367 } 360 }
368 361
369 visitDoStatement(DoStatement node) { 362 visitDoStatement(DoStatement node) {
370 writer.print('do '); 363 emitToken(node.doKeyword, suffix: ' ');
371 visit(node.body); 364 visit(node.body);
372 writer.print(' while ('); 365 emitToken(node.whileKeyword, prefix: ' ', suffix: ' (');
373 visit(node.condition); 366 visit(node.condition);
374 writer.print(');'); 367 emitToken(node.semicolon, prefix: ')');
375 } 368 }
376 369
377 visitDoubleLiteral(DoubleLiteral node) { 370 visitDoubleLiteral(DoubleLiteral node) {
378 writer.print(node.literal.lexeme); 371 print(node.literal.lexeme);
379 } 372 }
380 373
381 visitEmptyFunctionBody(EmptyFunctionBody node) { 374 visitEmptyFunctionBody(EmptyFunctionBody node) {
382 writer.print(';'); 375 emitToken(node.semicolon);
383 } 376 }
384 377
385 visitEmptyStatement(EmptyStatement node) { 378 visitEmptyStatement(EmptyStatement node) {
386 writer.print(';'); 379 emitToken(node.semicolon);
387 } 380 }
388 381
389 visitExportDirective(ExportDirective node) { 382 visitExportDirective(ExportDirective node) {
390 writer.print('export '); 383 emitToken(node.keyword, suffix: ' ');
391 visit(node.uri); 384 visit(node.uri);
392 visitPrefixedList(' ', node.combinators, ' '); 385 visitPrefixedList(' ', node.combinators, ' ');
393 writer.print(';'); 386 emitToken(node.semicolon);
394 } 387 }
395 388
396 visitExpressionFunctionBody(ExpressionFunctionBody node) { 389 visitExpressionFunctionBody(ExpressionFunctionBody node) {
397 writer.print('=> '); 390 emitToken(node.functionDefinition, suffix: ' ');
398 visit(node.expression); 391 visit(node.expression);
399 if (node.semicolon != null) { 392 emitToken(node.semicolon);
400 writer.print(';');
401 }
402 } 393 }
403 394
404 visitExpressionStatement(ExpressionStatement node) { 395 visitExpressionStatement(ExpressionStatement node) {
405 visit(node.expression); 396 visit(node.expression);
406 writer.print(';'); 397 emitToken(node.semicolon);
407 } 398 }
408 399
409 visitExtendsClause(ExtendsClause node) { 400 visitExtendsClause(ExtendsClause node) {
410 writer.print('extends '); 401 emitToken(node.keyword, suffix: ' ');
411 visit(node.superclass); 402 visit(node.superclass);
412 } 403 }
413 404
414 visitFieldDeclaration(FieldDeclaration node) { 405 visitFieldDeclaration(FieldDeclaration node) {
415 emitToken(node.keyword, ' '); 406 emitToken(node.keyword, suffix: ' ');
416 visit(node.fields); 407 visit(node.fields);
417 writer.print(';'); 408 emitToken(node.semicolon);
418 } 409 }
419 410
420 visitFieldFormalParameter(FieldFormalParameter node) { 411 visitFieldFormalParameter(FieldFormalParameter node) {
421 emitToken(node.keyword, ' '); 412 emitToken(node.keyword, suffix: ' ');
422 visitSuffixed(node.type, ' '); 413 visitSuffixed(node.type, ' ');
423 writer.print('this.'); 414 print('this.');
424 visit(node.identifier); 415 visit(node.identifier);
425 visit(node.parameters); 416 visit(node.parameters);
426 } 417 }
427 418
428 visitForEachStatement(ForEachStatement node) { 419 visitForEachStatement(ForEachStatement node) {
429 writer.print('for ('); 420 print('for (');
430 visit(node.loopVariable); 421 visit(node.loopVariable);
431 writer.print(' in '); 422 print(' in ');
432 visit(node.iterator); 423 visit(node.iterator);
433 writer.print(') '); 424 print(') ');
434 visit(node.body); 425 visit(node.body);
435 } 426 }
436 427
437 visitFormalParameterList(FormalParameterList node) { 428 visitFormalParameterList(FormalParameterList node) {
438 var groupEnd = null; 429 var groupEnd = null;
439 writer.print('('); 430 print('(');
440 var parameters = node.parameters; 431 var parameters = node.parameters;
441 var size = parameters.length; 432 var size = parameters.length;
442 for (var i = 0; i < size; i++) { 433 for (var i = 0; i < size; i++) {
443 var parameter = parameters[i]; 434 var parameter = parameters[i];
444 if (i > 0) { 435 if (i > 0) {
445 writer.print(', '); 436 print(', ');
446 } 437 }
447 if (groupEnd == null && parameter is DefaultFormalParameter) { 438 if (groupEnd == null && parameter is DefaultFormalParameter) {
448 if (identical(parameter.kind, ParameterKind.NAMED)) { 439 if (identical(parameter.kind, ParameterKind.NAMED)) {
449 groupEnd = '}'; 440 groupEnd = '}';
450 writer.print('{'); 441 print('{');
451 } else { 442 } else {
452 groupEnd = ']'; 443 groupEnd = ']';
453 writer.print('['); 444 print('[');
454 } 445 }
455 } 446 }
456 parameter.accept(this); 447 parameter.accept(this);
457 } 448 }
458 if (groupEnd != null) { 449 if (groupEnd != null) {
459 writer.print(groupEnd); 450 print(groupEnd);
460 } 451 }
461 writer.print(')'); 452 print(')');
462 } 453 }
463 454
464 visitForStatement(ForStatement node) { 455 visitForStatement(ForStatement node) {
465 var initialization = node.initialization; 456 var initialization = node.initialization;
466 writer.print('for ('); 457 print('for (');
467 if (initialization != null) { 458 if (initialization != null) {
468 visit(initialization); 459 visit(initialization);
469 } else { 460 } else {
470 visit(node.variables); 461 visit(node.variables);
471 } 462 }
472 writer.print(';'); 463 print(';');
473 visitPrefixed(' ', node.condition); 464 visitPrefixed(' ', node.condition);
474 writer.print(';'); 465 print(';');
475 visitPrefixedList(' ', node.updaters, ', '); 466 visitPrefixedList(' ', node.updaters, ', ');
476 writer.print(') '); 467 print(') ');
477 visit(node.body); 468 visit(node.body);
478 } 469 }
479 470
480 visitFunctionDeclaration(FunctionDeclaration node) { 471 visitFunctionDeclaration(FunctionDeclaration node) {
481 visitSuffixed(node.returnType, ' '); 472 visitSuffixed(node.returnType, ' ');
482 emitToken(node.propertyKeyword, ' '); 473 emitToken(node.propertyKeyword, suffix: ' ');
483 visit(node.name); 474 visit(node.name);
484 visit(node.functionExpression); 475 visit(node.functionExpression);
485 } 476 }
486 477
487 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) { 478 visitFunctionDeclarationStatement(FunctionDeclarationStatement node) {
488 visit(node.functionDeclaration); 479 visit(node.functionDeclaration);
489 writer.print(';'); 480 print(';');
490 } 481 }
491 482
492 visitFunctionExpression(FunctionExpression node) { 483 visitFunctionExpression(FunctionExpression node) {
493 visit(node.parameters); 484 visit(node.parameters);
494 writer.print(' '); 485 print(' ');
495 visit(node.body); 486 visit(node.body);
496 } 487 }
497 488
498 visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { 489 visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
499 visit(node.function); 490 visit(node.function);
500 visit(node.argumentList); 491 visit(node.argumentList);
501 } 492 }
502 493
503 visitFunctionTypeAlias(FunctionTypeAlias node) { 494 visitFunctionTypeAlias(FunctionTypeAlias node) {
504 writer.print('typedef '); 495 emitToken(node.keyword, suffix: ' ');
505 visitSuffixed(node.returnType, ' '); 496 visitSuffixed(node.returnType, ' ');
506 visit(node.name); 497 visit(node.name);
507 visit(node.typeParameters); 498 visit(node.typeParameters);
508 visit(node.parameters); 499 visit(node.parameters);
509 writer.print(';'); 500 emitToken(node.semicolon);
510 } 501 }
511 502
512 visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) { 503 visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) {
513 visitSuffixed(node.returnType, ' '); 504 visitSuffixed(node.returnType, ' ');
514 visit(node.identifier); 505 visit(node.identifier);
515 visit(node.parameters); 506 visit(node.parameters);
516 } 507 }
517 508
518 visitHideCombinator(HideCombinator node) { 509 visitHideCombinator(HideCombinator node) {
519 writer.print('hide '); 510 emitToken(node.keyword, suffix: ' ');
520 visitList(node.hiddenNames, ', '); 511 visitList(node.hiddenNames, ', ');
521 } 512 }
522 513
523 visitIfStatement(IfStatement node) { 514 visitIfStatement(IfStatement node) {
524 emitToken(node.ifKeyword); 515 emitToken(node.ifKeyword);
525 writer.print(' ('); 516 print(' (');
526 visit(node.condition); 517 visit(node.condition);
527 writer.print(') '); 518 print(') ');
528 visit(node.thenStatement); 519 visit(node.thenStatement);
529 visitPrefixed(' else ', node.elseStatement); 520 visitPrefixed(' else ', node.elseStatement);
530 } 521 }
531 522
532 visitImplementsClause(ImplementsClause node) { 523 visitImplementsClause(ImplementsClause node) {
533 writer.print('implements '); 524 emitToken(node.keyword, suffix: ' ');
534 visitList(node.interfaces, ', '); 525 visitList(node.interfaces, ', ');
535 } 526 }
536 527
537 visitImportDirective(ImportDirective node) { 528 visitImportDirective(ImportDirective node) {
538 writer.print('import '); 529 emitToken(node.keyword, suffix: ' ');
539 visit(node.uri); 530 visit(node.uri);
540 visitPrefixed(' as ', node.prefix); 531 visitPrefixed(' as ', node.prefix);
541 visitPrefixedList(' ', node.combinators, ' '); 532 visitPrefixedList(' ', node.combinators, ' ');
542 // writer.print(';');
543 emitToken(node.semicolon); 533 emitToken(node.semicolon);
544 // writer.newline();
545 } 534 }
546 535
547 visitIndexExpression(IndexExpression node) { 536 visitIndexExpression(IndexExpression node) {
548 if (node.isCascaded) { 537 if (node.isCascaded) {
549 writer.print('..'); 538 print('..');
550 } else { 539 } else {
551 visit(node.target); 540 visit(node.target);
552 } 541 }
553 writer.print('['); 542 print('[');
554 visit(node.index); 543 visit(node.index);
555 writer.print(']'); 544 print(']');
556 } 545 }
557 546
558 visitInstanceCreationExpression(InstanceCreationExpression node) { 547 visitInstanceCreationExpression(InstanceCreationExpression node) {
559 emitToken(node.keyword, ' '); 548 emitToken(node.keyword, suffix: ' ');
560 visit(node.constructorName); 549 visit(node.constructorName);
561 visit(node.argumentList); 550 visit(node.argumentList);
562 } 551 }
563 552
564 visitIntegerLiteral(IntegerLiteral node) { 553 visitIntegerLiteral(IntegerLiteral node) {
565 writer.print(node.literal.lexeme); 554 print(node.literal.lexeme);
566 } 555 }
567 556
568 visitInterpolationExpression(InterpolationExpression node) { 557 visitInterpolationExpression(InterpolationExpression node) {
569 if (node.rightBracket != null) { 558 if (node.rightBracket != null) {
570 writer.print('\${'); 559 print('\${');
571 visit(node.expression); 560 visit(node.expression);
572 writer.print('}'); 561 print('}');
573 } else { 562 } else {
574 writer.print('\$'); 563 print('\$');
575 visit(node.expression); 564 visit(node.expression);
576 } 565 }
577 } 566 }
578 567
579 visitInterpolationString(InterpolationString node) { 568 visitInterpolationString(InterpolationString node) {
580 writer.print(node.contents.lexeme); 569 print(node.contents.lexeme);
581 } 570 }
582 571
583 visitIsExpression(IsExpression node) { 572 visitIsExpression(IsExpression node) {
584 visit(node.expression); 573 visit(node.expression);
585 if (node.notOperator == null) { 574 if (node.notOperator == null) {
586 writer.print(' is '); 575 print(' is ');
587 } else { 576 } else {
588 writer.print(' is! '); 577 print(' is! ');
589 } 578 }
590 visit(node.type); 579 visit(node.type);
591 } 580 }
592 581
593 visitLabel(Label node) { 582 visitLabel(Label node) {
594 visit(node.label); 583 visit(node.label);
595 writer.print(':'); 584 print(':');
596 } 585 }
597 586
598 visitLabeledStatement(LabeledStatement node) { 587 visitLabeledStatement(LabeledStatement node) {
599 visitSuffixedList(node.labels, ' ', ' '); 588 visitSuffixedList(node.labels, ' ', ' ');
600 visit(node.statement); 589 visit(node.statement);
601 } 590 }
602 591
603 visitLibraryDirective(LibraryDirective node) { 592 visitLibraryDirective(LibraryDirective node) {
604 writer.print('library '); 593 emitToken(node.keyword, suffix: ' ');
605 visit(node.name); 594 visit(node.name);
606 writer.print(';'); 595 emitToken(node.semicolon);
607 } 596 }
608 597
609 visitLibraryIdentifier(LibraryIdentifier node) { 598 visitLibraryIdentifier(LibraryIdentifier node) {
610 writer.print(node.name); 599 print(node.name);
611 } 600 }
612 601
613 visitListLiteral(ListLiteral node) { 602 visitListLiteral(ListLiteral node) {
614 if (node.modifier != null) { 603 if (node.modifier != null) {
615 writer.print(node.modifier.lexeme); 604 print(node.modifier.lexeme);
616 writer.print(' '); 605 print(' ');
617 } 606 }
618 visit(node.typeArguments); 607 visit(node.typeArguments);
619 writer.print('['); 608 print('[');
620 visitList(node.elements, ', '); 609 visitList(node.elements, ', ');
621 writer.print(']'); 610 print(']');
622 } 611 }
623 612
624 visitMapLiteral(MapLiteral node) { 613 visitMapLiteral(MapLiteral node) {
625 if (node.modifier != null) { 614 if (node.modifier != null) {
626 writer.print(node.modifier.lexeme); 615 print(node.modifier.lexeme);
627 writer.print(' '); 616 print(' ');
628 } 617 }
629 visitSuffixed(node.typeArguments, ' '); 618 visitSuffixed(node.typeArguments, ' ');
630 writer.print('{'); 619 print('{');
631 visitList(node.entries, ', '); 620 visitList(node.entries, ', ');
632 writer.print('}'); 621 print('}');
633 } 622 }
634 623
635 visitMapLiteralEntry(MapLiteralEntry node) { 624 visitMapLiteralEntry(MapLiteralEntry node) {
636 visit(node.key); 625 visit(node.key);
637 writer.print(' : '); 626 print(' : ');
638 visit(node.value); 627 visit(node.value);
639 } 628 }
640 629
641 visitMethodDeclaration(MethodDeclaration node) { 630 visitMethodDeclaration(MethodDeclaration node) {
642 emitToken(node.externalKeyword, ' '); 631 emitToken(node.externalKeyword, suffix: ' ');
643 emitToken(node.modifierKeyword, ' '); 632 emitToken(node.modifierKeyword, suffix: ' ');
644 visitSuffixed(node.returnType, ' '); 633 visitSuffixed(node.returnType, ' ');
645 emitToken(node.propertyKeyword, ' '); 634 emitToken(node.propertyKeyword, suffix: ' ');
646 emitToken(node.operatorKeyword, ' '); 635 emitToken(node.operatorKeyword, suffix: ' ');
647 visit(node.name); 636 visit(node.name);
648 if (!node.isGetter) { 637 if (!node.isGetter) {
649 visit(node.parameters); 638 visit(node.parameters);
650 } 639 }
651 visitPrefixedBody(' ', node.body); 640 visitPrefixedBody(' ', node.body);
652 } 641 }
653 642
654 visitMethodInvocation(MethodInvocation node) { 643 visitMethodInvocation(MethodInvocation node) {
655 if (node.isCascaded) { 644 if (node.isCascaded) {
656 writer.print('..'); 645 print('..');
657 } else { 646 } else {
658 visitSuffixed(node.target, '.'); 647 visitSuffixed(node.target, '.');
659 } 648 }
660 visit(node.methodName); 649 visit(node.methodName);
661 visit(node.argumentList); 650 visit(node.argumentList);
662 } 651 }
663 652
664 visitNamedExpression(NamedExpression node) { 653 visitNamedExpression(NamedExpression node) {
665 visit(node.name); 654 visit(node.name);
666 visitPrefixed(' ', node.expression); 655 visitPrefixed(' ', node.expression);
667 } 656 }
668 657
669 visitNativeClause(NativeClause node) { 658 visitNativeClause(NativeClause node) {
670 writer.print('native '); 659 emitToken(node.keyword, suffix: ' ');
671 visit(node.name); 660 visit(node.name);
672 } 661 }
673 662
674 visitNativeFunctionBody(NativeFunctionBody node) { 663 visitNativeFunctionBody(NativeFunctionBody node) {
675 writer.print('native '); 664 emitToken(node.nativeToken, suffix: ' ');
676 visit(node.stringLiteral); 665 visit(node.stringLiteral);
677 writer.print(';'); 666 emitToken(node.semicolon);
678 } 667 }
679 668
680 visitNullLiteral(NullLiteral node) { 669 visitNullLiteral(NullLiteral node) {
681 writer.print('null'); 670 emitToken(node.literal);
682 } 671 }
683 672
684 visitParenthesizedExpression(ParenthesizedExpression node) { 673 visitParenthesizedExpression(ParenthesizedExpression node) {
685 writer.print('('); 674 emitToken(node.leftParenthesis);
686 visit(node.expression); 675 visit(node.expression);
687 writer.print(')'); 676 emitToken(node.rightParenthesis);
688 } 677 }
689 678
690 visitPartDirective(PartDirective node) { 679 visitPartDirective(PartDirective node) {
691 writer.print('part '); 680 emitToken(node.keyword, suffix: ' ');
692 visit(node.uri); 681 visit(node.uri);
693 writer.print(';'); 682 emitToken(node.semicolon);
694 } 683 }
695 684
696 visitPartOfDirective(PartOfDirective node) { 685 visitPartOfDirective(PartOfDirective node) {
697 writer.print('part of '); 686 emitToken(node.keyword, suffix: ' ');
698 visit(node.libraryName); 687 visit(node.libraryName);
699 writer.print(';'); 688 emitToken(node.semicolon);
700 } 689 }
701 690
702 visitPostfixExpression(PostfixExpression node) { 691 visitPostfixExpression(PostfixExpression node) {
703 visit(node.operand); 692 visit(node.operand);
704 writer.print(node.operator.lexeme); 693 print(node.operator.lexeme);
705 } 694 }
706 695
707 visitPrefixedIdentifier(PrefixedIdentifier node) { 696 visitPrefixedIdentifier(PrefixedIdentifier node) {
708 visit(node.prefix); 697 visit(node.prefix);
709 writer.print('.'); 698 print('.');
710 visit(node.identifier); 699 visit(node.identifier);
711 } 700 }
712 701
713 visitPrefixExpression(PrefixExpression node) { 702 visitPrefixExpression(PrefixExpression node) {
714 writer.print(node.operator.lexeme); 703 emitToken(node.operator);
715 visit(node.operand); 704 visit(node.operand);
716 } 705 }
717 706
718 visitPropertyAccess(PropertyAccess node) { 707 visitPropertyAccess(PropertyAccess node) {
719 if (node.isCascaded) { 708 if (node.isCascaded) {
720 writer.print('..'); 709 print('..');
721 } else { 710 } else {
722 visit(node.target); 711 visit(node.target);
723 writer.print('.'); 712 print('.');
724 } 713 }
725 visit(node.propertyName); 714 visit(node.propertyName);
726 } 715 }
727 716
728 visitRedirectingConstructorInvocation(RedirectingConstructorInvocation node) { 717 visitRedirectingConstructorInvocation(RedirectingConstructorInvocation node) {
729 writer.print('this'); 718 emitToken(node.keyword);
730 visitPrefixed('.', node.constructorName); 719 visitPrefixed('.', node.constructorName);
731 visit(node.argumentList); 720 visit(node.argumentList);
732 } 721 }
733 722
734 visitRethrowExpression(RethrowExpression node) { 723 visitRethrowExpression(RethrowExpression node) {
735 writer.print('rethrow'); 724 emitToken(node.keyword);
736 } 725 }
737 726
738 visitReturnStatement(ReturnStatement node) { 727 visitReturnStatement(ReturnStatement node) {
739 var expression = node.expression; 728 var expression = node.expression;
740 if (expression == null) { 729 if (expression == null) {
741 emit(node.keyword, min: 1); 730 emitToken(node.keyword, minNewlines: 1);
742 writer.print(';'); 731 emitToken(node.semicolon);
743 } else { 732 } else {
744 emit(node.keyword, min: 1); 733 emitToken(node.keyword, suffix: ' ', minNewlines: 1);
745 writer.print(' ');
746 expression.accept(this); 734 expression.accept(this);
747 writer.print(';'); 735 emitToken(node.semicolon);
748 } 736 }
749 } 737 }
750 738
751 visitScriptTag(ScriptTag node) { 739 visitScriptTag(ScriptTag node) {
752 writer.print(node.scriptTag.lexeme); 740 print(node.scriptTag.lexeme);
753 } 741 }
754 742
755 visitShowCombinator(ShowCombinator node) { 743 visitShowCombinator(ShowCombinator node) {
756 writer.print('show '); 744 emitToken(node.keyword, suffix: ' ');
757 visitList(node.shownNames, ', '); 745 visitList(node.shownNames, ', ');
758 } 746 }
759 747
760 visitSimpleFormalParameter(SimpleFormalParameter node) { 748 visitSimpleFormalParameter(SimpleFormalParameter node) {
761 emitToken(node.keyword, ' '); 749 emitToken(node.keyword, suffix: ' ');
762 visitSuffixed(node.type, ' '); 750 visitSuffixed(node.type, ' ');
763 visit(node.identifier); 751 visit(node.identifier);
764 } 752 }
765 753
766 visitSimpleIdentifier(SimpleIdentifier node) { 754 visitSimpleIdentifier(SimpleIdentifier node) {
767 emitToken(node.token); 755 emitToken(node.token);
768 } 756 }
769 757
770 visitSimpleStringLiteral(SimpleStringLiteral node) { 758 visitSimpleStringLiteral(SimpleStringLiteral node) {
771 writer.print(node.literal.lexeme); 759 emitToken(node.literal);
772 } 760 }
773 761
774 visitStringInterpolation(StringInterpolation node) { 762 visitStringInterpolation(StringInterpolation node) {
775 visitList(node.elements); 763 visitList(node.elements);
776 } 764 }
777 765
778 visitSuperConstructorInvocation(SuperConstructorInvocation node) { 766 visitSuperConstructorInvocation(SuperConstructorInvocation node) {
779 writer.print('super'); 767 emitToken(node.keyword);
780 visitPrefixed('.', node.constructorName); 768 visitPrefixed('.', node.constructorName);
781 visit(node.argumentList); 769 visit(node.argumentList);
782 } 770 }
783 771
784 visitSuperExpression(SuperExpression node) { 772 visitSuperExpression(SuperExpression node) {
785 writer.print('super'); 773 emitToken(node.keyword);
786 } 774 }
787 775
788 visitSwitchCase(SwitchCase node) { 776 visitSwitchCase(SwitchCase node) {
789 visitSuffixedList(node.labels, ' ', ' '); 777 visitSuffixedList(node.labels, ' ', ' ');
790 emitToken(node.keyword); 778 emitToken(node.keyword, suffix: ' ');
791 writer.print(' ');
792 visit(node.expression); 779 visit(node.expression);
793 writer.print(':'); 780 print(':');
794 writer.indent(); 781 indent();
795 visitList(node.statements); 782 visitList(node.statements);
796 writer.unindent(); 783 unindent();
797 } 784 }
798 785
799 visitSwitchDefault(SwitchDefault node) { 786 visitSwitchDefault(SwitchDefault node) {
800 visitSuffixedList(node.labels, ' ', ' '); 787 visitSuffixedList(node.labels, ' ', ' ');
801 writer.print('default: '); 788 emitToken(node.keyword, suffix: ': ');
802 visitList(node.statements, ' '); 789 visitList(node.statements, ' ');
803 } 790 }
804 791
805 visitSwitchStatement(SwitchStatement node) { 792 visitSwitchStatement(SwitchStatement node) {
806 emitToken(node.keyword); 793 emitToken(node.keyword);
807 writer.print(' ('); 794 print(' (');
808 visit(node.expression); 795 visit(node.expression);
809 writer.print(') '); 796 print(') ');
810 emitToken(node.leftBracket); 797 emitToken(node.leftBracket);
811 writer.indent(); 798 indent();
812 visitList(node.members); 799 visitList(node.members);
813 writer.unindent(); 800 unindent();
814 emitToken(node.rightBracket); 801 emitToken(node.rightBracket);
815 writer.newline(); 802 newline();
816 } 803 }
817 804
818 visitSymbolLiteral(SymbolLiteral node) { 805 visitSymbolLiteral(SymbolLiteral node) {
819 // No-op ? 806 // No-op ?
820 } 807 }
821 808
822 visitThisExpression(ThisExpression node) { 809 visitThisExpression(ThisExpression node) {
823 writer.print('this'); 810 emitToken(node.keyword);
824 } 811 }
825 812
826 visitThrowExpression(ThrowExpression node) { 813 visitThrowExpression(ThrowExpression node) {
827 writer.print('throw '); 814 emitToken(node.keyword, suffix: ' ');
828 visit(node.expression); 815 visit(node.expression);
829 } 816 }
830 817
831 visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { 818 visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
832 visitSuffixed(node.variables, ';'); 819 visitSuffixed(node.variables, ';');
833 } 820 }
834 821
835 visitTryStatement(TryStatement node) { 822 visitTryStatement(TryStatement node) {
836 writer.print('try '); 823 emitToken(node.tryKeyword, suffix: ' ');
837 visit(node.body); 824 visit(node.body);
838 visitPrefixedList(' ', node.catchClauses, ' '); 825 visitPrefixedList(' ', node.catchClauses, ' ');
839 visitPrefixed(' finally ', node.finallyClause); 826 visitPrefixed(' finally ', node.finallyClause);
840 } 827 }
841 828
842 visitTypeArgumentList(TypeArgumentList node) { 829 visitTypeArgumentList(TypeArgumentList node) {
843 writer.print('<'); 830 emitToken(node.leftBracket);
844 visitList(node.arguments, ', '); 831 visitList(node.arguments, ', ');
845 writer.print('>'); 832 emitToken(node.rightBracket);
846 } 833 }
847 834
848 visitTypeName(TypeName node) { 835 visitTypeName(TypeName node) {
849 visit(node.name); 836 visit(node.name);
850 visit(node.typeArguments); 837 visit(node.typeArguments);
851 } 838 }
852 839
853 visitTypeParameter(TypeParameter node) { 840 visitTypeParameter(TypeParameter node) {
854 visit(node.name); 841 visit(node.name);
855 visitPrefixed(' extends ', node.bound); 842 visitPrefixed(' extends ', node.bound);
856 } 843 }
857 844
858 visitTypeParameterList(TypeParameterList node) { 845 visitTypeParameterList(TypeParameterList node) {
859 writer.print('<'); 846 emitToken(node.leftBracket);
860 visitList(node.typeParameters, ', '); 847 visitList(node.typeParameters, ', ');
861 writer.print('>'); 848 emitToken(node.rightBracket);
862 } 849 }
863 850
864 visitVariableDeclaration(VariableDeclaration node) { 851 visitVariableDeclaration(VariableDeclaration node) {
865 visit(node.name); 852 visit(node.name);
866 visitPrefixed(' = ', node.initializer); 853 visitPrefixed(' = ', node.initializer);
867 } 854 }
868 855
869 visitVariableDeclarationList(VariableDeclarationList node) { 856 visitVariableDeclarationList(VariableDeclarationList node) {
870 emitToken(node.keyword, ' '); 857 emitToken(node.keyword, suffix: ' ');
871 visitSuffixed(node.type, ' '); 858 visitSuffixed(node.type, ' ');
872 visitList(node.variables, ', '); 859 visitList(node.variables, ', ');
873 } 860 }
874 861
875 visitVariableDeclarationStatement(VariableDeclarationStatement node) { 862 visitVariableDeclarationStatement(VariableDeclarationStatement node) {
876 visit(node.variables); 863 visit(node.variables);
877 writer.print(';'); 864 emitToken(node.semicolon);
878 } 865 }
879 866
880 visitWhileStatement(WhileStatement node) { 867 visitWhileStatement(WhileStatement node) {
881 writer.print('while ('); 868 emitToken(node.keyword, suffix: ' (');
882 visit(node.condition); 869 visit(node.condition);
883 writer.print(') '); 870 print(') ');
884 visit(node.body); 871 visit(node.body);
885 } 872 }
886 873
887 visitWithClause(WithClause node) { 874 visitWithClause(WithClause node) {
888 writer.print('with '); 875 emitToken(node.withKeyword, suffix: ' ');
889 visitList(node.mixinTypes, ', '); 876 visitList(node.mixinTypes, ', ');
890 } 877 }
891 878
892 /// Safely visit the given [node]. 879 /// Safely visit the given [node].
893 visit(ASTNode node) { 880 visit(ASTNode node) {
894 if (node != null) { 881 if (node != null) {
895 node.accept(this); 882 node.accept(this);
896 } 883 }
897 } 884 }
898 885
899 /// Safely visit the given [node], printing the [suffix] after the node if it 886 /// Safely visit the given [node], printing the [suffix] after the node if it
900 /// is non-null. 887 /// is non-null.
901 visitSuffixed(ASTNode node, String suffix) { 888 visitSuffixed(ASTNode node, String suffix) {
902 if (node != null) { 889 if (node != null) {
903 node.accept(this); 890 node.accept(this);
904 writer.print(suffix); 891 print(suffix);
905 } 892 }
906 } 893 }
907 894
908 /// Safely visit the given [node], printing the [prefix] before the node if 895 /// Safely visit the given [node], printing the [prefix] before the node if
909 /// it is non-null. 896 /// it is non-null.
910 visitPrefixed(String prefix, ASTNode node) { 897 visitPrefixed(String prefix, ASTNode node) {
911 if (node != null) { 898 if (node != null) {
912 writer.print(prefix); 899 print(prefix);
913 node.accept(this); 900 node.accept(this);
914 } 901 }
915 } 902 }
916 903
917 /// Visit the given function [body], printing the [prefix] before if given 904 /// Visit the given function [body], printing the [prefix] before if given
918 /// body is not empty. 905 /// body is not empty.
919 visitPrefixedBody(String prefix, FunctionBody body) { 906 visitPrefixedBody(String prefix, FunctionBody body) {
920 if (body is! EmptyFunctionBody) { 907 if (body is! EmptyFunctionBody) {
921 writer.print(prefix); 908 print(prefix);
922 } 909 }
923 visit(body); 910 visit(body);
924 } 911 }
925 912
926 /// Emit the given [token], printing the prefix before the [token] 913 /// Print a list of [nodes], optionally separated by the given [separator].
927 /// if it is non-null.
928 emitPrefixedToken(String prefix, Token token) {
929 if (token != null) {
930 writer.print(prefix);
931 emitToken(token);
932 }
933 }
934
935 /// Emit the given [token], printing the suffix after the [token]
936 /// node if it is non-null.
937 emitToken(Token token, [String suffix]) {
938 if (token != null) {
939 emit(token);
940 if (suffix != null) {
941 writer.print(suffix);
942 }
943 }
944 }
945
946 /// Print a list of [nodes], separated by the given [separator].
947 visitList(NodeList<ASTNode> nodes, [String separator = '']) { 914 visitList(NodeList<ASTNode> nodes, [String separator = '']) {
948 if (nodes != null) { 915 if (nodes != null) {
949 var size = nodes.length; 916 var size = nodes.length;
950 for (var i = 0; i < size; i++) { 917 for (var i = 0; i < size; i++) {
951 if (i > 0) { 918 if (i > 0) {
952 writer.print(separator); 919 print(separator);
953 } 920 }
954 nodes[i].accept(this); 921 nodes[i].accept(this);
955 } 922 }
956 } 923 }
957 } 924 }
958 925
959 /// Print a list of [nodes], separated by the given [separator]. 926 /// Print a list of [nodes], separated by the given [separator].
960 visitSuffixedList(NodeList<ASTNode> nodes, String separator, String suffix) { 927 visitSuffixedList(NodeList<ASTNode> nodes, String separator, String suffix) {
961 if (nodes != null) { 928 if (nodes != null) {
962 var size = nodes.length; 929 var size = nodes.length;
963 if (size > 0) { 930 if (size > 0) {
964 for (var i = 0; i < size; i++) { 931 for (var i = 0; i < size; i++) {
965 if (i > 0) { 932 if (i > 0) {
966 writer.print(separator); 933 print(separator);
967 } 934 }
968 nodes[i].accept(this); 935 nodes[i].accept(this);
969 } 936 }
970 writer.print(suffix); 937 print(suffix);
971 } 938 }
972 } 939 }
973 } 940 }
974 941
975 /// Print a list of [nodes], separated by the given [separator]. 942 /// Print a list of [nodes], separated by the given [separator].
976 visitPrefixedList(String prefix, NodeList<ASTNode> nodes, 943 visitPrefixedList(String prefix, NodeList<ASTNode> nodes,
977 [String separator = null]) { 944 [String separator = null]) {
978 if (nodes != null) { 945 if (nodes != null) {
979 var size = nodes.length; 946 var size = nodes.length;
980 if (size > 0) { 947 if (size > 0) {
981 writer.print(prefix); 948 print(prefix);
982 for (var i = 0; i < size; i++) { 949 for (var i = 0; i < size; i++) {
983 if (i > 0 && separator != null) { 950 if (i > 0 && separator != null) {
984 writer.print(separator); 951 print(separator);
985 } 952 }
986 nodes[i].accept(this); 953 nodes[i].accept(this);
987 } 954 }
988 } 955 }
989 } 956 }
990 } 957 }
991 958
992 /// Emit the given [token], preceeded by any detected newlines or a minimum 959
993 /// as specified by [min]. 960 /// Emit the given [token], if it's non-null, preceded by any detected
994 emit(Token token, {min: 0}) { 961 /// newlines or a minimum as specified by [minNewlines], printing a [prefix]
962 /// before and a [suffix] after.
963 emitToken(Token token, {String prefix, String suffix,
964 int minNewlines: 0}) {
965 if (token != null) {
966 print(prefix);
967 emitPrecedingNewlines(token, min: minNewlines);
968 print(token.lexeme);
969 print(suffix);
970 }
971 }
972
973 /// Print the given [string] to the source writer if it's non-null.
974 print(String string) {
975 if (string != null) {
976 writer.print(string);
977 }
978 }
979
980 /// Emit a newline.
981 newline() {
982 writer.newline();
983 }
984
985 /// Emit [n] newlines.
986 newlines(n) {
987 writer.newlines(n);
988 }
989
990 /// Indent.
991 indent() {
992 writer.indent();
993 }
994
995 /// Unindent
996 unindent() {
997 writer.unindent();
998 }
999
1000 /// Emit any detected newlines or a minimum as specified by [minNewlines].
1001 emitPrecedingNewlines(Token token, {min: 0}) {
995 var comment = token.precedingComments; 1002 var comment = token.precedingComments;
996 var currentToken = comment != null ? comment : token; 1003 var currentToken = comment != null ? comment : token;
997 var newlines = max(min, countNewlinesBetween(previousToken, currentToken)); 1004 var lines = max(min, countNewlinesBetween(previousToken, currentToken));
998 writer.newlines(newlines); 1005 newlines(lines);
999 while (comment != null) { 1006 while (comment != null) {
1000 writer.print(comment.toString().trim()); 1007 print(comment.toString().trim());
1001 writer.newline(); 1008 newline();
1002 comment = comment.next; 1009 comment = comment.next;
1003 } 1010 }
1004 1011
1005 previousToken = token; 1012 previousToken = token;
1006 writer.print(token.lexeme);
1007 } 1013 }
1008 1014
1009 /// Count the blanks between these two nodes. 1015 /// Count the blanks between these two nodes.
1010 int countBlankLinesBetween(ASTNode lastNode, ASTNode currentNode) => 1016 int countBlankLinesBetween(ASTNode lastNode, ASTNode currentNode) =>
1011 countNewlinesBetween(lastNode.endToken, currentNode.beginToken); 1017 countNewlinesBetween(lastNode.endToken, currentNode.beginToken);
1012 1018
1013 /// Count newlines preceeding this [node]. 1019 /// Count newlines preceeding this [node].
1014 int countPrecedingNewlines(ASTNode node) => 1020 int countPrecedingNewlines(ASTNode node) =>
1015 countNewlinesBetween(node.beginToken.previous, node.beginToken); 1021 countNewlinesBetween(node.beginToken.previous, node.beginToken);
1016 1022
1017 /// Count newlines succeeding this [node]. 1023 /// Count newlines succeeding this [node].
1018 int countSucceedingNewlines(ASTNode node) => node == null ? 0 : 1024 int countSucceedingNewlines(ASTNode node) => node == null ? 0 :
1019 countNewlinesBetween(node.endToken, node.endToken.next); 1025 countNewlinesBetween(node.endToken, node.endToken.next);
1020 1026
1021 /// Count the blanks between these two nodes. 1027 /// Count the blanks between these two nodes.
1022 int countNewlinesBetween(Token last, Token current) { 1028 int countNewlinesBetween(Token last, Token current) {
1023 if (last == null || current == null) { 1029 if (last == null || current == null) {
1024 return 0; 1030 return 0;
1025 } 1031 }
1026 var lastLine = 1032 var lastLine =
1027 lineInfo.getLocation(last.offset).lineNumber; 1033 lineInfo.getLocation(last.offset).lineNumber;
1028 var currentLine = 1034 var currentLine =
1029 lineInfo.getLocation(current.offset).lineNumber; 1035 lineInfo.getLocation(current.offset).lineNumber;
1030 return currentLine - lastLine; 1036 return currentLine - lastLine;
1031 } 1037 }
1032 1038
1033 String toString() => writer.toString(); 1039 String toString() => writer.toString();
1034 1040
1035 } 1041 }
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