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

Side by Side Diff: lib/src/source_visitor.dart

Issue 840113002: Handle Windows line endings in multi-line strings. Fix #126. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Created 5 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 unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | test/formatter_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library dart_style.src.source_visitor; 5 library dart_style.src.source_visitor;
6 6
7 import 'package:analyzer/analyzer.dart'; 7 import 'package:analyzer/analyzer.dart';
8 import 'package:analyzer/src/generated/scanner.dart'; 8 import 'package:analyzer/src/generated/scanner.dart';
9 import 'package:analyzer/src/generated/source.dart'; 9 import 'package:analyzer/src/generated/source.dart';
10 10
11 import '../dart_style.dart'; 11 import '../dart_style.dart';
12 import 'chunk.dart'; 12 import 'chunk.dart';
13 import 'line_writer.dart'; 13 import 'line_writer.dart';
14 import 'whitespace.dart'; 14 import 'whitespace.dart';
15 15
16 /// An AST visitor that drives formatting heuristics. 16 /// An AST visitor that drives formatting heuristics.
17 class SourceVisitor implements AstVisitor { 17 class SourceVisitor implements AstVisitor {
18 final DartFormatter _formatter;
19
18 /// The writer to which the output lines are written. 20 /// The writer to which the output lines are written.
19 final LineWriter _writer; 21 final LineWriter _writer;
20 22
21 /// Cached line info for calculating blank lines. 23 /// Cached line info for calculating blank lines.
22 LineInfo _lineInfo; 24 LineInfo _lineInfo;
23 25
24 /// The source being formatted. 26 /// The source being formatted.
25 final SourceCode _source; 27 final SourceCode _source;
26 28
27 /// `true` if the visitor has written past the beginning of the selection in 29 /// `true` if the visitor has written past the beginning of the selection in
28 /// the original source text. 30 /// the original source text.
29 bool _passedSelectionStart = false; 31 bool _passedSelectionStart = false;
30 32
31 /// `true` if the visitor has written past the end of the selection in the 33 /// `true` if the visitor has written past the end of the selection in the
32 /// original source text. 34 /// original source text.
33 bool _passedSelectionEnd = false; 35 bool _passedSelectionEnd = false;
34 36
35 /// Initialize a newly created visitor to write source code representing 37 /// Initialize a newly created visitor to write source code representing
36 /// the visited nodes to the given [writer]. 38 /// the visited nodes to the given [writer].
37 SourceVisitor(DartFormatter formatter, this._lineInfo, SourceCode source) 39 SourceVisitor(formatter, this._lineInfo, SourceCode source)
38 : _source = source, 40 : _formatter = formatter,
41 _source = source,
39 _writer = new LineWriter(formatter, source); 42 _writer = new LineWriter(formatter, source);
40 43
41 /// Runs the visitor on [node], formatting its contents. 44 /// Runs the visitor on [node], formatting its contents.
42 /// 45 ///
43 /// Returns a [SourceCode] containing the resulting formatted source and 46 /// Returns a [SourceCode] containing the resulting formatted source and
44 /// updated selection, if any. 47 /// updated selection, if any.
45 /// 48 ///
46 /// This is the only method that should be called externally. Everything else 49 /// This is the only method that should be called externally. Everything else
47 /// is effectively private. 50 /// is effectively private.
48 SourceCode run(AstNode node) { 51 SourceCode run(AstNode node) {
(...skipping 1533 matching lines...) Expand 10 before | Expand all | Expand 10 after
1582 bool _isLambda(AstNode node) => 1585 bool _isLambda(AstNode node) =>
1583 node.parent is FunctionExpression && 1586 node.parent is FunctionExpression &&
1584 node.parent.parent is! FunctionDeclaration; 1587 node.parent.parent is! FunctionDeclaration;
1585 1588
1586 /// Writes the string literal [string] to the output. 1589 /// Writes the string literal [string] to the output.
1587 /// 1590 ///
1588 /// Splits multiline strings into separate chunks so that the line splitter 1591 /// Splits multiline strings into separate chunks so that the line splitter
1589 /// can handle them correctly. 1592 /// can handle them correctly.
1590 void _writeStringLiteral(String string, int offset) { 1593 void _writeStringLiteral(String string, int offset) {
1591 // Split each line of a multiline string into separate chunks. 1594 // Split each line of a multiline string into separate chunks.
1592 var lines = string.split("\n"); 1595 var lines = string.split(_formatter.lineEnding);
1593 1596
1594 _writeText(lines.first, offset); 1597 _writeText(lines.first, offset);
1595 offset += lines.first.length; 1598 offset += lines.first.length;
1596 1599
1597 for (var line in lines.skip(1)) { 1600 for (var line in lines.skip(1)) {
1598 _writer.writeWhitespace(Whitespace.newlineFlushLeft); 1601 _writer.writeWhitespace(Whitespace.newlineFlushLeft);
1599 offset++; 1602 offset++;
1600 _writeText(line, offset); 1603 _writeText(line, offset);
1601 offset += line.length; 1604 offset += line.length;
1602 } 1605 }
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 /// Gets the 1-based line number that the beginning of [token] lies on. 1801 /// Gets the 1-based line number that the beginning of [token] lies on.
1799 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; 1802 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber;
1800 1803
1801 /// Gets the 1-based line number that the end of [token] lies on. 1804 /// Gets the 1-based line number that the end of [token] lies on.
1802 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; 1805 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber;
1803 1806
1804 /// Gets the 1-based column number that the beginning of [token] lies on. 1807 /// Gets the 1-based column number that the beginning of [token] lies on.
1805 int _startColumn(Token token) => 1808 int _startColumn(Token token) =>
1806 _lineInfo.getLocation(token.offset).columnNumber; 1809 _lineInfo.getLocation(token.offset).columnNumber;
1807 } 1810 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | test/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698