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

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

Issue 1255643002: New, simpler and faster line splitter. (Closed) Base URL: https://github.com/dart-lang/dart_style.git@master
Patch Set: Optimize nesting. Reformat. Created 5 years, 5 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/line_splitter.dart ('k') | lib/src/nesting.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.line_writer; 5 library dart_style.src.line_writer;
6 6
7 import 'chunk.dart'; 7 import 'chunk.dart';
8 import 'dart_formatter.dart'; 8 import 'dart_formatter.dart';
9 import 'debug.dart' as debug; 9 import 'debug.dart' as debug;
10 import 'line_splitter.dart'; 10 import 'line_splitter.dart';
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 142
143 var chunks = _chunks.sublist(start, end); 143 var chunks = _chunks.sublist(start, end);
144 144
145 if (debug.traceLineWriter) { 145 if (debug.traceLineWriter) {
146 debug.log(debug.green("\nWriting:")); 146 debug.log(debug.green("\nWriting:"));
147 debug.dumpChunks(start, chunks); 147 debug.dumpChunks(start, chunks);
148 debug.log(); 148 debug.log();
149 } 149 }
150 150
151 // Run the line splitter. 151 // Run the line splitter.
152 var splitter = new LineSplitter(this, chunks, _blockIndentation); 152 var splitter = new LineSplitter(this, chunks, _blockIndentation, indent,
153 var solution = splitter.apply(indent, flushLeft: flushLeft); 153 flushLeft: flushLeft);
154 var splits = splitter.apply();
154 155
155 // Write the indentation of the first line. 156 // Write the indentation of the first line.
156 if (!flushLeft) { 157 if (!flushLeft) {
157 _buffer.write(" " * (indent + _blockIndentation)); 158 _buffer.write(" " * (indent + _blockIndentation));
158 } 159 }
159 160
160 // Write each chunk with the appropriate splits between them. 161 // Write each chunk with the appropriate splits between them.
161 for (var i = 0; i < chunks.length; i++) { 162 for (var i = 0; i < chunks.length; i++) {
162 var chunk = chunks[i]; 163 var chunk = chunks[i];
163 _writeChunk(chunk); 164 _writeChunk(chunk);
164 165
165 if (chunk.blockChunks.isNotEmpty) { 166 if (chunk.blockChunks.isNotEmpty) {
166 if (!solution.splits.shouldSplitAt(i)) { 167 if (!splits.shouldSplitAt(i)) {
167 // This block didn't split (which implies none of the child blocks 168 // This block didn't split (which implies none of the child blocks
168 // of that block split either, recursively), so write them all inline. 169 // of that block split either, recursively), so write them all inline.
169 _writeChunksUnsplit(chunk.blockChunks); 170 _writeChunksUnsplit(chunk.blockChunks);
170 } else { 171 } else {
171 // Include the formatted block contents. 172 // Include the formatted block contents.
172 var block = formatBlock(chunk, solution.splits.getColumn(i)); 173 var block = formatBlock(chunk, splits.getColumn(i));
173 174
174 // If this block contains one of the selection markers, tell the 175 // If this block contains one of the selection markers, tell the
175 // writer where it ended up in the final output. 176 // writer where it ended up in the final output.
176 if (block.selectionStart != null) { 177 if (block.selectionStart != null) {
177 _selectionStart = length + block.selectionStart; 178 _selectionStart = length + block.selectionStart;
178 } 179 }
179 180
180 if (block.selectionEnd != null) { 181 if (block.selectionEnd != null) {
181 _selectionEnd = length + block.selectionEnd; 182 _selectionEnd = length + block.selectionEnd;
182 } 183 }
183 184
184 _buffer.write(block.text); 185 _buffer.write(block.text);
185 } 186 }
186 } 187 }
187 188
188 if (i == chunks.length - 1) { 189 if (i == chunks.length - 1) {
189 // Don't write trailing whitespace after the last chunk. 190 // Don't write trailing whitespace after the last chunk.
190 } else if (solution.splits.shouldSplitAt(i)) { 191 } else if (splits.shouldSplitAt(i)) {
191 _buffer.write(_lineEnding); 192 _buffer.write(_lineEnding);
192 if (chunk.isDouble) _buffer.write(_lineEnding); 193 if (chunk.isDouble) _buffer.write(_lineEnding);
193 194
194 _buffer.write(" " * (solution.splits.getColumn(i))); 195 _buffer.write(" " * (splits.getColumn(i)));
195 } else { 196 } else {
196 if (chunk.spaceWhenUnsplit) _buffer.write(" "); 197 if (chunk.spaceWhenUnsplit) _buffer.write(" ");
197 } 198 }
198 } 199 }
199 200
200 return solution.cost; 201 return splits.cost;
201 } 202 }
202 203
203 /// Writes [chunks] (and any child chunks of them, recursively) without any 204 /// Writes [chunks] (and any child chunks of them, recursively) without any
204 /// splitting. 205 /// splitting.
205 void _writeChunksUnsplit(List<Chunk> chunks) { 206 void _writeChunksUnsplit(List<Chunk> chunks) {
206 for (var chunk in chunks) { 207 for (var chunk in chunks) {
207 _writeChunk(chunk); 208 _writeChunk(chunk);
208 209
209 if (chunk.spaceWhenUnsplit) _buffer.write(" "); 210 if (chunk.spaceWhenUnsplit) _buffer.write(" ");
210 211
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 final int selectionStart; 268 final int selectionStart;
268 269
269 /// Where in the resulting buffer the selection end point should appear if it 270 /// Where in the resulting buffer the selection end point should appear if it
270 /// was contained within this split list of chunks. 271 /// was contained within this split list of chunks.
271 /// 272 ///
272 /// Otherwise, this is `null`. 273 /// Otherwise, this is `null`.
273 final int selectionEnd; 274 final int selectionEnd;
274 275
275 FormatResult(this.text, this.cost, this.selectionStart, this.selectionEnd); 276 FormatResult(this.text, this.cost, this.selectionStart, this.selectionEnd);
276 } 277 }
OLDNEW
« no previous file with comments | « lib/src/line_splitter.dart ('k') | lib/src/nesting.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698