OLD | NEW |
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.code_formatter; | 5 library dart_style.src.code_formatter; |
6 | 6 |
7 import 'package:analyzer/src/string_source.dart'; | 7 import 'package:analyzer/src/string_source.dart'; |
8 import 'package:analyzer/src/generated/parser.dart'; | 8 import 'package:analyzer/src/generated/parser.dart'; |
9 import 'package:analyzer/src/generated/scanner.dart'; | 9 import 'package:analyzer/src/generated/scanner.dart'; |
10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
11 | 11 |
12 import 'error_listener.dart'; | 12 import 'error_listener.dart'; |
13 import 'source_visitor.dart'; | 13 import 'source_visitor.dart'; |
14 | 14 |
| 15 /// Describes a chunk of source code that is to be formatted or has been |
| 16 /// formatted. |
| 17 class SourceCode { |
| 18 /// The [uri] where the source code is from. |
| 19 /// |
| 20 /// Used in error messages if the code cannot be parsed. |
| 21 final String uri; |
| 22 |
| 23 /// The Dart source code text. |
| 24 final String text; |
| 25 |
| 26 /// Whether the source is a compilation unit or a bare statement. |
| 27 final bool isCompilationUnit; |
| 28 |
| 29 /// The offset in [text] where the selection begins, or `null` if there is |
| 30 /// no selection. |
| 31 final int selectionStart; |
| 32 |
| 33 /// The number of selected characters or `null` if there is no selection. |
| 34 final int selectionLength; |
| 35 |
| 36 SourceCode(this.text, |
| 37 {this.uri, this.isCompilationUnit: true, this.selectionStart, |
| 38 this.selectionLength}) { |
| 39 // Must either provide both selection bounds or neither. |
| 40 if ((selectionStart == null) != (selectionLength == null)) { |
| 41 throw new ArgumentError( |
| 42 "Is selectionStart is provided, selectionLength must be too."); |
| 43 } |
| 44 |
| 45 if (selectionStart != null) { |
| 46 if (selectionStart < 0) { |
| 47 throw new ArgumentError("selectionStart must be non-negative."); |
| 48 } |
| 49 |
| 50 if (selectionStart > text.length) { |
| 51 throw new ArgumentError("selectionStart must be within text."); |
| 52 } |
| 53 } |
| 54 |
| 55 if (selectionLength != null) { |
| 56 if (selectionLength < 0) { |
| 57 throw new ArgumentError("selectionLength must be non-negative."); |
| 58 } |
| 59 |
| 60 if (selectionStart + selectionLength > text.length) { |
| 61 throw new ArgumentError("selectionLength must end within text."); |
| 62 } |
| 63 } |
| 64 } |
| 65 } |
| 66 |
15 /// Dart source code formatter. | 67 /// Dart source code formatter. |
16 class DartFormatter { | 68 class DartFormatter { |
17 /// The string that newlines should use. | 69 /// The string that newlines should use. |
18 /// | 70 /// |
19 /// If not explicitly provided, this is inferred from the source text. If the | 71 /// If not explicitly provided, this is inferred from the source text. If the |
20 /// first newline is `\r\n` (Windows), it will use that. Otherwise, it uses | 72 /// first newline is `\r\n` (Windows), it will use that. Otherwise, it uses |
21 /// Unix-style line endings (`\n`). | 73 /// Unix-style line endings (`\n`). |
22 String lineEnding; | 74 String lineEnding; |
23 | 75 |
24 /// The number of characters allowed in a single line. | 76 /// The number of characters allowed in a single line. |
25 final int pageWidth; | 77 final int pageWidth; |
26 | 78 |
27 /// The number of levels of indentation to prefix the output lines with. | 79 /// The number of levels of indentation to prefix the output lines with. |
28 final int indent; | 80 final int indent; |
29 | 81 |
30 /// Creates a new formatter for Dart code. | 82 /// Creates a new formatter for Dart code. |
31 /// | 83 /// |
32 /// If [lineEnding] is given, that will be used for any newlines in the | 84 /// If [lineEnding] is given, that will be used for any newlines in the |
33 /// output. Otherwise, the line separator will be inferred from the line | 85 /// output. Otherwise, the line separator will be inferred from the line |
34 /// endings in the source file. | 86 /// endings in the source file. |
35 /// | 87 /// |
36 /// If [indent] is given, that many levels of indentation will be prefixed | 88 /// If [indent] is given, that many levels of indentation will be prefixed |
37 /// before each resulting line in the output. | 89 /// before each resulting line in the output. |
38 DartFormatter({this.lineEnding, int pageWidth, this.indent: 0}) | 90 DartFormatter({this.lineEnding, int pageWidth, this.indent: 0}) |
39 : this.pageWidth = (pageWidth == null) ? 80 : pageWidth; | 91 : this.pageWidth = (pageWidth == null) ? 80 : pageWidth; |
40 | 92 |
41 /// Format the given [source] string containing an entire Dart compilation | 93 /// Formats the given [source] string containing an entire Dart compilation |
42 /// unit. | 94 /// unit. |
43 /// | 95 /// |
44 /// If [uri] is given, it is a [String] or [Uri] used to identify the file | 96 /// If [uri] is given, it is a [String] or [Uri] used to identify the file |
45 /// being formatted in error messages. | 97 /// being formatted in error messages. |
46 String format(String source, {uri}) { | 98 String format(String source, {uri}) { |
47 if (uri == null) { | 99 if (uri == null) { |
48 uri = "<unknown>"; | 100 uri = "<unknown>"; |
49 } else if (uri is Uri) { | 101 } else if (uri is Uri) { |
50 uri = uri.toString(); | 102 uri = uri.toString(); |
51 } else if (uri is String) { | 103 } else if (uri is String) { |
52 // Do nothing. | 104 // Do nothing. |
53 } else { | 105 } else { |
54 throw new ArgumentError("uri must be `null`, a Uri, or a String."); | 106 throw new ArgumentError("uri must be `null`, a Uri, or a String."); |
55 } | 107 } |
56 | 108 |
57 return _format(source, uri: uri, isCompilationUnit: true); | 109 return formatSource( |
| 110 new SourceCode(source, uri: uri, isCompilationUnit: true)).text; |
58 } | 111 } |
59 | 112 |
60 /// Format the given [source] string containing a single Dart statement. | 113 /// Formats the given [source] string containing a single Dart statement. |
61 String formatStatement(String source) { | 114 String formatStatement(String source) { |
62 return _format(source, isCompilationUnit: false); | 115 return formatSource(new SourceCode(source, isCompilationUnit: false)).text; |
63 } | 116 } |
64 | 117 |
65 String _format(String source, {String uri, bool isCompilationUnit}) { | 118 /// Formats the given [source]. |
| 119 /// |
| 120 /// Returns a new [SourceCode] containing the formatted code and the resulting |
| 121 /// selection, if any. |
| 122 SourceCode formatSource(SourceCode source) { |
66 var errorListener = new ErrorListener(); | 123 var errorListener = new ErrorListener(); |
67 | 124 |
68 // Tokenize the source. | 125 // Tokenize the source. |
69 var reader = new CharSequenceReader(source); | 126 var reader = new CharSequenceReader(source.text); |
70 var stringSource = new StringSource(source, uri); | 127 var stringSource = new StringSource(source.text, source.uri); |
71 var scanner = new Scanner(stringSource, reader, errorListener); | 128 var scanner = new Scanner(stringSource, reader, errorListener); |
72 var startToken = scanner.tokenize(); | 129 var startToken = scanner.tokenize(); |
73 var lineInfo = new LineInfo(scanner.lineStarts); | 130 var lineInfo = new LineInfo(scanner.lineStarts); |
74 | 131 |
75 // Infer the line ending if not given one. Do it here since now we know | 132 // Infer the line ending if not given one. Do it here since now we know |
76 // where the lines start. | 133 // where the lines start. |
77 if (lineEnding == null) { | 134 if (lineEnding == null) { |
78 // If the first newline is "\r\n", use that. Otherwise, use "\n". | 135 // If the first newline is "\r\n", use that. Otherwise, use "\n". |
79 if (scanner.lineStarts.length > 1 && | 136 if (scanner.lineStarts.length > 1 && |
80 scanner.lineStarts[1] >= 2 && | 137 scanner.lineStarts[1] >= 2 && |
81 source[scanner.lineStarts[1] - 2] == '\r') { | 138 source.text[scanner.lineStarts[1] - 2] == '\r') { |
82 lineEnding = "\r\n"; | 139 lineEnding = "\r\n"; |
83 } else { | 140 } else { |
84 lineEnding = "\n"; | 141 lineEnding = "\n"; |
85 } | 142 } |
86 } | 143 } |
87 | 144 |
88 errorListener.throwIfErrors(); | 145 errorListener.throwIfErrors(); |
89 | 146 |
90 // Parse it. | 147 // Parse it. |
91 var parser = new Parser(stringSource, errorListener); | 148 var parser = new Parser(stringSource, errorListener); |
92 parser.parseAsync = true; | 149 parser.parseAsync = true; |
93 | 150 |
94 var node; | 151 var node; |
95 if (isCompilationUnit) { | 152 if (source.isCompilationUnit) { |
96 node = parser.parseCompilationUnit(startToken); | 153 node = parser.parseCompilationUnit(startToken); |
97 } else { | 154 } else { |
98 node = parser.parseStatement(startToken); | 155 node = parser.parseStatement(startToken); |
99 } | 156 } |
100 | 157 |
101 errorListener.throwIfErrors(); | 158 errorListener.throwIfErrors(); |
102 | 159 |
103 // Format it. | 160 // Format it. |
104 var buffer = new StringBuffer(); | 161 var visitor = new SourceVisitor(this, lineInfo, source); |
105 var visitor = new SourceVisitor(this, lineInfo, source, buffer); | 162 return visitor.run(node); |
106 | |
107 visitor.run(node); | |
108 | |
109 // Be a good citizen, end with a newline. | |
110 if (isCompilationUnit) buffer.write(lineEnding); | |
111 | |
112 return buffer.toString(); | |
113 } | 163 } |
114 } | 164 } |
OLD | NEW |