| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 src.services.index; | 5 library src.services.index; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analysis_server/analysis/index_core.dart'; | 9 import 'package:analysis_server/analysis/index_core.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 13 | 14 |
| 14 /** | 15 /** |
| 15 * A wrapper around an [Element] that implements the [IndexableObject] interface
. | 16 * A wrapper around an [Element] that implements the [IndexableObject] interface
. |
| 16 */ | 17 */ |
| 17 class IndexableElement implements IndexableObject { | 18 class IndexableElement implements IndexableObject { |
| 18 /** | 19 /** |
| 19 * The element being wrapped. | 20 * The element being wrapped. |
| 20 */ | 21 */ |
| 21 final Element element; | 22 final Element element; |
| 22 | 23 |
| 23 /** | 24 /** |
| 24 * Initialize a newly created wrapper to wrap the given [element]. | 25 * Initialize a newly created wrapper to wrap the given [element]. |
| 25 */ | 26 */ |
| 26 IndexableElement(this.element) { | 27 IndexableElement(this.element) { |
| 27 if (element == null) { | 28 if (element == null) { |
| 28 throw new ArgumentError.notNull('element'); | 29 throw new ArgumentError.notNull('element'); |
| 29 } | 30 } |
| 30 } | 31 } |
| 31 | 32 |
| 32 @override | 33 @override |
| 33 int get hashCode => element.hashCode; | 34 int get hashCode => element.hashCode; |
| 34 | 35 |
| 35 @override | 36 @override |
| 36 IndexableObjectKind get kind => IndexableElementKind.forElement(element); | 37 IndexableObjectKind get kind => IndexableElementKind.forElement(element); |
| 37 | 38 |
| 38 @override | 39 @override |
| 39 int get length => element.nameLength; | |
| 40 | |
| 41 @override | |
| 42 String get name => element.displayName; | |
| 43 | |
| 44 @override | |
| 45 int get offset { | 40 int get offset { |
| 46 if (element is ConstructorElement) { | 41 if (element is ConstructorElement) { |
| 47 return element.enclosingElement.nameOffset; | 42 return element.enclosingElement.nameOffset; |
| 48 } | 43 } |
| 49 return element.nameOffset; | 44 return element.nameOffset; |
| 50 } | 45 } |
| 51 | 46 |
| 52 @override | 47 @override |
| 53 Source get source => element.source; | 48 Source get source => element.source; |
| 54 | 49 |
| 55 @override | 50 @override |
| 56 bool operator ==(Object object) => | 51 bool operator ==(Object object) => |
| 57 object is IndexableElement && element == object.element; | 52 object is IndexableElement && element == object.element; |
| 58 | 53 |
| 59 @override | 54 @override |
| 60 String toString() => element.toString(); | 55 String toString() => element.toString(); |
| 61 } | 56 } |
| 62 | 57 |
| 63 /** | 58 /** |
| 64 * The kind associated with an [IndexableElement]. | 59 * The kind associated with an [IndexableElement]. |
| 65 */ | 60 */ |
| 66 class IndexableElementKind implements IndexableObjectKind { | 61 class IndexableElementKind implements IndexableObjectKind { |
| 67 /** | 62 /** |
| 68 * A table mapping element kinds to the corresponding indexable element kind. | 63 * A table mapping element kinds to the corresponding indexable element kind. |
| 69 */ | 64 */ |
| 70 static Map<ElementKind, IndexableElementKind> _kindMap = | 65 static final Map<ElementKind, IndexableElementKind> _kindMap = |
| 71 new HashMap<ElementKind, IndexableElementKind>(); | 66 new HashMap<ElementKind, IndexableElementKind>(); |
| 72 | 67 |
| 73 /** | 68 /** |
| 74 * A table mapping the index of a constructor (in the lexically-ordered list | 69 * A table mapping the index of a constructor (in the lexically-ordered list |
| 75 * of constructors associated with a class) to the indexable element kind used | 70 * of constructors associated with a class) to the indexable element kind used |
| 76 * to represent it. | 71 * to represent it. |
| 77 */ | 72 */ |
| 78 static Map<int, IndexableElementKind> _constructorKinds = | 73 static final Map<int, IndexableElementKind> _constructorKinds = |
| 79 new HashMap<int, IndexableElementKind>(); | 74 new HashMap<int, IndexableElementKind>(); |
| 80 | 75 |
| 81 @override | 76 @override |
| 82 final int index = IndexableObjectKind.nextIndex; | 77 final int index = IndexableObjectKind.nextIndex; |
| 83 | 78 |
| 84 /** | 79 /** |
| 85 * The element kind represented by this index element kind. | 80 * The element kind represented by this index element kind. |
| 86 */ | 81 */ |
| 87 final ElementKind elementKind; | 82 final ElementKind elementKind; |
| 88 | 83 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 return new IndexableElement(element.setter); | 133 return new IndexableElement(element.setter); |
| 139 } | 134 } |
| 140 } | 135 } |
| 141 return new IndexableElement(element); | 136 return new IndexableElement(element); |
| 142 } | 137 } |
| 143 } | 138 } |
| 144 } | 139 } |
| 145 return null; | 140 return null; |
| 146 } | 141 } |
| 147 | 142 |
| 143 @override |
| 144 int encodeHash(StringToInt stringToInt, IndexableObject indexable) { |
| 145 Element element = (indexable as IndexableElement).element; |
| 146 String elementName = element.displayName; |
| 147 int elementNameId = stringToInt(elementName); |
| 148 if (indexable is IndexableElement) { |
| 149 LibraryElement libraryElement = indexable.element.library; |
| 150 if (libraryElement != null) { |
| 151 String libraryPath = libraryElement.source.fullName; |
| 152 int libraryPathId = stringToInt(libraryPath); |
| 153 return JenkinsSmiHash.combine(libraryPathId, elementNameId); |
| 154 } |
| 155 } |
| 156 return elementNameId; |
| 157 } |
| 158 |
| 148 /** | 159 /** |
| 149 * Return the indexable element kind representing the given [element]. | 160 * Return the indexable element kind representing the given [element]. |
| 150 */ | 161 */ |
| 151 static IndexableElementKind forElement(Element element) { | 162 static IndexableElementKind forElement(Element element) { |
| 152 if (element is ConstructorElement) { | 163 if (element is ConstructorElement) { |
| 153 ClassElement classElement = element.enclosingElement; | 164 ClassElement classElement = element.enclosingElement; |
| 154 int constructorIndex = classElement.constructors.indexOf(element); | 165 int constructorIndex = classElement.constructors.indexOf(element); |
| 155 return _constructorKinds.putIfAbsent(constructorIndex, | 166 return _constructorKinds.putIfAbsent(constructorIndex, |
| 156 () => new IndexableElementKind._(ElementKind.CONSTRUCTOR)); | 167 () => new IndexableElementKind._(ElementKind.CONSTRUCTOR)); |
| 157 } | 168 } |
| 158 ElementKind elementKind = element.kind; | 169 ElementKind elementKind = element.kind; |
| 159 return _kindMap.putIfAbsent( | 170 return _kindMap.putIfAbsent( |
| 160 elementKind, () => new IndexableElementKind._(elementKind)); | 171 elementKind, () => new IndexableElementKind._(elementKind)); |
| 161 } | 172 } |
| 162 } | 173 } |
| OLD | NEW |