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

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

Issue 822273004: Add API to provide a selection range, and return the updated selection after formatting. (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 | « lib/src/chunk.dart ('k') | lib/src/formatter_exception.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.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 if (selectionLength == null) {
Brian Wilkerson 2015/01/08 00:17:45 I think this case is covered by line 40.
Bob Nystrom 2015/01/08 00:36:38 Oops, forgot to delete this! Done.
55 throw new ArgumentError(
56 "Is selectionStart is provided, selectionLength must be too.");
57 }
58 }
59
60 if (selectionLength != null) {
61 if (selectionLength < 0) {
62 throw new ArgumentError("selectionLength must be non-negative.");
63 }
64
65 if (selectionStart + selectionLength > text.length) {
66 throw new ArgumentError("selectionLength must end within text.");
67 }
68 }
69 }
70 }
71
15 /// Dart source code formatter. 72 /// Dart source code formatter.
16 class DartFormatter { 73 class DartFormatter {
17 /// The string that newlines should use. 74 /// The string that newlines should use.
18 /// 75 ///
19 /// If not explicitly provided, this is inferred from the source text. If the 76 /// 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 77 /// first newline is `\r\n` (Windows), it will use that. Otherwise, it uses
21 /// Unix-style line endings (`\n`). 78 /// Unix-style line endings (`\n`).
22 String lineEnding; 79 String lineEnding;
23 80
24 /// The number of characters allowed in a single line. 81 /// The number of characters allowed in a single line.
25 final int pageWidth; 82 final int pageWidth;
26 83
27 /// The number of levels of indentation to prefix the output lines with. 84 /// The number of levels of indentation to prefix the output lines with.
28 final int indent; 85 final int indent;
29 86
30 /// Creates a new formatter for Dart code. 87 /// Creates a new formatter for Dart code.
31 /// 88 ///
32 /// If [lineEnding] is given, that will be used for any newlines in the 89 /// 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 90 /// output. Otherwise, the line separator will be inferred from the line
34 /// endings in the source file. 91 /// endings in the source file.
35 /// 92 ///
36 /// If [indent] is given, that many levels of indentation will be prefixed 93 /// If [indent] is given, that many levels of indentation will be prefixed
37 /// before each resulting line in the output. 94 /// before each resulting line in the output.
38 DartFormatter({this.lineEnding, int pageWidth, this.indent: 0}) 95 DartFormatter({this.lineEnding, int pageWidth, this.indent: 0})
39 : this.pageWidth = (pageWidth == null) ? 80 : pageWidth; 96 : this.pageWidth = (pageWidth == null) ? 80 : pageWidth;
40 97
41 /// Format the given [source] string containing an entire Dart compilation 98 /// Formats the given [source] string containing an entire Dart compilation
42 /// unit. 99 /// unit.
43 /// 100 ///
44 /// If [uri] is given, it is a [String] or [Uri] used to identify the file 101 /// If [uri] is given, it is a [String] or [Uri] used to identify the file
45 /// being formatted in error messages. 102 /// being formatted in error messages.
46 String format(String source, {uri}) { 103 String format(String source, {uri}) {
47 if (uri == null) { 104 if (uri == null) {
48 uri = "<unknown>"; 105 uri = "<unknown>";
49 } else if (uri is Uri) { 106 } else if (uri is Uri) {
50 uri = uri.toString(); 107 uri = uri.toString();
51 } else if (uri is String) { 108 } else if (uri is String) {
52 // Do nothing. 109 // Do nothing.
53 } else { 110 } else {
54 throw new ArgumentError("uri must be `null`, a Uri, or a String."); 111 throw new ArgumentError("uri must be `null`, a Uri, or a String.");
55 } 112 }
56 113
57 return _format(source, uri: uri, isCompilationUnit: true); 114 return formatSource(
115 new SourceCode(source, uri: uri, isCompilationUnit: true)).text;
58 } 116 }
59 117
60 /// Format the given [source] string containing a single Dart statement. 118 /// Formats the given [source] string containing a single Dart statement.
61 String formatStatement(String source) { 119 String formatStatement(String source) {
62 return _format(source, isCompilationUnit: false); 120 return formatSource(new SourceCode(source, isCompilationUnit: false)).text;
63 } 121 }
64 122
65 String _format(String source, {String uri, bool isCompilationUnit}) { 123 /// Formats the given [source].
124 ///
125 /// Returns a new [SourceCode] containing the formatted code and the resulting
126 /// selection, if any.
127 SourceCode formatSource(SourceCode source) {
66 var errorListener = new ErrorListener(); 128 var errorListener = new ErrorListener();
67 129
68 // Tokenize the source. 130 // Tokenize the source.
69 var reader = new CharSequenceReader(source); 131 var reader = new CharSequenceReader(source.text);
70 var stringSource = new StringSource(source, uri); 132 var stringSource = new StringSource(source.text, source.uri);
71 var scanner = new Scanner(stringSource, reader, errorListener); 133 var scanner = new Scanner(stringSource, reader, errorListener);
72 var startToken = scanner.tokenize(); 134 var startToken = scanner.tokenize();
73 var lineInfo = new LineInfo(scanner.lineStarts); 135 var lineInfo = new LineInfo(scanner.lineStarts);
74 136
75 // Infer the line ending if not given one. Do it here since now we know 137 // Infer the line ending if not given one. Do it here since now we know
76 // where the lines start. 138 // where the lines start.
77 if (lineEnding == null) { 139 if (lineEnding == null) {
78 // If the first newline is "\r\n", use that. Otherwise, use "\n". 140 // If the first newline is "\r\n", use that. Otherwise, use "\n".
79 if (scanner.lineStarts.length > 1 && 141 if (scanner.lineStarts.length > 1 &&
80 scanner.lineStarts[1] >= 2 && 142 scanner.lineStarts[1] >= 2 &&
81 source[scanner.lineStarts[1] - 2] == '\r') { 143 source.text[scanner.lineStarts[1] - 2] == '\r') {
82 lineEnding = "\r\n"; 144 lineEnding = "\r\n";
83 } else { 145 } else {
84 lineEnding = "\n"; 146 lineEnding = "\n";
85 } 147 }
86 } 148 }
87 149
88 errorListener.throwIfErrors(); 150 errorListener.throwIfErrors();
89 151
90 // Parse it. 152 // Parse it.
91 var parser = new Parser(stringSource, errorListener); 153 var parser = new Parser(stringSource, errorListener);
Brian Wilkerson 2015/01/08 00:17:45 It would be nice (in a later CL) if there were a w
Bob Nystrom 2015/01/08 00:36:38 Yup. Filed a tracking bug: https://github.com/dart
92 parser.parseAsync = true; 154 parser.parseAsync = true;
Brian Wilkerson 2015/01/08 00:17:45 This should no longer be necessary. Async support
Bob Nystrom 2015/01/08 00:36:38 dart_style is using this constraint for the analyz
Brian Wilkerson 2015/01/08 05:31:29 In that case, you probably also want to add: pa
93 155
94 var node; 156 var node;
95 if (isCompilationUnit) { 157 if (source.isCompilationUnit) {
96 node = parser.parseCompilationUnit(startToken); 158 node = parser.parseCompilationUnit(startToken);
97 } else { 159 } else {
98 node = parser.parseStatement(startToken); 160 node = parser.parseStatement(startToken);
99 } 161 }
100 162
101 errorListener.throwIfErrors(); 163 errorListener.throwIfErrors();
102 164
103 // Format it. 165 // Format it.
104 var buffer = new StringBuffer(); 166 var visitor = new SourceVisitor(this, lineInfo, source);
105 var visitor = new SourceVisitor(this, lineInfo, source, buffer); 167 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 } 168 }
114 } 169 }
OLDNEW
« no previous file with comments | « lib/src/chunk.dart ('k') | lib/src/formatter_exception.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698