 Chromium Code Reviews
 Chromium Code Reviews Issue 934603003:
  Add properties to the element tree  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
    
  
    Issue 934603003:
  Add properties to the element tree  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 analysis_server.src.status.ast_writer; | 5 library analysis_server.src.status.ast_writer; | 
| 6 | 6 | 
| 7 import 'dart:convert'; | 7 import 'package:analysis_server/src/status/tree_writer.dart'; | 
| 8 | |
| 9 import 'package:analysis_server/src/get_handler.dart'; | |
| 10 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; | 
| 11 import 'package:analyzer/src/generated/element.dart'; | 9 import 'dart:collection'; | 
| 12 import 'package:analyzer/src/generated/java_engine.dart'; | |
| 13 import 'package:analyzer/src/generated/source.dart'; | |
| 14 | 10 | 
| 15 /** | 11 /** | 
| 16 * A visitor that will produce an HTML representation of an AST structure. | 12 * A visitor that will produce an HTML representation of an AST structure. | 
| 17 */ | 13 */ | 
| 18 class AstWriter extends UnifyingAstVisitor { | 14 class AstWriter extends UnifyingAstVisitor with TreeWriter { | 
| 19 /** | |
| 20 * The buffer on which the HTML is to be written. | |
| 21 */ | |
| 22 final StringBuffer buffer; | |
| 23 | |
| 24 /** | |
| 25 * The current level of indentation. | |
| 26 */ | |
| 27 int indentLevel = 0; | |
| 28 | |
| 29 /** | |
| 30 * A list containing the exceptions that were caught while attempting to write | |
| 31 * out an AST structure. | |
| 32 */ | |
| 33 List<CaughtException> exceptions = <CaughtException>[]; | |
| 34 | |
| 35 /** | 15 /** | 
| 36 * Initialize a newly created element writer to write the HTML representation | 16 * Initialize a newly created element writer to write the HTML representation | 
| 37 * of visited nodes on the given [buffer]. | 17 * of visited nodes on the given [buffer]. | 
| 38 */ | 18 */ | 
| 39 AstWriter(this.buffer); | 19 AstWriter(StringBuffer buffer) { | 
| 20 this.buffer = buffer; | |
| 21 } | |
| 40 | 22 | 
| 41 @override | 23 @override | 
| 42 void visitNode(AstNode node) { | 24 void visitNode(AstNode node) { | 
| 43 _writeNode(node); | 25 _writeNode(node); | 
| 26 writeProperties(_computeProperties(node)); | |
| 44 indentLevel++; | 27 indentLevel++; | 
| 45 try { | 28 try { | 
| 46 node.visitChildren(this); | 29 node.visitChildren(this); | 
| 47 } finally { | 30 } finally { | 
| 48 indentLevel--; | 31 indentLevel--; | 
| 49 } | 32 } | 
| 50 } | 33 } | 
| 51 | 34 | 
| 52 /** | 35 /** | 
| 53 * Return the name of the given [node], or `null` if the given node is not a | 36 * Return the name of the given [node], or `null` if the given node is not a | 
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 if (first) { | 85 if (first) { | 
| 103 first = false; | 86 first = false; | 
| 104 } else { | 87 } else { | 
| 105 buffer.write(', '); | 88 buffer.write(', '); | 
| 106 } | 89 } | 
| 107 buffer.write(variable.name.name); | 90 buffer.write(variable.name.name); | 
| 108 } | 91 } | 
| 109 return buffer.toString(); | 92 return buffer.toString(); | 
| 110 } | 93 } | 
| 111 | 94 | 
| 112 void _indent([int extra = 0]) { | |
| 113 for (int i = 0; i < indentLevel; i++) { | |
| 114 buffer.write('┊   '); | |
| 115 } | |
| 116 if (extra > 0) { | |
| 117 buffer.write('┊   '); | |
| 118 for (int i = 1; i < extra; i++) { | |
| 119 buffer.write('     '); | |
| 120 } | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 /** | 95 /** | 
| 125 * Write a representation of the given [node] to the buffer. | 96 * Write a representation of the given [node] to the buffer. | 
| 126 */ | 97 */ | 
| 127 void _writeNode(AstNode node) { | 98 void _writeNode(AstNode node) { | 
| 128 _indent(); | 99 indent(); | 
| 129 buffer.write(node.runtimeType); | 100 buffer.write(node.runtimeType); | 
| 130 buffer.write(' <span style="color:gray">['); | 101 buffer.write(' <span style="color:gray">['); | 
| 131 buffer.write(node.offset); | 102 buffer.write(node.offset); | 
| 132 buffer.write('..'); | 103 buffer.write('..'); | 
| 133 buffer.write(node.offset + node.length - 1); | 104 buffer.write(node.offset + node.length - 1); | 
| 134 buffer.write(']</span>'); | 105 buffer.write(']</span>'); | 
| 135 buffer.write('<br>'); | 106 buffer.write('<br>'); | 
| 136 _writeProperty('name', _getName(node)); | |
| 137 if (node is BinaryExpression) { | |
| 138 _writeProperty('static element', node.staticElement); | |
| 139 _writeProperty('static type', node.staticType); | |
| 140 _writeProperty('propagated element', node.propagatedElement); | |
| 141 _writeProperty('propagated type', node.propagatedType); | |
| 142 } else if (node is CompilationUnit) { | |
| 143 _writeProperty("element", node.element); | |
| 144 } else if (node is ExportDirective) { | |
| 145 _writeProperty("element", node.element); | |
| 146 _writeProperty("source", node.source); | |
| 147 } else if (node is FunctionExpressionInvocation) { | |
| 148 _writeProperty('static element', node.staticElement); | |
| 149 _writeProperty('static type', node.staticType); | |
| 150 _writeProperty('propagated element', node.propagatedElement); | |
| 151 _writeProperty('propagated type', node.propagatedType); | |
| 152 } else if (node is ImportDirective) { | |
| 153 _writeProperty("element", node.element); | |
| 154 _writeProperty("source", node.source); | |
| 155 } else if (node is LibraryDirective) { | |
| 156 _writeProperty("element", node.element); | |
| 157 } else if (node is PartDirective) { | |
| 158 _writeProperty("element", node.element); | |
| 159 _writeProperty("source", node.source); | |
| 160 } else if (node is PartOfDirective) { | |
| 161 _writeProperty("element", node.element); | |
| 162 } else if (node is PostfixExpression) { | |
| 163 _writeProperty('static element', node.staticElement); | |
| 164 _writeProperty('static type', node.staticType); | |
| 165 _writeProperty('propagated element', node.propagatedElement); | |
| 166 _writeProperty('propagated type', node.propagatedType); | |
| 167 } else if (node is PrefixExpression) { | |
| 168 _writeProperty('static element', node.staticElement); | |
| 169 _writeProperty('static type', node.staticType); | |
| 170 _writeProperty('propagated element', node.propagatedElement); | |
| 171 _writeProperty('propagated type', node.propagatedType); | |
| 172 } else if (node is SimpleIdentifier) { | |
| 173 _writeProperty('static element', node.staticElement); | |
| 174 _writeProperty('static type', node.staticType); | |
| 175 _writeProperty('propagated element', node.propagatedElement); | |
| 176 _writeProperty('propagated type', node.propagatedType); | |
| 177 } else if (node is SimpleStringLiteral) { | |
| 178 _writeProperty("value", node.value); | |
| 179 } else if (node is Expression) { | |
| 180 _writeProperty('static type', node.staticType); | |
| 181 _writeProperty('propagated type', node.propagatedType); | |
| 182 } | |
| 183 } | 107 } | 
| 184 | 108 | 
| 185 /** | 109 /** | 
| 186 * Write the [value] of the property with the given [name]. | 110 * Write a representation of the properties of the given [node] to the buffer. | 
| 187 */ | 111 */ | 
| 188 void _writeProperty(String name, Object value) { | 112 Map<String, Object> _computeProperties(AstNode node) { | 
| 189 if (value != null) { | 113 Map<String, Object> properties = new HashMap<String, Object>(); | 
| 190 String valueString = null; | 114 | 
| 191 try { | 115 properties['name'] = _getName(node); | 
| 192 if (value is Source) { | 116 if (node is BinaryExpression) { | 
| 193 valueString = 'Source (uri="${value.uri}", path="${value.fullName}")'; | 117 properties['static element'] =node.staticElement; | 
| 
scheglov
2015/02/16 21:20:29
Format before commit?
 
Brian Wilkerson
2015/02/16 21:41:35
Done
 | |
| 194 } else { | 118 properties['static type'] = node.staticType; | 
| 195 valueString = value.toString(); | 119 properties['propagated element'] = node.propagatedElement; | 
| 196 } | 120 properties['propagated type'] = node.propagatedType; | 
| 197 } catch (exception, stackTrace) { | 121 } else if (node is CompilationUnit) { | 
| 198 exceptions.add(new CaughtException(exception, stackTrace)); | 122 properties['element'] = node.element; | 
| 199 } | 123 } else if (node is ExportDirective) { | 
| 200 _indent(2); | 124 properties['element'] = node.element; | 
| 201 buffer.write('$name = '); | 125 properties['source'] = node.source; | 
| 202 if (valueString == null) { | 126 } else if (node is FunctionExpressionInvocation) { | 
| 203 buffer.write('<span style="color: #FF0000">'); | 127 properties['static element'] = node.staticElement; | 
| 204 buffer.write(HTML_ESCAPE.convert(value.runtimeType.toString())); | 128 properties['static type'] = node.staticType; | 
| 205 buffer.write('</span>'); | 129 properties['propagated element'] = node.propagatedElement; | 
| 206 } else { | 130 properties['propagated type'] = node.propagatedType; | 
| 207 buffer.write(HTML_ESCAPE.convert(valueString)); | 131 } else if (node is ImportDirective) { | 
| 208 if (value is Element && value is! LibraryElement) { | 132 properties['element'] = node.element; | 
| 209 String name = value.name; | 133 properties['source'] = node.source; | 
| 210 if (name != null) { | 134 } else if (node is LibraryDirective) { | 
| 211 buffer.write('  ['); | 135 properties['element'] = node.element; | 
| 212 buffer.write(GetHandler.makeLink(GetHandler.INDEX_ELEMENT_BY_NAME, { | 136 } else if (node is PartDirective) { | 
| 213 'name': name | 137 properties['element'] = node.element; | 
| 214 }, 'search index')); | 138 properties['source'] = node.source; | 
| 215 buffer.write(']'); | 139 } else if (node is PartOfDirective) { | 
| 216 } | 140 properties['element'] = node.element; | 
| 217 } | 141 } else if (node is PostfixExpression) { | 
| 218 } | 142 properties['static element'] = node.staticElement; | 
| 219 buffer.write('<br>'); | 143 properties['static type'] = node.staticType; | 
| 144 properties['propagated element'] = node.propagatedElement; | |
| 145 properties['propagated type'] = node.propagatedType; | |
| 146 } else if (node is PrefixExpression) { | |
| 147 properties['static element'] = node.staticElement; | |
| 148 properties['static type'] = node.staticType; | |
| 149 properties['propagated element'] = node.propagatedElement; | |
| 150 properties['propagated type'] = node.propagatedType; | |
| 151 } else if (node is SimpleIdentifier) { | |
| 152 properties['static element'] = node.staticElement; | |
| 153 properties['static type'] = node.staticType; | |
| 154 properties['propagated element'] = node.propagatedElement; | |
| 155 properties['propagated type'] = node.propagatedType; | |
| 156 } else if (node is SimpleStringLiteral) { | |
| 157 properties['value'] = node.value; | |
| 158 } else if (node is Expression) { | |
| 159 properties['static type'] = node.staticType; | |
| 160 properties['propagated type'] = node.propagatedType; | |
| 220 } | 161 } | 
| 162 | |
| 163 return properties; | |
| 221 } | 164 } | 
| 222 } | 165 } | 
| OLD | NEW |