OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 computer.hover; | 5 library computer.hover; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analysis_server/src/constants.dart'; | 9 import 'package:analysis_server/src/constants.dart'; |
10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 final int _offset; | 64 final int _offset; |
65 | 65 |
66 DartUnitHoverComputer(this._unit, this._offset); | 66 DartUnitHoverComputer(this._unit, this._offset); |
67 | 67 |
68 /** | 68 /** |
69 * Returns the computed hover, maybe `null`. | 69 * Returns the computed hover, maybe `null`. |
70 */ | 70 */ |
71 Map<String, Object> compute() { | 71 Map<String, Object> compute() { |
72 AstNode node = new NodeLocator.con1(_offset).searchWithin(_unit); | 72 AstNode node = new NodeLocator.con1(_offset).searchWithin(_unit); |
73 if (node is Expression) { | 73 if (node is Expression) { |
74 Hover hover = new Hover(); | 74 Hover hover = new Hover(node.offset, node.length); |
75 // element | 75 // element |
76 Element element = ElementLocator.locateWithOffset(node, _offset); | 76 Element element = ElementLocator.locateWithOffset(node, _offset); |
77 if (element != null) { | 77 if (element != null) { |
78 // variable, if synthetic accessor | 78 // variable, if synthetic accessor |
79 if (element is PropertyAccessorElement) { | 79 if (element is PropertyAccessorElement) { |
80 PropertyAccessorElement accessor = element; | 80 PropertyAccessorElement accessor = element; |
81 if (accessor.isSynthetic) { | 81 if (accessor.isSynthetic) { |
82 element = accessor.variable; | 82 element = accessor.variable; |
83 } | 83 } |
84 } | 84 } |
85 // description | 85 // description |
86 hover.elementDescription = element.toString(); | 86 hover.elementDescription = element.toString(); |
| 87 hover.elementKind = element.kind.displayName; |
87 // library | 88 // library |
88 LibraryElement library = element.library; | 89 LibraryElement library = element.library; |
89 if (library != null) { | 90 if (library != null) { |
90 hover.containingLibraryName = library.name; | 91 hover.containingLibraryName = library.name; |
91 hover.containingLibraryPath = library.source.fullName; | 92 hover.containingLibraryPath = library.source.fullName; |
92 } | 93 } |
93 // documentation | 94 // documentation |
94 String dartDoc = element.computeDocumentationComment(); | 95 String dartDoc = element.computeDocumentationComment(); |
95 dartDoc = _removeDartDocDelimiters(dartDoc); | 96 dartDoc = _removeDartDocDelimiters(dartDoc); |
96 hover.dartDoc = dartDoc; | 97 hover.dartDoc = dartDoc; |
97 } | 98 } |
98 // parameter | 99 // parameter |
99 hover.parameter = _safeToString(node.bestParameterElement); | 100 hover.parameter = _safeToString(node.bestParameterElement); |
100 // types | 101 // types |
101 hover.staticType = _safeToString(node.staticType); | 102 hover.staticType = _safeToString(node.staticType); |
102 hover.propagatedType = _safeToString(node.propagatedType); | 103 hover.propagatedType = _safeToString(node.propagatedType); |
103 // done | 104 // done |
104 return hover.toJson(); | 105 return hover.toJson(); |
105 } | 106 } |
106 // not an expression | 107 // not an expression |
107 return null; | 108 return null; |
108 } | 109 } |
109 | 110 |
110 static _safeToString(obj) => obj != null ? obj.toString() : null; | 111 static _safeToString(obj) => obj != null ? obj.toString() : null; |
111 } | 112 } |
112 | 113 |
113 | 114 |
114 class Hover { | 115 class Hover { |
| 116 final int offset; |
| 117 final int length; |
115 String containingLibraryName; | 118 String containingLibraryName; |
116 String containingLibraryPath; | 119 String containingLibraryPath; |
117 String dartDoc; | 120 String dartDoc; |
118 String elementDescription; | 121 String elementDescription; |
| 122 String elementKind; |
119 String parameter; | 123 String parameter; |
120 String propagatedType; | 124 String propagatedType; |
121 String staticType; | 125 String staticType; |
122 | 126 |
123 Hover(); | 127 Hover(this.offset, this.length); |
124 | 128 |
125 factory Hover.fromJson(Map<String, Object> map) { | 129 factory Hover.fromJson(Map<String, Object> map) { |
126 Hover hover = new Hover(); | 130 int offset = map[OFFSET]; |
| 131 int length = map[LENGTH]; |
| 132 Hover hover = new Hover(offset, length); |
127 hover.containingLibraryName = map[CONTAINING_LIBRARY_NAME]; | 133 hover.containingLibraryName = map[CONTAINING_LIBRARY_NAME]; |
128 hover.containingLibraryPath = map[CONTAINING_LIBRARY_PATH]; | 134 hover.containingLibraryPath = map[CONTAINING_LIBRARY_PATH]; |
129 hover.dartDoc = map[DART_DOC]; | 135 hover.dartDoc = map[DART_DOC]; |
130 hover.elementDescription = map[ELEMENT_DESCRIPTION]; | 136 hover.elementDescription = map[ELEMENT_DESCRIPTION]; |
| 137 hover.elementKind = map[ELEMENT_KIND]; |
131 hover.parameter = map[PARAMETER]; | 138 hover.parameter = map[PARAMETER]; |
132 hover.propagatedType = map[PROPAGATED_TYPE]; | 139 hover.propagatedType = map[PROPAGATED_TYPE]; |
133 hover.staticType = map[STATIC_TYPE]; | 140 hover.staticType = map[STATIC_TYPE]; |
134 return hover; | 141 return hover; |
135 } | 142 } |
136 | 143 |
137 Map<String, Object> toJson() { | 144 Map<String, Object> toJson() { |
138 Map<String, Object> json = new HashMap<String, Object>(); | 145 Map<String, Object> json = new HashMap<String, Object>(); |
| 146 json[OFFSET] = offset; |
| 147 json[LENGTH] = length; |
139 if (containingLibraryName != null) { | 148 if (containingLibraryName != null) { |
140 json[CONTAINING_LIBRARY_NAME] = containingLibraryName; | 149 json[CONTAINING_LIBRARY_NAME] = containingLibraryName; |
141 } | 150 } |
142 if (containingLibraryName != null) { | 151 if (containingLibraryName != null) { |
143 json[CONTAINING_LIBRARY_PATH] = containingLibraryPath; | 152 json[CONTAINING_LIBRARY_PATH] = containingLibraryPath; |
144 } | 153 } |
145 if (dartDoc != null) { | 154 if (dartDoc != null) { |
146 json[DART_DOC] = dartDoc; | 155 json[DART_DOC] = dartDoc; |
147 } | 156 } |
148 if (elementDescription != null) { | 157 if (elementDescription != null) { |
149 json[ELEMENT_DESCRIPTION] = elementDescription; | 158 json[ELEMENT_DESCRIPTION] = elementDescription; |
150 } | 159 } |
| 160 if (elementKind != null) { |
| 161 json[ELEMENT_KIND] = elementKind; |
| 162 } |
151 if (parameter != null) { | 163 if (parameter != null) { |
152 json[PARAMETER] = parameter; | 164 json[PARAMETER] = parameter; |
153 } | 165 } |
154 if (propagatedType != null) { | 166 if (propagatedType != null) { |
155 json[PROPAGATED_TYPE] = propagatedType; | 167 json[PROPAGATED_TYPE] = propagatedType; |
156 } | 168 } |
157 if (staticType != null) { | 169 if (staticType != null) { |
158 json[STATIC_TYPE] = staticType; | 170 json[STATIC_TYPE] = staticType; |
159 } | 171 } |
160 return json; | 172 return json; |
161 } | 173 } |
162 } | 174 } |
OLD | NEW |