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

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

Issue 153203002: Indent fix for trailing comments (dartbug.com/16383). (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/data/stmt_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
===================================================================
--- pkg/analyzer/lib/src/services/writer.dart (revision 32259)
+++ pkg/analyzer/lib/src/services/writer.dart (working copy)
@@ -8,16 +8,16 @@
class Line {
- final tokens = <LineToken>[];
+ final List<LineToken> tokens = <LineToken>[];
final bool useTabs;
final int spacesPerIndent;
- final int indent;
+ final int indentLevel;
final LinePrinter printer;
- Line({this.indent: 0, this.useTabs: false, this.spacesPerIndent: 2,
+ Line({this.indentLevel: 0, this.useTabs: false, this.spacesPerIndent: 2,
this.printer: const SimpleLinePrinter()}) {
- if (indent > 0) {
- _indent(indent);
+ if (indentLevel > 0) {
+ indent(indentLevel);
}
}
@@ -35,10 +35,13 @@
tokens.add(token);
}
+ bool isEmpty() => tokens.isEmpty;
+
bool isWhitespace() => tokens.every((tok) => tok is SpaceToken);
- void _indent(int n) {
- tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent));
+ void indent(int n) {
+ tokens.insert(0,
+ useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent));
}
String toString() => printer.printLine(this);
@@ -79,7 +82,7 @@
var chunks = breakLine(line);
for (var i = 0; i < chunks.length; ++i) {
if (i > 0) {
- buf.write(indent(chunks[i], line.indent));
+ buf.write(indent(chunks[i], line.indentLevel));
} else {
buf.write(chunks[i]);
}
@@ -309,7 +312,7 @@
} else {
linePrinter = new SimpleLinePrinter();
}
- currentLine = new Line(indent: indentCount, printer: linePrinter);
+ currentLine = newLine();
}
LineToken get lastToken => _lastToken;
@@ -321,6 +324,10 @@
void indent() {
++indentCount;
+ // Rather than fiddle with deletions/insertions just start fresh
+ if (currentLine.isWhitespace()) {
+ currentLine = newLine();
+ }
}
void newline() {
@@ -329,7 +336,7 @@
}
_addToken(new NewlineToken(this.lineSeparator));
buffer.write(currentLine.toString());
- currentLine = new Line(indent: indentCount, printer: linePrinter);
+ currentLine = newLine();
}
void newlines(int num) {
@@ -338,12 +345,12 @@
}
}
- void print(x) {
+ void write(x) {
_addToken(new LineToken(x));
}
- void println(String s) {
- print(s);
+ void writeln(String s) {
+ write(s);
newline();
}
@@ -357,8 +364,14 @@
void unindent() {
--indentCount;
+ // Rather than fiddle with deletions/insertions just start fresh
+ if (currentLine.isWhitespace()) {
+ currentLine = newLine();
+ }
}
+ Line newLine() => new Line(indentLevel: indentCount, printer: linePrinter);
+
String toString() {
var source = new StringBuffer(buffer.toString());
if (!currentLine.isWhitespace()) {
« no previous file with comments | « pkg/analyzer/lib/src/services/formatter_impl.dart ('k') | pkg/analyzer/test/services/data/stmt_tests.data » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698