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

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

Issue 136863003: Line breaking plumbing (continued). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 11 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/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/writer.dart
===================================================================
--- pkg/analyzer/lib/src/services/writer.dart (revision 31744)
+++ pkg/analyzer/lib/src/services/writer.dart (working copy)
@@ -63,9 +63,22 @@
SimpleLineBreaker(this.maxLength);
String printLine(Line line) {
- //TODO(pquitslund): implement
+ var buf = new StringBuffer();
+ var chunks = breakLine(line);
+ for (var i = 0; i < chunks.length; ++i) {
+ if (i > 0) {
+ buf.write(indent(chunks[i]));
+ } else {
+ buf.write(chunks[i]);
+ }
+ }
+ return buf.toString();
}
+ String indent(Chunk chunk) {
+ return '\n' + chunk.toString();
+ }
+
List<Chunk> breakLine(Line line) {
var chunks = <Chunk>[];
@@ -93,7 +106,9 @@
if (work.fits(tok)) {
work.add(tok);
} else {
- current.add(work);
+ if (!isWhitespace(work)) {
+ current.add(work);
+ }
if (current.length > 0) {
chunks.add(current);
current = new Chunk(maxLength: maxLength);
@@ -112,6 +127,11 @@
return chunks;
}
+ bool isWhitespace(Chunk chunk) {
pquitslund 2014/01/13 16:39:55 This *can't* be the best way to test this... Idea
+ var str = chunk.buffer.toString();
+ return str.lastIndexOf(new RegExp(r"(\w+)")) == str.length - 1;
+ }
+
/// Test whether this token is a good start for a new working chunk
bool goodStart(LineToken tok, Chunk workingChunk) =>
tok is SpaceToken && tok.breakWeight >= workingChunk.start.breakWeight;
@@ -148,7 +168,7 @@
/// A working piece of text used in calculating line breaks
class Chunk implements LineText {
- final buffer = new StringBuffer();
+ final StringBuffer buffer = new StringBuffer();
int maxLength;
SpaceToken start;
@@ -167,7 +187,7 @@
text.addTo(this);
}
- String toString() => buffer.toString().trim();
+ String toString() => buffer.toString();
void addTo(Chunk chunk) {
chunk.buffer.write(start.value);
@@ -226,7 +246,7 @@
SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE,
int maxLineLength: 80}) {
- linePrinter = new SimpleLinePrinter();
+ linePrinter = new SimpleLineBreaker(maxLineLength);
currentLine = new Line(indent: indentCount, printer: linePrinter);
}
@@ -269,7 +289,7 @@
spaces(1);
}
- void spaces(n, {breakWeight: null}) {
+ void spaces(n, {breakWeight: DEFAULT_SPACE_WEIGHT}) {
currentLine.addSpaces(n, breakWeight: breakWeight);
}
« no previous file with comments | « pkg/analyzer/lib/src/services/formatter_impl.dart ('k') | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698