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

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

Issue 23458038: Ensure formatted CUs end with a newline (dartbug.com/13188). (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/lib/src/services/writer.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 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 var scriptTag = node.scriptTag; 448 var scriptTag = node.scriptTag;
449 var directives = node.directives; 449 var directives = node.directives;
450 visit(scriptTag); 450 visit(scriptTag);
451 451
452 visitNodes(directives, separatedBy: newlines, followedBy: newlines); 452 visitNodes(directives, separatedBy: newlines, followedBy: newlines);
453 453
454 visitNodes(node.declarations, separatedBy: newlines); 454 visitNodes(node.declarations, separatedBy: newlines);
455 455
456 // Handle trailing whitespace 456 // Handle trailing whitespace
457 token(node.endToken /* EOF */); 457 token(node.endToken /* EOF */);
458
459 // Be a good citizen, end with a NL
460 ensureTrailingNewline();
458 } 461 }
459 462
460 visitConditionalExpression(ConditionalExpression node) { 463 visitConditionalExpression(ConditionalExpression node) {
461 visit(node.condition); 464 visit(node.condition);
462 space(); 465 space();
463 token(node.question); 466 token(node.question);
464 space(); 467 space();
465 visit(node.thenExpression); 468 visit(node.thenExpression);
466 space(); 469 space();
467 token(node.colon); 470 token(node.colon);
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 append(' '); 1206 append(' ');
1204 } 1207 }
1205 1208
1206 /// Emit a breakable space 1209 /// Emit a breakable space
1207 breakableSpace() { 1210 breakableSpace() {
1208 //Implement 1211 //Implement
1209 } 1212 }
1210 1213
1211 /// Append the given [string] to the source writer if it's non-null. 1214 /// Append the given [string] to the source writer if it's non-null.
1212 append(String string) { 1215 append(String string) {
1213 if (string != null) { 1216 if (string != null && !string.isEmpty) {
1214 writer.print(string); 1217 writer.print(string);
1215 } 1218 }
1216 } 1219 }
1217 1220
1218 /// Indent. 1221 /// Indent.
1219 indent() { 1222 indent() {
1220 writer.indent(); 1223 writer.indent();
1221 } 1224 }
1222 1225
1223 /// Unindent 1226 /// Unindent
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1259 } 1262 }
1260 1263
1261 previousToken = comment; 1264 previousToken = comment;
1262 comment = comment.next; 1265 comment = comment.next;
1263 } 1266 }
1264 1267
1265 previousToken = token; 1268 previousToken = token;
1266 return lines; 1269 return lines;
1267 } 1270 }
1268 1271
1272
1273 ensureTrailingNewline() {
1274 if (writer.lastToken is! NewlineToken) {
1275 writer.newline();
1276 }
1277 }
1278
1279
1269 /// Test if this [comment] is at the end of a line. 1280 /// Test if this [comment] is at the end of a line.
1270 bool isAtEOL(Token comment) => 1281 bool isAtEOL(Token comment) =>
1271 comment != null && comment.toString().trim().startsWith(twoSlashes) && 1282 comment != null && comment.toString().trim().startsWith(twoSlashes) &&
1272 sameLine(comment, previousToken); 1283 sameLine(comment, previousToken);
1273 1284
1274 /// Emit this [comment], inserting leading whitespace if appropriate. 1285 /// Emit this [comment], inserting leading whitespace if appropriate.
1275 emitComment(Token comment, Token previousToken) { 1286 emitComment(Token comment, Token previousToken) {
1276 if (!writer.currentLine.isWhitespace() && !isBlock(comment)) { 1287 if (!writer.currentLine.isWhitespace() && !isBlock(comment)) {
1277 var ws = countSpacesBetween(previousToken, comment); 1288 var ws = countSpacesBetween(previousToken, comment);
1278 // Preserve one space but no more 1289 // Preserve one space but no more
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1348 var lastLine = 1359 var lastLine =
1349 lineInfo.getLocation(lastOffset).lineNumber; 1360 lineInfo.getLocation(lastOffset).lineNumber;
1350 var currentLine = 1361 var currentLine =
1351 lineInfo.getLocation(currentOffset).lineNumber; 1362 lineInfo.getLocation(currentOffset).lineNumber;
1352 return currentLine - lastLine; 1363 return currentLine - lastLine;
1353 } 1364 }
1354 1365
1355 String toString() => writer.toString(); 1366 String toString() => writer.toString();
1356 1367
1357 } 1368 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer_experimental/lib/src/services/writer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698