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

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

Issue 477373002: Improve formatting of variable declarations/assignments. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/lib/src/services/formatter_impl.dart ('k') | pkg/analyzer/test/services/data/wrap_tests.data » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/services/writer.dart
diff --git a/pkg/analyzer/lib/src/services/writer.dart b/pkg/analyzer/lib/src/services/writer.dart
index f4443cdc98ef2a7772ed1611ae19509fd2955f28..6c4ae5c65fa5747b6a74cd54f6ad5c9835b5c241 100644
--- a/pkg/analyzer/lib/src/services/writer.dart
+++ b/pkg/analyzer/lib/src/services/writer.dart
@@ -101,6 +101,31 @@ class SimpleLineBreaker extends LinePrinter {
List<Chunk> breakLine(Line line) {
List<LineToken> tokens = preprocess(line.tokens);
List<Chunk> chunks = <Chunk>[new Chunk(line.indentLevel, maxLength, tokens)];
+ // try SINGLE_SPACE_WEIGHT
+ {
+ Chunk chunk = chunks[0];
+ if (chunk.length > maxLength) {
+ for (int i = 0; i < tokens.length; i++) {
+ LineToken token = tokens[i];
+ if (token is SpaceToken && token.breakWeight == SINGLE_SPACE_WEIGHT) {
+ var beforeChunk = chunk.subChunk(chunk.indent, 0, i);
+ var restChunk = chunk.subChunk(chunk.indent + 2, i + 1);
+ // check if 'init' in 'var v = init;' fits a line
+ if (restChunk.length < maxLength) {
+ return [beforeChunk, restChunk];
+ }
+ // check if 'var v = method(' in 'var v = method(args)' does not fit
+ int weight = chunk.findMinSpaceWeight();
+ if (chunk.getLengthToSpaceWithWeight(weight) > maxLength) {
+ chunks = [beforeChunk, restChunk];
+ }
+ // done anyway
+ break;
+ }
+ }
+ }
+ }
+ // other spaces
while (true) {
List<Chunk> newChunks = <Chunk>[];
bool hasChanges = false;
@@ -115,8 +140,8 @@ class SimpleLineBreaker extends LinePrinter {
int length = 0;
for (int i = 0; i < tokens.length; i++) {
LineToken token = tokens[i];
- if (token is SpaceToken && token.breakWeight == weight
- && i < tokens.length - 1) {
+ if (token is SpaceToken && token.breakWeight == weight &&
+ i < tokens.length - 1) {
LineToken nextToken = tokens[i + 1];
if (length + token.length + nextToken.length > maxLength) {
newChunks.add(chunk.subChunk(newIndent, start, i));
@@ -211,6 +236,8 @@ bool isWhitespace(String string) => string.codeUnits.every(
final LINE_START = new SpaceToken(0);
const DEFAULT_SPACE_WEIGHT = UNBREAKABLE_SPACE_WEIGHT - 1;
+/// The weight of a space after '=' in variable declaration or assignment
+const SINGLE_SPACE_WEIGHT = UNBREAKABLE_SPACE_WEIGHT - 2;
const UNBREAKABLE_SPACE_WEIGHT = 100000000;
/// Simple non-breaking printer
@@ -243,7 +270,20 @@ class Chunk {
this.tokens.addAll(tokens);
}
- int get length => tokens.fold(0, (len, token) => len + token.length);
+ int get length {
+ return tokens.fold(0, (len, token) => len + token.length);
+ }
+
+ int getLengthToSpaceWithWeight(int weight) {
+ int length = 0;
+ for (LineToken token in tokens) {
+ if (token is SpaceToken && token.breakWeight == weight) {
+ break;
+ }
+ length += token.length;
+ }
+ return length;
+ }
bool fits(LineToken a, LineToken b) {
return length + a.length + a.length <= maxLength;
@@ -253,6 +293,12 @@ class Chunk {
tokens.add(token);
}
+ bool hasInitializerSpace() {
+ return tokens.any((token) {
+ return token is SpaceToken && token.breakWeight == SINGLE_SPACE_WEIGHT;
+ });
+ }
+
bool hasAnySpace() {
return tokens.any((token) => token is SpaceToken);
}
« no previous file with comments | « pkg/analyzer/lib/src/services/formatter_impl.dart ('k') | pkg/analyzer/test/services/data/wrap_tests.data » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698