Handle Windows line endings in multi-line strings. Fix #126.
BUG=https://github.com/dart-lang/dart_style/issues/126
R=pquitslund@google.com
Review URL: https://chromiumcodereview.appspot.com//840113002
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cdd3b36..e8f1125 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,4 @@
# 0.1.1-dev
-* Support formatting enums.
\ No newline at end of file
+* Support formatting enums (#120).
+* Handle Windows line endings in multiline strings (#126).
\ No newline at end of file
diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart
index 540735d..0a228f7 100644
--- a/lib/src/source_visitor.dart
+++ b/lib/src/source_visitor.dart
@@ -15,6 +15,8 @@
/// An AST visitor that drives formatting heuristics.
class SourceVisitor implements AstVisitor {
+ final DartFormatter _formatter;
+
/// The writer to which the output lines are written.
final LineWriter _writer;
@@ -34,8 +36,9 @@
/// Initialize a newly created visitor to write source code representing
/// the visited nodes to the given [writer].
- SourceVisitor(DartFormatter formatter, this._lineInfo, SourceCode source)
- : _source = source,
+ SourceVisitor(formatter, this._lineInfo, SourceCode source)
+ : _formatter = formatter,
+ _source = source,
_writer = new LineWriter(formatter, source);
/// Runs the visitor on [node], formatting its contents.
@@ -1589,7 +1592,7 @@
/// can handle them correctly.
void _writeStringLiteral(String string, int offset) {
// Split each line of a multiline string into separate chunks.
- var lines = string.split("\n");
+ var lines = string.split(_formatter.lineEnding);
_writeText(lines.first, offset);
offset += lines.first.length;
diff --git a/test/formatter_test.dart b/test/formatter_test.dart
index e35c6d3..4d46828 100644
--- a/test/formatter_test.dart
+++ b/test/formatter_test.dart
@@ -117,22 +117,32 @@
group('line endings', () {
test('uses given line ending', () {
expect(new DartFormatter(lineEnding: "%").format("var i = 1;"),
- equals("var i = 1;%"));
+ equals("var i = 1;%"));
});
test('infers \\r\\n if the first newline uses that', () {
expect(new DartFormatter().format("var\r\ni\n=\n1;\n"),
- equals("var i = 1;\r\n"));
+ equals("var i = 1;\r\n"));
});
test('infers \\n if the first newline uses that', () {
expect(new DartFormatter().format("var\ni\r\n=\r\n1;\r\n"),
- equals("var i = 1;\n"));
+ equals("var i = 1;\n"));
});
test('defaults to \\n if there are no newlines', () {
expect(new DartFormatter().format("var i =1;"),
- equals("var i = 1;\n"));
+ equals("var i = 1;\n"));
+ });
+
+ test('handles Windows line endings in multiline strings', () {
+ expect(new DartFormatter(lineEnding: "\r\n").formatStatement(
+ ' """first\r\n'
+ 'second\r\n'
+ 'third""" ;'), equals(
+ '"""first\r\n'
+ 'second\r\n'
+ 'third""";'));
});
});
}