| OLD | NEW |
| (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 src.services.index; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 | |
| 9 import 'package:analysis_server/src/provisional/index/index_core.dart'; | |
| 10 import 'package:analyzer/dart/element/element.dart'; | |
| 11 import 'package:analyzer/src/generated/engine.dart'; | |
| 12 import 'package:analyzer/src/generated/source.dart'; | |
| 13 import 'package:analyzer/src/generated/utilities_general.dart'; | |
| 14 | |
| 15 /** | |
| 16 * A wrapper around an [Element] that implements the [IndexableObject] interface
. | |
| 17 */ | |
| 18 class IndexableElement implements IndexableObject { | |
| 19 /** | |
| 20 * The element being wrapped. | |
| 21 */ | |
| 22 final Element element; | |
| 23 | |
| 24 /** | |
| 25 * Initialize a newly created wrapper to wrap the given [element]. | |
| 26 */ | |
| 27 IndexableElement(this.element) { | |
| 28 if (element == null) { | |
| 29 throw new ArgumentError.notNull('element'); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 @override | |
| 34 String get filePath { | |
| 35 return element.source?.fullName; | |
| 36 } | |
| 37 | |
| 38 @override | |
| 39 int get hashCode => element.hashCode; | |
| 40 | |
| 41 @override | |
| 42 IndexableElementKind get kind => IndexableElementKind.forElement(element); | |
| 43 | |
| 44 @override | |
| 45 int get offset { | |
| 46 if (element is ConstructorElement) { | |
| 47 return element.enclosingElement.nameOffset; | |
| 48 } | |
| 49 return element.nameOffset; | |
| 50 } | |
| 51 | |
| 52 @override | |
| 53 bool operator ==(Object object) => | |
| 54 object is IndexableElement && element == object.element; | |
| 55 | |
| 56 @override | |
| 57 String toString() => element.toString(); | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * The kind associated with an [IndexableElement]. | |
| 62 */ | |
| 63 class IndexableElementKind implements IndexableObjectKind<IndexableElement> { | |
| 64 /** | |
| 65 * A table mapping element kinds to the corresponding indexable element kind. | |
| 66 */ | |
| 67 static final Map<ElementKind, IndexableElementKind> _kindMap = | |
| 68 new HashMap<ElementKind, IndexableElementKind>(); | |
| 69 | |
| 70 /** | |
| 71 * A table mapping the index of a constructor (in the lexically-ordered list | |
| 72 * of constructors associated with a class) to the indexable element kind used | |
| 73 * to represent it. | |
| 74 */ | |
| 75 static final Map<int, IndexableElementKind> _constructorKinds = | |
| 76 new HashMap<int, IndexableElementKind>(); | |
| 77 | |
| 78 @override | |
| 79 final int index = IndexableObjectKind.nextIndex; | |
| 80 | |
| 81 /** | |
| 82 * The element kind represented by this index element kind. | |
| 83 */ | |
| 84 final ElementKind elementKind; | |
| 85 | |
| 86 /** | |
| 87 * Initialize a newly created kind to have the given [index] and be associated | |
| 88 * with the given [elementKind]. | |
| 89 */ | |
| 90 IndexableElementKind._(this.elementKind) { | |
| 91 IndexableObjectKind.register(this); | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Return the index of the constructor with this indexable element kind. | |
| 96 */ | |
| 97 int get constructorIndex { | |
| 98 for (int index in _constructorKinds.keys) { | |
| 99 if (_constructorKinds[index] == this) { | |
| 100 return index; | |
| 101 } | |
| 102 } | |
| 103 return -1; | |
| 104 } | |
| 105 | |
| 106 @override | |
| 107 IndexableElement decode( | |
| 108 AnalysisContext context, String filePath, int offset) { | |
| 109 List<Source> unitSources = context.getSourcesWithFullName(filePath); | |
| 110 for (Source unitSource in unitSources) { | |
| 111 List<Source> libSources = context.getLibrariesContaining(unitSource); | |
| 112 for (Source libSource in libSources) { | |
| 113 CompilationUnitElement unitElement = | |
| 114 context.getCompilationUnitElement(unitSource, libSource); | |
| 115 if (unitElement == null) { | |
| 116 return null; | |
| 117 } | |
| 118 if (elementKind == ElementKind.LIBRARY) { | |
| 119 return new IndexableElement(unitElement.library); | |
| 120 } else if (elementKind == ElementKind.COMPILATION_UNIT) { | |
| 121 return new IndexableElement(unitElement); | |
| 122 } else { | |
| 123 Element element = unitElement.getElementAt(offset); | |
| 124 if (element == null) { | |
| 125 return null; | |
| 126 } | |
| 127 if (element is ClassElement && | |
| 128 elementKind == ElementKind.CONSTRUCTOR) { | |
| 129 return new IndexableElement(element.constructors[constructorIndex]); | |
| 130 } | |
| 131 if (element is PropertyInducingElement) { | |
| 132 if (elementKind == ElementKind.GETTER) { | |
| 133 return new IndexableElement(element.getter); | |
| 134 } | |
| 135 if (elementKind == ElementKind.SETTER) { | |
| 136 return new IndexableElement(element.setter); | |
| 137 } | |
| 138 } | |
| 139 return new IndexableElement(element); | |
| 140 } | |
| 141 } | |
| 142 } | |
| 143 return null; | |
| 144 } | |
| 145 | |
| 146 @override | |
| 147 int encodeHash(StringToInt stringToInt, IndexableElement indexable) { | |
| 148 Element element = indexable.element; | |
| 149 String elementName = element.displayName; | |
| 150 int elementNameId = stringToInt(elementName); | |
| 151 LibraryElement libraryElement = element.library; | |
| 152 if (libraryElement != null) { | |
| 153 String libraryPath = libraryElement.source.fullName; | |
| 154 int libraryPathId = stringToInt(libraryPath); | |
| 155 return JenkinsSmiHash.combine(libraryPathId, elementNameId); | |
| 156 } | |
| 157 return elementNameId; | |
| 158 } | |
| 159 | |
| 160 /** | |
| 161 * Return the indexable element kind representing the given [element]. | |
| 162 */ | |
| 163 static IndexableElementKind forElement(Element element) { | |
| 164 if (element is ConstructorElement) { | |
| 165 ClassElement classElement = element.enclosingElement; | |
| 166 int constructorIndex = classElement.constructors.indexOf(element); | |
| 167 return _constructorKinds.putIfAbsent(constructorIndex, | |
| 168 () => new IndexableElementKind._(ElementKind.CONSTRUCTOR)); | |
| 169 } | |
| 170 ElementKind elementKind = element.kind; | |
| 171 return _kindMap.putIfAbsent( | |
| 172 elementKind, () => new IndexableElementKind._(elementKind)); | |
| 173 } | |
| 174 } | |
| OLD | NEW |