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