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

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

Issue 18346013: Formatter re-think/updates. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library source_writer;
6
7
8 class Line {
9
10 final int lineLength;
11 final tokens = <LineToken>[];
12 final bool useTabs;
13 final int spacesPerIndent;
14
15 Line({indent: 0, this.lineLength: 80, this.useTabs: false,
16 this.spacesPerIndent: 2}) {
17 if (indent > 0) {
18 _indent(indent);
19 }
20 }
21
22 addSpace() {
23 addSpaces(1);
24 }
25
26 addSpaces(int n) {
27 if (n > 0) {
28 tokens.add(new SpaceToken(n));
29 }
30 }
31
32 addToken(LineToken token) {
33 tokens.add(token);
34 }
35
36 _indent(int n) {
37 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent));
38 }
39
40 String toString() {
41 var buffer = new StringBuffer();
42 for (var tok in tokens) {
43 buffer.write(tok.toString());
44 }
45 return buffer.toString();
46 }
47
48 }
49
50
51 class LineToken {
52
53 final String value;
54
55 LineToken(this.value);
56
57 String toString() => value;
58 }
59
60 class SpaceToken extends LineToken {
61
62 SpaceToken(int n) : super(getSpaces(n));
63 }
64
65 class TabToken extends LineToken {
66
67 TabToken(int n) : super(getTabs(n));
68 }
69
70
71 class NewlineToken extends LineToken {
72
73 NewlineToken(String value) : super(value);
74 }
75
76
77
78 class SourceWriter {
79
80 final StringBuffer buffer = new StringBuffer();
81 Line currentLine = new Line();
82
83 final String lineSeparator;
84 int indentCount = 0;
85
86 SourceWriter({int initialIndent: 0, this.lineSeparator: '\n'}) :
87 indentCount = initialIndent;
88
89 indent() {
90 ++indentCount;
91 }
92
93 unindent() {
94 --indentCount;
95 }
96
97 print(x) {
98 currentLine.addToken(new LineToken(x));
99 }
100
101 newline() {
102 currentLine.addToken(new NewlineToken(this.lineSeparator));
103 buffer.write(currentLine.toString());
104 currentLine = new Line(indent: indentCount);
105 }
106
107 space() {
108 spaces(1);
109 }
110
111 spaces(n) {
112 currentLine.addSpaces(n);
113 }
114
115 println(String s) {
116 print(s);
117 newline();
118 }
119
120 String toString() {
121 var source = new StringBuffer(buffer.toString());
122 source.write(currentLine);
123 return source.toString();
124 }
125 }
126
127
128 const SPACE = ' ';
129 final SPACES = [
130 '',
131 ' ',
132 ' ',
133 ' ',
134 ' ',
135 ' ',
136 ' ',
137 ' ',
138 ' ',
139 ' ',
140 ' ',
141 ' ',
142 ' ',
143 ' ',
144 ' ',
145 ' ',
146 ' ',
147 ];
148 final TABS = [
149 '',
150 '\t',
151 '\t\t',
152 '\t\t\t',
153 '\t\t\t\t',
154 '\t\t\t\t\t',
155 '\t\t\t\t\t\t',
156 '\t\t\t\t\t\t\t',
157 '\t\t\t\t\t\t\t\t',
158 '\t\t\t\t\t\t\t\t\t',
159 '\t\t\t\t\t\t\t\t\t\t',
160 '\t\t\t\t\t\t\t\t\t\t\t',
161 '\t\t\t\t\t\t\t\t\t\t\t\t',
162 '\t\t\t\t\t\t\t\t\t\t\t\t\t',
163 '\t\t\t\t\t\t\t\t\t\t\t\t\t\t',
164 ];
165
166
167 String getIndentString(int indentWidth, {bool useTabs: false}) =>
168 useTabs ? getTabs(indentWidth) : getSpaces(indentWidth);
169
170 String getSpaces(int n) => n < SPACES.length ? SPACES[n] : repeat(' ', n);
171
172 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n);
173
174 String repeat(String ch, int times) {
175 var sb = new StringBuffer();
176 for (var i = 0; i < times; ++i) {
177 sb.write(ch);
178 }
179 return sb.toString();
180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698