Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, 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 import 'dart:convert'; | |
| 6 | |
| 7 import 'package:analyzer/exception/exception.dart'; | |
| 8 import 'package:analyzer/src/dart/element/element.dart'; | |
| 9 import 'package:analyzer/src/generated/constant.dart'; | |
| 10 import 'package:analyzer/src/generated/source.dart'; | |
| 11 | |
| 12 /** | |
| 13 * Utility methods that can be mixed in to classes that produce an HTML | |
| 14 * representation of a tree structure. | |
| 15 */ | |
| 16 abstract class TreeWriter { | |
|
Brian Wilkerson
2017/05/31 14:54:43
This would need to stay because of the subclasses.
devoncarew
2017/06/01 16:59:11
Done.
| |
| 17 /** | |
| 18 * The buffer on which the HTML is to be written. | |
| 19 */ | |
| 20 StringBuffer buffer; | |
| 21 | |
| 22 /** | |
| 23 * The current level of indentation. | |
| 24 */ | |
| 25 int indentLevel = 0; | |
| 26 | |
| 27 /** | |
| 28 * A list containing the exceptions that were caught while attempting to write | |
| 29 * out the tree structure. | |
| 30 */ | |
| 31 List<CaughtException> exceptions = <CaughtException>[]; | |
| 32 | |
| 33 void indent([int extra = 0]) { | |
| 34 for (int i = 0; i < indentLevel; i++) { | |
| 35 buffer.write('┊ '); | |
| 36 } | |
| 37 if (extra > 0) { | |
| 38 buffer.write('┊ '); | |
| 39 for (int i = 1; i < extra; i++) { | |
| 40 buffer.write(' '); | |
| 41 } | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Write a representation of the given [properties] to the buffer. | |
| 47 */ | |
| 48 void writeProperties(Map<String, Object> properties) { | |
| 49 List<String> propertyNames = properties.keys.toList(); | |
| 50 propertyNames.sort(); | |
| 51 for (String propertyName in propertyNames) { | |
| 52 writeProperty(propertyName, properties[propertyName]); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Write the [value] of the property with the given [name]. | |
| 58 */ | |
| 59 void writeProperty(String name, Object value) { | |
| 60 if (value != null) { | |
| 61 indent(2); | |
| 62 buffer.write('$name = '); | |
| 63 _writePropertyValue(value, indentLevel); | |
| 64 buffer.write('<br>'); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 String _toString(Object value) { | |
| 69 try { | |
| 70 if (value is Source) { | |
| 71 return 'Source (uri="${value.uri}", path="${value.fullName}")'; | |
| 72 } else if (value is ElementAnnotationImpl) { | |
| 73 StringBuffer buffer = new StringBuffer(); | |
| 74 buffer.write(_toString(value.element)); | |
| 75 EvaluationResultImpl result = value.evaluationResult; | |
| 76 if (result == null) { | |
| 77 buffer.write(': no result'); | |
| 78 } else { | |
| 79 buffer.write(': value = '); | |
| 80 buffer.write(result.value); | |
| 81 buffer.write('; errors = '); | |
| 82 buffer.write(result.errors); | |
| 83 } | |
| 84 return buffer.toString(); | |
| 85 } else { | |
| 86 return value.toString(); | |
| 87 } | |
| 88 } catch (exception, stackTrace) { | |
| 89 exceptions.add(new CaughtException(exception, stackTrace)); | |
| 90 } | |
| 91 return null; | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Write the [value] of the property with the given [name]. | |
| 96 */ | |
| 97 void _writePropertyValue(Object value, int baseIndent) { | |
| 98 if (value is List) { | |
| 99 if (value.isEmpty) { | |
| 100 buffer.write('[]'); | |
| 101 } else { | |
| 102 int elementIndent = baseIndent + 2; | |
| 103 buffer.write('[<br>'); | |
| 104 for (Object element in value) { | |
| 105 indent(elementIndent); | |
| 106 _writePropertyValue(element, elementIndent); | |
| 107 buffer.write('<br>'); | |
| 108 } | |
| 109 indent(baseIndent); | |
| 110 buffer.write(']'); | |
| 111 } | |
| 112 } else { | |
| 113 String valueString = _toString(value); | |
| 114 if (valueString == null) { | |
| 115 buffer.write('<span style="color: #FF0000">'); | |
| 116 buffer.write(HTML_ESCAPE.convert(value.runtimeType.toString())); | |
| 117 buffer.write('</span>'); | |
| 118 } else { | |
| 119 buffer.write(HTML_ESCAPE.convert(valueString)); | |
| 120 } | |
| 121 } | |
| 122 } | |
| 123 } | |
| OLD | NEW |