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.chunk; | 5 library dart_style.src.chunk; |
6 | 6 |
7 import 'debug.dart'; | 7 import 'debug.dart'; |
8 | 8 |
9 /// A chunk of non-breaking output text terminated by a hard or soft newline. | 9 /// A chunk of non-breaking output text terminated by a hard or soft newline. |
10 /// | 10 /// |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 if (spaceWhenUnsplit) parts.add("space"); | 161 if (spaceWhenUnsplit) parts.add("space"); |
162 if (_isDouble) parts.add("double"); | 162 if (_isDouble) parts.add("double"); |
163 | 163 |
164 if (_indent == null) { | 164 if (_indent == null) { |
165 parts.add("(no split info)"); | 165 parts.add("(no split info)"); |
166 } else if (isHardSplit) { | 166 } else if (isHardSplit) { |
167 parts.add("hard"); | 167 parts.add("hard"); |
168 } else { | 168 } else { |
169 var param = "p$_param"; | 169 var param = "p$_param"; |
170 | 170 |
171 if (_param.cost != Cost.NORMAL) param += " \$${_param.cost}"; | 171 if (_param.cost != Cost.normal) param += " \$${_param.cost}"; |
172 | 172 |
173 if (_param.implies.isNotEmpty) { | 173 if (_param.implies.isNotEmpty) { |
174 var impliedIds = _param.implies.map( | 174 var impliedIds = _param.implies.map( |
175 (param) => "p${param.id}").join(" "); | 175 (param) => "p${param.id}").join(" "); |
176 param += " -> $impliedIds"; | 176 param += " -> $impliedIds"; |
177 } | 177 } |
178 | 178 |
179 parts.add(param); | 179 parts.add(param); |
180 } | 180 } |
181 | 181 |
182 return parts.join(" "); | 182 return parts.join(" "); |
183 } | 183 } |
184 } | 184 } |
185 | 185 |
186 /// Constants for the cost heuristics used to determine which set of splits is | 186 /// Constants for the cost heuristics used to determine which set of splits is |
187 /// most desirable. | 187 /// most desirable. |
188 class Cost { | 188 class Cost { |
189 /// The smallest cost. | 189 /// The smallest cost. |
190 /// | 190 /// |
191 /// This isn't zero because we want to ensure all splitting has *some* cost, | 191 /// This isn't zero because we want to ensure all splitting has *some* cost, |
192 /// otherwise, the formatter won't try to keep things on one line at all. | 192 /// otherwise, the formatter won't try to keep things on one line at all. |
193 /// Almost all splits and spans use this. Greater costs tend to come from a | 193 /// Almost all splits and spans use this. Greater costs tend to come from a |
194 /// greater number of nested spans. | 194 /// greater number of nested spans. |
195 static const NORMAL = 1; | 195 static const normal = 1; |
196 | 196 |
197 /// The cost of splitting after a "=" both for assignment and initialization. | 197 /// The cost of splitting after a "=" both for assignment and initialization. |
198 static const ASSIGNMENT = 2; | 198 static const assignment = 2; |
199 | 199 |
200 /// The cost of a single character that goes past the page limit. | 200 /// The cost of a single character that goes past the page limit. |
201 /// | 201 /// |
202 /// This cost is high to ensure any solution that fits in the page is | 202 /// This cost is high to ensure any solution that fits in the page is |
203 /// preferred over one that does not. | 203 /// preferred over one that does not. |
204 static const OVERFLOW_CHAR = 1000; | 204 static const overflowChar = 1000; |
205 } | 205 } |
206 | 206 |
207 /// Controls whether or not one or more soft split [Chunk]s are split. | 207 /// Controls whether or not one or more soft split [Chunk]s are split. |
208 /// | 208 /// |
209 /// When [LineSplitter] tries to split a line to fit within its page width, it | 209 /// When [LineSplitter] tries to split a line to fit within its page width, it |
210 /// does so by trying different combinations of parameters to see which set of | 210 /// does so by trying different combinations of parameters to see which set of |
211 /// active ones yields the best result. | 211 /// active ones yields the best result. |
212 class SplitParam { | 212 class SplitParam { |
213 static int _nextId = 0; | 213 static int _nextId = 0; |
214 | 214 |
215 /// A semi-unique numeric indentifier for the param. | 215 /// A semi-unique numeric indentifier for the param. |
216 /// | 216 /// |
217 /// This is useful for debugging and also speeds up using the param in hash | 217 /// This is useful for debugging and also speeds up using the param in hash |
218 /// sets. Ids are *semi*-unique because they may wrap around in long running | 218 /// sets. Ids are *semi*-unique because they may wrap around in long running |
219 /// processes. Since params are equal based on their identity, this is | 219 /// processes. Since params are equal based on their identity, this is |
220 /// innocuous and prevents ids from growing without bound. | 220 /// innocuous and prevents ids from growing without bound. |
221 final int id = _nextId = (_nextId + 1) & 0x0fffffff; | 221 final int id = _nextId = (_nextId + 1) & 0x0fffffff; |
222 | 222 |
223 /// The cost of this param when split. | 223 /// The cost of this param when split. |
224 final int cost; | 224 final int cost; |
225 | 225 |
226 /// The other [SplitParam]s that are "implied" by this one. | 226 /// The other [SplitParam]s that are "implied" by this one. |
227 /// | 227 /// |
228 /// Implication means that if the splitter chooses to split this param, it | 228 /// Implication means that if the splitter chooses to split this param, it |
229 /// must also split all of its implied ones (transitively). Implication is | 229 /// must also split all of its implied ones (transitively). Implication is |
230 /// one-way. If A implies B, it's fine to split B without splitting A. | 230 /// one-way. If A implies B, it's fine to split B without splitting A. |
231 final implies = <SplitParam>[]; | 231 final implies = <SplitParam>[]; |
232 | 232 |
233 /// Creates a new [SplitParam]. | 233 /// Creates a new [SplitParam]. |
234 SplitParam([this.cost = Cost.NORMAL]); | 234 SplitParam([this.cost = Cost.normal]); |
235 | 235 |
236 String toString() => "$id"; | 236 String toString() => "$id"; |
237 | 237 |
238 int get hashCode => id.hashCode; | 238 int get hashCode => id.hashCode; |
239 | 239 |
240 bool operator ==(other) => identical(this, other); | 240 bool operator ==(other) => identical(this, other); |
241 } | 241 } |
242 | 242 |
243 /// Delimits a range of chunks that must end up on the same line to avoid an | 243 /// Delimits a range of chunks that must end up on the same line to avoid an |
244 /// additional cost. | 244 /// additional cost. |
(...skipping 27 matching lines...) Expand all Loading... |
272 result += " - $end"; | 272 result += " - $end"; |
273 } else { | 273 } else { |
274 result += "..."; | 274 result += "..."; |
275 } | 275 } |
276 | 276 |
277 if (cost != null) result += " \$$cost"; | 277 if (cost != null) result += " \$$cost"; |
278 | 278 |
279 return result + ")"; | 279 return result + ")"; |
280 } | 280 } |
281 } | 281 } |
OLD | NEW |