OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dart_style.src.dart_formatter; | |
6 | |
7 import 'dart:math' as math; | |
8 | |
9 import 'package:analyzer/analyzer.dart'; | |
10 import 'package:analyzer/src/generated/parser.dart'; | |
11 import 'package:analyzer/src/generated/scanner.dart'; | |
12 import 'package:analyzer/src/generated/source.dart'; | |
13 import 'package:analyzer/src/string_source.dart'; | |
14 | |
15 import 'error_listener.dart'; | |
16 import 'formatter_exception.dart'; | |
17 import 'source_code.dart'; | |
18 import 'source_visitor.dart'; | |
19 | |
20 /// Dart source code formatter. | |
21 class DartFormatter { | |
22 /// The string that newlines should use. | |
23 /// | |
24 /// If not explicitly provided, this is inferred from the source text. If the | |
25 /// first newline is `\r\n` (Windows), it will use that. Otherwise, it uses | |
26 /// Unix-style line endings (`\n`). | |
27 String lineEnding; | |
28 | |
29 /// The number of characters allowed in a single line. | |
30 final int pageWidth; | |
31 | |
32 /// The number of characters of indentation to prefix the output lines with. | |
33 final int indent; | |
34 | |
35 /// Creates a new formatter for Dart code. | |
36 /// | |
37 /// If [lineEnding] is given, that will be used for any newlines in the | |
38 /// output. Otherwise, the line separator will be inferred from the line | |
39 /// endings in the source file. | |
40 /// | |
41 /// If [indent] is given, that many levels of indentation will be prefixed | |
42 /// before each resulting line in the output. | |
43 DartFormatter({this.lineEnding, int pageWidth, this.indent: 0}) | |
44 : this.pageWidth = (pageWidth == null) ? 80 : pageWidth; | |
45 | |
46 /// Formats the given [source] string containing an entire Dart compilation | |
47 /// unit. | |
48 /// | |
49 /// If [uri] is given, it is a [String] or [Uri] used to identify the file | |
50 /// being formatted in error messages. | |
51 String format(String source, {uri}) { | |
52 if (uri == null) { | |
53 uri = "<unknown>"; | |
54 } else if (uri is Uri) { | |
55 uri = uri.toString(); | |
56 } else if (uri is String) { | |
57 // Do nothing. | |
58 } else { | |
59 throw new ArgumentError("uri must be `null`, a Uri, or a String."); | |
60 } | |
61 | |
62 return formatSource( | |
63 new SourceCode(source, uri: uri, isCompilationUnit: true)).text; | |
64 } | |
65 | |
66 /// Formats the given [source] string containing a single Dart statement. | |
67 String formatStatement(String source) { | |
68 return formatSource(new SourceCode(source, isCompilationUnit: false)).text; | |
69 } | |
70 | |
71 /// Formats the given [source]. | |
72 /// | |
73 /// Returns a new [SourceCode] containing the formatted code and the resulting | |
74 /// selection, if any. | |
75 SourceCode formatSource(SourceCode source) { | |
76 var errorListener = new ErrorListener(); | |
77 | |
78 // Tokenize the source. | |
79 var reader = new CharSequenceReader(source.text); | |
80 var stringSource = new StringSource(source.text, source.uri); | |
81 var scanner = new Scanner(stringSource, reader, errorListener); | |
82 var startToken = scanner.tokenize(); | |
83 var lineInfo = new LineInfo(scanner.lineStarts); | |
84 | |
85 // Infer the line ending if not given one. Do it here since now we know | |
86 // where the lines start. | |
87 if (lineEnding == null) { | |
88 // If the first newline is "\r\n", use that. Otherwise, use "\n". | |
89 if (scanner.lineStarts.length > 1 && | |
90 scanner.lineStarts[1] >= 2 && | |
91 source.text[scanner.lineStarts[1] - 2] == '\r') { | |
92 lineEnding = "\r\n"; | |
93 } else { | |
94 lineEnding = "\n"; | |
95 } | |
96 } | |
97 | |
98 errorListener.throwIfErrors(); | |
99 | |
100 // Parse it. | |
101 var parser = new Parser(stringSource, errorListener); | |
102 | |
103 var node; | |
104 if (source.isCompilationUnit) { | |
105 node = parser.parseCompilationUnit(startToken); | |
106 } else { | |
107 node = parser.parseStatement(startToken); | |
108 | |
109 // Make sure we consumed all of the source. | |
110 var token = node.endToken.next; | |
111 if (token.type != TokenType.EOF) { | |
112 var error = new AnalysisError.con2( | |
113 stringSource, | |
114 token.offset, | |
115 math.max(token.length, 1), | |
116 ParserErrorCode.UNEXPECTED_TOKEN, | |
117 [token.lexeme]); | |
118 | |
119 throw new FormatterException([error]); | |
120 } | |
121 } | |
122 | |
123 errorListener.throwIfErrors(); | |
124 | |
125 // Format it. | |
126 var visitor = new SourceVisitor(this, lineInfo, source); | |
127 return visitor.run(node); | |
128 } | |
129 } | |
OLD | NEW |