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

Unified Diff: pkg/analyzer/lib/src/services/formatter_impl.dart

Issue 113693003: dartfmt gets opinionated about 'gratuitous' linebreaks. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/services/formatter_impl.dart
===================================================================
--- pkg/analyzer/lib/src/services/formatter_impl.dart (revision 31210)
+++ pkg/analyzer/lib/src/services/formatter_impl.dart (working copy)
@@ -352,6 +352,10 @@
/// A flag to indicate that a newline should be emitted before the next token.
bool needsNewline = false;
+ /// A flag to indicate that user introduced newlines should be emitted before
+ /// the next token.
+ bool preserveNewlines = false;
+
/// A counter for spaces that should be emitted preceding the next token.
int leadingSpaces = 0;
@@ -462,7 +466,8 @@
visitCascadeExpression(CascadeExpression node) {
visit(node.target);
indent(2);
- visitNodes(node.cascadeSections);
+ newlines();
+ visitNodes(node.cascadeSections, separatedBy: newlines);
unindent(2);
}
@@ -490,6 +495,7 @@
}
visitClassDeclaration(ClassDeclaration node) {
+ preserveLeadingNewlines();
modifier(node.abstractKeyword);
token(node.classKeyword);
space();
@@ -547,6 +553,8 @@
visitNodes(node.declarations, separatedBy: newlines);
+ preserveLeadingNewlines();
+
// Handle trailing whitespace
token(node.endToken /* EOF */);
@@ -801,6 +809,7 @@
}
visitFunctionDeclaration(FunctionDeclaration node) {
+ preserveLeadingNewlines();
visitNode(node.returnType, followedBy: space);
token(node.propertyKeyword, followedBy: space);
visit(node.name);
@@ -965,10 +974,12 @@
modifier(node.constKeyword);
visitNode(node.typeArguments, followedBy: space);
token(node.leftBracket);
+ newlines();
indent();
- visitCommaSeparatedNodes(node.entries);
+ visitCommaSeparatedNodes(node.entries, followedBy: newlines);
optionalTrailingComma(node.rightBracket);
unindent();
+ newlines();
token(node.rightBracket);
}
@@ -1309,7 +1320,11 @@
}
/// Visit a comma-separated list of [nodes] if not null.
- visitCommaSeparatedNodes(NodeList<ASTNode> nodes) {
+ visitCommaSeparatedNodes(NodeList<ASTNode> nodes, {followedBy(): null}) {
+ //TODO(pquitslund): handle this more neatly
pquitslund 2013/12/18 17:16:47 EG: by introducing a const pointer to the space fu
Brian Wilkerson 2013/12/18 17:53:07 In order to do that, the space function will have
+ if (followedBy == null) {
+ followedBy = space;
+ }
if (nodes != null) {
var size = nodes.length;
if (size > 0) {
@@ -1319,7 +1334,7 @@
if (i > 0) {
var comma = node.beginToken.previous;
token(comma);
- space();
+ followedBy();
}
node.accept(this);
}
@@ -1368,6 +1383,12 @@
}
}
+ /// Indicate that user introduced newlines should be emitted before the next
+ /// token.
+ preserveLeadingNewlines() {
+ preserveNewlines = true;
+ }
+
token(Token token, {precededBy(), followedBy(), int minNewlines: 0}) {
if (token != null) {
if (needsNewline) {
@@ -1478,7 +1499,11 @@
currentToken = comment != null ? comment : token;
}
- var lines = max(min, countNewlinesBetween(previousToken, currentToken));
+ var lines = 0;
+ if (needsNewline || preserveNewlines) {
+ lines = max(min, countNewlinesBetween(previousToken, currentToken));
+ preserveNewlines = false;
+ }
emitNewlines(lines);
previousToken =
« no previous file with comments | « no previous file | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698