| 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 library analysis_server.src.status.ast_writer; |
| 6 |
| 7 import 'package:analysis_server/src/status/utilities.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 |
| 10 /** |
| 11 * A visitor that will produce an HTML representation of an AST structure. |
| 12 */ |
| 13 class AstWriter extends UnifyingAstVisitor { |
| 14 /** |
| 15 * The buffer on which the HTML is to be written. |
| 16 */ |
| 17 final StringBuffer buffer; |
| 18 |
| 19 /** |
| 20 * The current level of indentation. |
| 21 */ |
| 22 int indentLevel = 0; |
| 23 |
| 24 /** |
| 25 * Initialize a newly created element writer to write the HTML representation |
| 26 * of visited nodes on the given [buffer]. |
| 27 */ |
| 28 AstWriter(this.buffer); |
| 29 |
| 30 @override |
| 31 void visitNode(AstNode node) { |
| 32 _writeNode(node); |
| 33 indentLevel++; |
| 34 try { |
| 35 node.visitChildren(this); |
| 36 } finally { |
| 37 indentLevel--; |
| 38 } |
| 39 } |
| 40 |
| 41 /** |
| 42 * Return the name of the given [node], or `null` if the given node is not a |
| 43 * declaration. |
| 44 */ |
| 45 String _getName(AstNode node) { |
| 46 if (node is ClassTypeAlias) { |
| 47 return node.name.name; |
| 48 } else if (node is ClassDeclaration) { |
| 49 return node.name.name; |
| 50 } else if (node is ConstructorDeclaration) { |
| 51 if (node.name == null) { |
| 52 return node.returnType.name; |
| 53 } else { |
| 54 return node.returnType.name + '.' + node.name.name; |
| 55 } |
| 56 } else if (node is ConstructorName) { |
| 57 return node.toSource(); |
| 58 } else if (node is FieldDeclaration) { |
| 59 return _getNames(node.fields); |
| 60 } else if (node is FunctionDeclaration) { |
| 61 SimpleIdentifier nameNode = node.name; |
| 62 if (nameNode != null) { |
| 63 return nameNode.name; |
| 64 } |
| 65 } else if (node is FunctionTypeAlias) { |
| 66 return node.name.name; |
| 67 } else if (node is Identifier) { |
| 68 return node.name; |
| 69 } else if (node is MethodDeclaration) { |
| 70 return node.name.name; |
| 71 } else if (node is TopLevelVariableDeclaration) { |
| 72 return _getNames(node.variables); |
| 73 } else if (node is TypeName) { |
| 74 return node.toSource(); |
| 75 } else if (node is TypeParameter) { |
| 76 return node.name.name; |
| 77 } else if (node is VariableDeclaration) { |
| 78 return node.name.name; |
| 79 } |
| 80 return null; |
| 81 } |
| 82 |
| 83 /** |
| 84 * Return a string containing a comma-separated list of the names of all of |
| 85 * the variables in the given list of [variables]. |
| 86 */ |
| 87 String _getNames(VariableDeclarationList variables) { |
| 88 StringBuffer buffer = new StringBuffer(); |
| 89 bool first = true; |
| 90 for (VariableDeclaration variable in variables.variables) { |
| 91 if (first) { |
| 92 first = false; |
| 93 } else { |
| 94 buffer.write(', '); |
| 95 } |
| 96 buffer.write(variable.name.name); |
| 97 } |
| 98 return buffer.toString(); |
| 99 } |
| 100 |
| 101 void _indent([int extra = 0]) { |
| 102 for (int i = 0; i < indentLevel; i++) { |
| 103 buffer.write('┊ '); |
| 104 } |
| 105 if (extra > 0) { |
| 106 buffer.write('┊ '); |
| 107 for (int i = 1; i < extra; i++) { |
| 108 buffer.write(' '); |
| 109 } |
| 110 } |
| 111 } |
| 112 |
| 113 /** |
| 114 * Write a representation of the given [node] to the buffer. |
| 115 */ |
| 116 void _writeNode(AstNode node) { |
| 117 _indent(); |
| 118 buffer.write(node.runtimeType); |
| 119 buffer.write(' <span style="color:gray">['); |
| 120 buffer.write(node.offset); |
| 121 buffer.write('..'); |
| 122 buffer.write(node.offset + node.length - 1); |
| 123 buffer.write(']</span>'); |
| 124 buffer.write('<br>'); |
| 125 _writeProperty('name', _getName(node)); |
| 126 if (node is BinaryExpression) { |
| 127 _writeProperty('static element', node.staticElement); |
| 128 _writeProperty('static type', node.staticType); |
| 129 _writeProperty('propagated element', node.propagatedElement); |
| 130 _writeProperty('propagated type', node.propagatedType); |
| 131 } else if (node is CompilationUnit) { |
| 132 _writeProperty("element", node.element); |
| 133 } else if (node is ExportDirective) { |
| 134 _writeProperty("element", node.element); |
| 135 } else if (node is FunctionExpressionInvocation) { |
| 136 _writeProperty('static element', node.staticElement); |
| 137 _writeProperty('static type', node.staticType); |
| 138 _writeProperty('propagated element', node.propagatedElement); |
| 139 _writeProperty('propagated type', node.propagatedType); |
| 140 } else if (node is ImportDirective) { |
| 141 _writeProperty("element", node.element); |
| 142 } else if (node is LibraryDirective) { |
| 143 _writeProperty("element", node.element); |
| 144 } else if (node is PartDirective) { |
| 145 _writeProperty("element", node.element); |
| 146 } else if (node is PartOfDirective) { |
| 147 _writeProperty("element", node.element); |
| 148 } else if (node is PostfixExpression) { |
| 149 _writeProperty('static element', node.staticElement); |
| 150 _writeProperty('static type', node.staticType); |
| 151 _writeProperty('propagated element', node.propagatedElement); |
| 152 _writeProperty('propagated type', node.propagatedType); |
| 153 } else if (node is PrefixExpression) { |
| 154 _writeProperty('static element', node.staticElement); |
| 155 _writeProperty('static type', node.staticType); |
| 156 _writeProperty('propagated element', node.propagatedElement); |
| 157 _writeProperty('propagated type', node.propagatedType); |
| 158 } else if (node is SimpleIdentifier) { |
| 159 _writeProperty('static element', node.staticElement); |
| 160 _writeProperty('static type', node.staticType); |
| 161 _writeProperty('propagated element', node.propagatedElement); |
| 162 _writeProperty('propagated type', node.propagatedType); |
| 163 } else if (node is SimpleStringLiteral) { |
| 164 _writeProperty("value", node.value); |
| 165 } else if (node is Expression) { |
| 166 _writeProperty('static type', node.staticType); |
| 167 _writeProperty('propagated type', node.propagatedType); |
| 168 } |
| 169 } |
| 170 |
| 171 /** |
| 172 * Write the [value] of the property with the given [name]. |
| 173 */ |
| 174 void _writeProperty(String name, Object value) { |
| 175 if (value != null) { |
| 176 _indent(2); |
| 177 buffer.write('$name = '); |
| 178 buffer.write(encodeHtml(value.toString())); |
| 179 buffer.write('<br>'); |
| 180 } |
| 181 } |
| 182 } |
| OLD | NEW |