Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 pretty_print; | |
| 6 | |
| 7 import 'utils.dart'; | |
| 8 | |
| 9 /** | |
| 10 * Returns a pretty-printed representation of [object]. | |
| 11 * | |
| 12 * If [maxLineLength] is passed, this will attempt to ensure that each line is | |
| 13 * no longer than [maxLineLength] characters long. This isn't guaranteed, since | |
| 14 * individual objects may have string representations that are too long, but | |
| 15 * most lines will be less than [maxLineLength] long. | |
| 16 * | |
| 17 * If [maxItems] is passed, [Iterable]s and [Map]s will only print their first | |
| 18 * [maxItems] members or key/value pairs, respectively. | |
| 19 */ | |
| 20 String prettyPrint(object, {int maxLineLength, int maxItems}) { | |
| 21 String _prettyPrint(object, int indent, Set seen, bool top) { | |
| 22 if (seen.contains(object)) return "(recursive)"; | |
| 23 seen = seen.union(new Set.from([object])); | |
| 24 String pp(child) => _prettyPrint(child, indent + 2, seen, false); | |
| 25 | |
| 26 if (object is Iterable) { | |
| 27 var typeName = object is List ? "" : typeName(object) + ":"; | |
| 28 | |
|
gram
2013/05/17 01:00:30
Please add a short comment at the start of each of
nweiz
2013/05/17 19:50:56
Done.
| |
| 29 var strings = object.map(pp).toList(); | |
| 30 if (maxItems != null && strings.length > maxItems) { | |
| 31 strings.replaceRange(maxItems - 1, strings.length, ['...']); | |
| 32 } | |
| 33 | |
| 34 var singleLine = "$typeName[${strings.join(', ')}]"; | |
| 35 if ((maxLineLength == null || | |
| 36 singleLine.length + indent <= maxLineLength) && | |
| 37 !singleLine.contains("\n")) { | |
| 38 return singleLine; | |
| 39 } | |
| 40 | |
| 41 return "$typeName[\n" + strings.map((string) { | |
| 42 return _indent(indent + 2) + string; | |
| 43 }).join(",\n") + "\n" + _indent(indent) + "]"; | |
| 44 } else if (object is Map) { | |
| 45 var strings = object.keys.map((key) { | |
| 46 return '${pp(key)}: ${pp(object[key])}'; | |
| 47 }).toList(); | |
| 48 if (maxItems != null && strings.length > maxItems) { | |
| 49 strings.replaceRange(maxItems - 1, strings.length, ['...']); | |
| 50 } | |
| 51 | |
| 52 var singleLine = "{${strings.join(", ")}}"; | |
| 53 if ((maxLineLength == null || | |
| 54 singleLine.length + indent <= maxLineLength) && | |
| 55 !singleLine.contains("\n")) { | |
| 56 return singleLine; | |
| 57 } | |
| 58 | |
| 59 return "{\n" + strings.map((string) { | |
| 60 return _indent(indent + 2) + string; | |
| 61 }).join(",\n") + "\n" + _indent(indent) + "}"; | |
| 62 } else if (object is String) { | |
| 63 var lines = object.split("\n"); | |
| 64 return "'" + lines.map(escapeString) | |
| 65 .join("\\n'\n${_indent(indent + 2)}'") + "'"; | |
| 66 } else { | |
| 67 var value = object.toString().replaceAll("\n", _indent(indent) + "\n"); | |
| 68 var defaultToString = value.startsWith("Instance of "); | |
| 69 if (top) value = "<$value>"; | |
| 70 | |
| 71 if (object is num || object is bool || object is Function || | |
| 72 object == null || defaultToString) { | |
| 73 return value; | |
| 74 } else { | |
| 75 var type = typeName(object); | |
| 76 // TODO(nweiz): find a public superclass of the object's type to | |
| 77 // display once there's a portable API to do that. | |
| 78 if (type.startsWith("_")) return value; | |
| 79 return "$type:$value"; | |
| 80 } | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 return _prettyPrint(object, 0, new Set(), true); | |
| 85 } | |
| 86 | |
| 87 String _indent(int length) => new List.filled(length, ' ').join(''); | |
| OLD | NEW |