Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: pkg/analysis_server/lib/src/status/tree_writer.dart

Issue 934603003: Add properties to the element tree (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add missed file Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 'dart:convert';
8
9 import 'package:analysis_server/src/get_handler.dart';
10 import 'package:analyzer/src/generated/element.dart';
11 import 'package:analyzer/src/generated/java_engine.dart';
12 import 'package:analyzer/src/generated/source.dart';
13 import 'package:analyzer/src/generated/constant.dart';
14
15 /**
16 * Utility methods that can be mixed in to classes that produce an HTML
17 * representation of a tree structure.
18 */
19 abstract class TreeWriter {
20 /**
21 * The buffer on which the HTML is to be written.
22 */
23 StringBuffer buffer;
24
25 /**
26 * The current level of indentation.
27 */
28 int indentLevel = 0;
29
30 /**
31 * A list containing the exceptions that were caught while attempting to write
32 * out the tree structure.
33 */
34 List<CaughtException> exceptions = <CaughtException>[];
35
36 void indent([int extra = 0]) {
37 for (int i = 0; i < indentLevel; i++) {
38 buffer.write('&#x250A;&nbsp;&nbsp;&nbsp;');
39 }
40 if (extra > 0) {
41 buffer.write('&#x250A;&nbsp;&nbsp;&nbsp;');
42 for (int i = 1; i < extra; i++) {
43 buffer.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
44 }
45 }
46 }
47
48 /**
49 * Write a representation of the given [properties] to the buffer.
50 */
51 void writeProperties(Map<String, Object> properties) {
52 List<String> propertyNames = properties.keys.toList();
53 propertyNames.sort();
54 for (String propertyName in propertyNames) {
55 writeProperty(propertyName, properties[propertyName]);
56 }
57 }
58
59 /**
60 * Write the [value] of the property with the given [name].
61 */
62 void writeProperty(String name, Object value) {
63 if (value != null) {
64 indent(2);
65 buffer.write('$name = ');
66 _writePropertyValue(value, indentLevel);
67 buffer.write('<br>');
68 }
69 }
70
71 /**
72 * Write the [value] of the property with the given [name].
73 */
74 void _writePropertyValue(Object value, int baseIndent) {
75 if (value is List) {
76 if (value.isEmpty) {
77 buffer.write('[]');
78 } else {
79 int elementIndent = baseIndent + 2;
80 buffer.write('[<br>');
81 for (Object element in value) {
82 indent(elementIndent);
83 _writePropertyValue(element, elementIndent);
84 buffer.write('<br>');
85 }
86 indent(baseIndent);
87 buffer.write(']');
88 }
89 } else {
90 String valueString = _toString(value);
91 if (valueString == null) {
92 buffer.write('<span style="color: #FF0000">');
93 buffer.write(HTML_ESCAPE.convert(value.runtimeType.toString()));
94 buffer.write('</span>');
95 } else {
96 buffer.write(HTML_ESCAPE.convert(valueString));
97 if (value is Element && value is! LibraryElement) {
98 String name = value.name;
99 if (name != null) {
100 buffer.write('&nbsp;&nbsp;[');
101 buffer.write(GetHandler.makeLink(GetHandler.INDEX_ELEMENT_BY_NAME, {
102 'name': name
103 }, 'search index'));
104 buffer.write(']');
105 }
106 }
107 }
108 }
109 }
110
111 String _toString(Object value) {
112 try {
113 if (value is Source) {
114 return 'Source (uri="${value.uri}", path="${value.fullName}")';
115 } else if (value is ElementAnnotationImpl) {
116 StringBuffer buffer = new StringBuffer();
117 buffer.write(_toString(value.element));
118 EvaluationResultImpl result = value.evaluationResult;
119 if (result == null) {
120 buffer.write(': no result');
121 } else {
122 buffer.write(': value = ');
123 buffer.write(result.value);
124 buffer.write('; errors = ');
125 buffer.write(result.errors);
126 }
127 return buffer.toString();
128 } else {
129 return value.toString();
130 }
131 } catch (exception, stackTrace) {
132 exceptions.add(new CaughtException(exception, stackTrace));
133 }
134 return null;
135 }
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698