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

Side by Side Diff: pkg/analyzer/lib/src/services/writer.dart

Issue 136983004: Line-break indenter plumbing. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 source_writer; 5 library source_writer;
6 6
7 7
8 class Line { 8 class Line {
9 9
10 final tokens = <LineToken>[]; 10 final tokens = <LineToken>[];
11 final bool useTabs; 11 final bool useTabs;
12 final int spacesPerIndent; 12 final int spacesPerIndent;
13 final int indent;
13 final LinePrinter printer; 14 final LinePrinter printer;
14 15
15 Line({indent: 0, this.useTabs: false, this.spacesPerIndent: 2, 16 Line({this.indent: 0, this.useTabs: false, this.spacesPerIndent: 2,
16 this.printer: const SimpleLinePrinter()}) { 17 this.printer: const SimpleLinePrinter()}) {
17 if (indent > 0) { 18 if (indent > 0) {
18 _indent(indent); 19 _indent(indent);
19 } 20 }
20 } 21 }
21 22
22 void addSpace() { 23 void addSpace() {
23 addSpaces(1); 24 addSpaces(1);
24 } 25 }
25 26
(...skipping 21 matching lines...) Expand all
47 /// Base class for line printers 48 /// Base class for line printers
48 abstract class LinePrinter { 49 abstract class LinePrinter {
49 50
50 const LinePrinter(); 51 const LinePrinter();
51 52
52 /// Convert this [line] to a [String] representation. 53 /// Convert this [line] to a [String] representation.
53 String printLine(Line line); 54 String printLine(Line line);
54 } 55 }
55 56
56 57
58 typedef String Indenter(int n);
59
60
57 /// A simple line breaking [LinePrinter] 61 /// A simple line breaking [LinePrinter]
58 class SimpleLineBreaker extends LinePrinter { 62 class SimpleLineBreaker extends LinePrinter {
59 63
64 static final NO_OP_INDENTER = (n) => '';
65
60 final chunks = <Chunk>[]; 66 final chunks = <Chunk>[];
61 final int maxLength; 67 final int maxLength;
68 Indenter indenter;
62 69
63 SimpleLineBreaker(this.maxLength); 70 SimpleLineBreaker(this.maxLength, [this.indenter]) {
71 if (indenter == null) {
72 indenter = NO_OP_INDENTER;
73 }
74 }
64 75
65 String printLine(Line line) { 76 String printLine(Line line) {
66 var buf = new StringBuffer(); 77 var buf = new StringBuffer();
67 var chunks = breakLine(line); 78 var chunks = breakLine(line);
68 for (var i = 0; i < chunks.length; ++i) { 79 for (var i = 0; i < chunks.length; ++i) {
69 if (i > 0) { 80 if (i > 0) {
70 buf.write(indent(chunks[i])); 81 buf.write(indent(chunks[i], line.indent));
71 } else { 82 } else {
72 buf.write(chunks[i]); 83 buf.write(chunks[i]);
73 } 84 }
74 } 85 }
75 return buf.toString(); 86 return buf.toString();
76 } 87 }
77 88
78 String indent(Chunk chunk) { 89 String indent(Chunk chunk, int level) =>
79 return '\n' + chunk.toString(); 90 '\n' + indenter(level + 2) + chunk.toString();
80 }
81 91
82 List<Chunk> breakLine(Line line) { 92 List<Chunk> breakLine(Line line) {
83 93
84 var chunks = <Chunk>[]; 94 var chunks = <Chunk>[];
85 95
86 // The current unbroken line 96 // The current unbroken line
87 var current = new Chunk(maxLength: maxLength); 97 var current = new Chunk(maxLength: maxLength);
88 98
89 // A tentative working chunk that will either start a new line or get 99 // A tentative working chunk that will either start a new line or get
90 // absorbed into 'current' 100 // absorbed into 'current'
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 final StringBuffer buffer = new StringBuffer(); 248 final StringBuffer buffer = new StringBuffer();
239 Line currentLine; 249 Line currentLine;
240 250
241 final String lineSeparator; 251 final String lineSeparator;
242 int indentCount = 0; 252 int indentCount = 0;
243 253
244 LinePrinter linePrinter; 254 LinePrinter linePrinter;
245 LineToken _lastToken; 255 LineToken _lastToken;
246 256
247 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE, 257 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE,
248 int maxLineLength: 80}) { 258 bool useTabs: false, int spacesPerIndent: 2, int maxLineLength: 80}) {
249 linePrinter = new SimpleLineBreaker(maxLineLength); 259 // linePrinter = new SimpleLineBreaker(maxLineLength, (n) =>
260 // getIndentString(n, useTabs: useTabs, spacesPerIndent: spacesPerIndent) );
261 linePrinter = new SimpleLinePrinter();
250 currentLine = new Line(indent: indentCount, printer: linePrinter); 262 currentLine = new Line(indent: indentCount, printer: linePrinter);
251 } 263 }
252 264
253 LineToken get lastToken => _lastToken; 265 LineToken get lastToken => _lastToken;
254 266
255 _addToken(LineToken token) { 267 _addToken(LineToken token) {
256 _lastToken = token; 268 _lastToken = token;
257 currentLine.addToken(token); 269 currentLine.addToken(token);
258 } 270 }
259 271
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 '\t\t\t\t\t\t\t\t', 352 '\t\t\t\t\t\t\t\t',
341 '\t\t\t\t\t\t\t\t\t', 353 '\t\t\t\t\t\t\t\t\t',
342 '\t\t\t\t\t\t\t\t\t\t', 354 '\t\t\t\t\t\t\t\t\t\t',
343 '\t\t\t\t\t\t\t\t\t\t\t', 355 '\t\t\t\t\t\t\t\t\t\t\t',
344 '\t\t\t\t\t\t\t\t\t\t\t\t', 356 '\t\t\t\t\t\t\t\t\t\t\t\t',
345 '\t\t\t\t\t\t\t\t\t\t\t\t\t', 357 '\t\t\t\t\t\t\t\t\t\t\t\t\t',
346 '\t\t\t\t\t\t\t\t\t\t\t\t\t\t', 358 '\t\t\t\t\t\t\t\t\t\t\t\t\t\t',
347 ]; 359 ];
348 360
349 361
350 String getIndentString(int indentWidth, {bool useTabs: false}) => 362 String getIndentString(int indentWidth, {bool useTabs: false,
351 useTabs ? getTabs(indentWidth) : getSpaces(indentWidth); 363 int spacesPerIndent: 2}) => useTabs ? getTabs(indentWidth) :
364 getSpaces(indentWidth * spacesPerIndent);
352 365
353 String getSpaces(int n) => n < SPACES.length ? SPACES[n] : repeat(' ', n); 366 String getSpaces(int n) => n < SPACES.length ? SPACES[n] : repeat(' ', n);
354 367
355 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); 368 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n);
356 369
357 String repeat(String ch, int times) { 370 String repeat(String ch, int times) {
358 var sb = new StringBuffer(); 371 var sb = new StringBuffer();
359 for (var i = 0; i < times; ++i) { 372 for (var i = 0; i < times; ++i) {
360 sb.write(ch); 373 sb.write(ch);
361 } 374 }
362 return sb.toString(); 375 return sb.toString();
363 } 376 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/services/formatter_impl.dart ('k') | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698