| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library computer.hover; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analysis_server/src/constants.dart'; |
| 10 import 'package:analyzer/src/generated/ast.dart'; |
| 11 import 'package:analyzer/src/generated/element.dart'; |
| 12 |
| 13 |
| 14 /** |
| 15 * A computer for the hover at the specified offset of a Dart [CompilationUnit]. |
| 16 */ |
| 17 class DartUnitHoverComputer { |
| 18 final CompilationUnit _unit; |
| 19 final int _offset; |
| 20 |
| 21 DartUnitHoverComputer(this._unit, this._offset); |
| 22 |
| 23 /** |
| 24 * Returns the computed hover, maybe `null`. |
| 25 */ |
| 26 Map<String, Object> compute() { |
| 27 AstNode node = new NodeLocator.con1(_offset).searchWithin(_unit); |
| 28 if (node is Expression) { |
| 29 Hover hover = new Hover(); |
| 30 // element |
| 31 Element element = ElementLocator.locateWithOffset(node, _offset); |
| 32 if (element != null) { |
| 33 // variable, if synthetic accessor |
| 34 if (element is PropertyAccessorElement) { |
| 35 PropertyAccessorElement accessor = element; |
| 36 if (accessor.isSynthetic) { |
| 37 element = accessor.variable; |
| 38 } |
| 39 } |
| 40 // description |
| 41 hover.elementDescription = element.toString(); |
| 42 // library |
| 43 LibraryElement library = element.library; |
| 44 if (library != null) { |
| 45 hover.containingLibraryName = library.name; |
| 46 hover.containingLibraryPath = library.source.fullName; |
| 47 } |
| 48 // documentation |
| 49 hover.dartDoc = element.computeDocumentationComment(); |
| 50 } |
| 51 // parameter |
| 52 hover.parameter = _safeToString(node.bestParameterElement); |
| 53 // types |
| 54 hover.staticType = _safeToString(node.staticType); |
| 55 hover.propagatedType = _safeToString(node.propagatedType); |
| 56 // done |
| 57 return hover.toJson(); |
| 58 } |
| 59 // not an expression |
| 60 return null; |
| 61 } |
| 62 |
| 63 static _safeToString(obj) => obj != null ? obj.toString() : null; |
| 64 } |
| 65 |
| 66 |
| 67 class Hover { |
| 68 String containingLibraryName; |
| 69 String containingLibraryPath; |
| 70 String dartDoc; |
| 71 String elementDescription; |
| 72 String parameter; |
| 73 String propagatedType; |
| 74 String staticType; |
| 75 |
| 76 Hover(); |
| 77 |
| 78 factory Hover.fromJson(Map<String, Object> map) { |
| 79 Hover hover = new Hover(); |
| 80 hover.containingLibraryName = map[CONTAINING_LIBRARY_NAME]; |
| 81 hover.containingLibraryPath = map[CONTAINING_LIBRARY_PATH]; |
| 82 hover.dartDoc = map[DART_DOC]; |
| 83 hover.elementDescription = map[ELEMENT_DESCRIPTION]; |
| 84 hover.parameter = map[PARAMETER]; |
| 85 hover.propagatedType = map[PROPAGATED_TYPE]; |
| 86 hover.staticType = map[STATIC_TYPE]; |
| 87 return hover; |
| 88 } |
| 89 |
| 90 Map<String, Object> toJson() { |
| 91 Map<String, Object> json = new HashMap<String, Object>(); |
| 92 if (containingLibraryName != null) { |
| 93 json[CONTAINING_LIBRARY_NAME] = containingLibraryName; |
| 94 } |
| 95 if (containingLibraryName != null) { |
| 96 json[CONTAINING_LIBRARY_PATH] = containingLibraryPath; |
| 97 } |
| 98 if (dartDoc != null) { |
| 99 json[DART_DOC] = dartDoc; |
| 100 } |
| 101 if (elementDescription != null) { |
| 102 json[ELEMENT_DESCRIPTION] = elementDescription; |
| 103 } |
| 104 if (parameter != null) { |
| 105 json[PARAMETER] = parameter; |
| 106 } |
| 107 if (propagatedType != null) { |
| 108 json[PROPAGATED_TYPE] = propagatedType; |
| 109 } |
| 110 if (staticType != null) { |
| 111 json[STATIC_TYPE] = staticType; |
| 112 } |
| 113 return json; |
| 114 } |
| 115 } |
| OLD | NEW |