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/plugin/index/index_core.dart'; | 9 import 'package:analysis_server/plugin/index/index_core.dart'; |
10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
(...skipping 13 matching lines...) Expand all Loading... |
24 /** | 24 /** |
25 * Initialize a newly created wrapper to wrap the given [element]. | 25 * Initialize a newly created wrapper to wrap the given [element]. |
26 */ | 26 */ |
27 IndexableElement(this.element) { | 27 IndexableElement(this.element) { |
28 if (element == null) { | 28 if (element == null) { |
29 throw new ArgumentError.notNull('element'); | 29 throw new ArgumentError.notNull('element'); |
30 } | 30 } |
31 } | 31 } |
32 | 32 |
33 @override | 33 @override |
| 34 String get filePath { |
| 35 return element.source?.fullName; |
| 36 } |
| 37 |
| 38 @override |
34 int get hashCode => element.hashCode; | 39 int get hashCode => element.hashCode; |
35 | 40 |
36 @override | 41 @override |
37 IndexableObjectKind get kind => IndexableElementKind.forElement(element); | 42 IndexableElementKind get kind => IndexableElementKind.forElement(element); |
38 | 43 |
39 @override | 44 @override |
40 int get offset { | 45 int get offset { |
41 if (element is ConstructorElement) { | 46 if (element is ConstructorElement) { |
42 return element.enclosingElement.nameOffset; | 47 return element.enclosingElement.nameOffset; |
43 } | 48 } |
44 return element.nameOffset; | 49 return element.nameOffset; |
45 } | 50 } |
46 | 51 |
47 @override | 52 @override |
48 String get filePath { | |
49 return element.source?.fullName; | |
50 } | |
51 | |
52 @override | |
53 bool operator ==(Object object) => | 53 bool operator ==(Object object) => |
54 object is IndexableElement && element == object.element; | 54 object is IndexableElement && element == object.element; |
55 | 55 |
56 @override | 56 @override |
57 String toString() => element.toString(); | 57 String toString() => element.toString(); |
58 } | 58 } |
59 | 59 |
60 /** | 60 /** |
61 * The kind associated with an [IndexableElement]. | 61 * The kind associated with an [IndexableElement]. |
62 */ | 62 */ |
63 class IndexableElementKind implements IndexableObjectKind { | 63 class IndexableElementKind implements IndexableObjectKind<IndexableElement> { |
64 /** | 64 /** |
65 * A table mapping element kinds to the corresponding indexable element kind. | 65 * A table mapping element kinds to the corresponding indexable element kind. |
66 */ | 66 */ |
67 static final Map<ElementKind, IndexableElementKind> _kindMap = | 67 static final Map<ElementKind, IndexableElementKind> _kindMap = |
68 new HashMap<ElementKind, IndexableElementKind>(); | 68 new HashMap<ElementKind, IndexableElementKind>(); |
69 | 69 |
70 /** | 70 /** |
71 * A table mapping the index of a constructor (in the lexically-ordered list | 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 | 72 * of constructors associated with a class) to the indexable element kind used |
73 * to represent it. | 73 * to represent it. |
(...skipping 23 matching lines...) Expand all Loading... |
97 int get constructorIndex { | 97 int get constructorIndex { |
98 for (int index in _constructorKinds.keys) { | 98 for (int index in _constructorKinds.keys) { |
99 if (_constructorKinds[index] == this) { | 99 if (_constructorKinds[index] == this) { |
100 return index; | 100 return index; |
101 } | 101 } |
102 } | 102 } |
103 return -1; | 103 return -1; |
104 } | 104 } |
105 | 105 |
106 @override | 106 @override |
107 IndexableObject decode(AnalysisContext context, String filePath, int offset) { | 107 IndexableElement decode( |
| 108 AnalysisContext context, String filePath, int offset) { |
108 List<Source> unitSources = context.getSourcesWithFullName(filePath); | 109 List<Source> unitSources = context.getSourcesWithFullName(filePath); |
109 for (Source unitSource in unitSources) { | 110 for (Source unitSource in unitSources) { |
110 List<Source> libSources = context.getLibrariesContaining(unitSource); | 111 List<Source> libSources = context.getLibrariesContaining(unitSource); |
111 for (Source libSource in libSources) { | 112 for (Source libSource in libSources) { |
112 CompilationUnitElement unitElement = | 113 CompilationUnitElement unitElement = |
113 context.getCompilationUnitElement(unitSource, libSource); | 114 context.getCompilationUnitElement(unitSource, libSource); |
114 if (unitElement == null) { | 115 if (unitElement == null) { |
115 return null; | 116 return null; |
116 } | 117 } |
117 if (elementKind == ElementKind.LIBRARY) { | 118 if (elementKind == ElementKind.LIBRARY) { |
(...skipping 18 matching lines...) Expand all Loading... |
136 } | 137 } |
137 } | 138 } |
138 return new IndexableElement(element); | 139 return new IndexableElement(element); |
139 } | 140 } |
140 } | 141 } |
141 } | 142 } |
142 return null; | 143 return null; |
143 } | 144 } |
144 | 145 |
145 @override | 146 @override |
146 int encodeHash(StringToInt stringToInt, IndexableObject indexable) { | 147 int encodeHash(StringToInt stringToInt, IndexableElement indexable) { |
147 Element element = (indexable as IndexableElement).element; | 148 Element element = indexable.element; |
148 String elementName = element.displayName; | 149 String elementName = element.displayName; |
149 int elementNameId = stringToInt(elementName); | 150 int elementNameId = stringToInt(elementName); |
150 if (indexable is IndexableElement) { | 151 LibraryElement libraryElement = element.library; |
151 LibraryElement libraryElement = indexable.element.library; | 152 if (libraryElement != null) { |
152 if (libraryElement != null) { | 153 String libraryPath = libraryElement.source.fullName; |
153 String libraryPath = libraryElement.source.fullName; | 154 int libraryPathId = stringToInt(libraryPath); |
154 int libraryPathId = stringToInt(libraryPath); | 155 return JenkinsSmiHash.combine(libraryPathId, elementNameId); |
155 return JenkinsSmiHash.combine(libraryPathId, elementNameId); | |
156 } | |
157 } | 156 } |
158 return elementNameId; | 157 return elementNameId; |
159 } | 158 } |
160 | 159 |
161 /** | 160 /** |
162 * Return the indexable element kind representing the given [element]. | 161 * Return the indexable element kind representing the given [element]. |
163 */ | 162 */ |
164 static IndexableElementKind forElement(Element element) { | 163 static IndexableElementKind forElement(Element element) { |
165 if (element is ConstructorElement) { | 164 if (element is ConstructorElement) { |
166 ClassElement classElement = element.enclosingElement; | 165 ClassElement classElement = element.enclosingElement; |
167 int constructorIndex = classElement.constructors.indexOf(element); | 166 int constructorIndex = classElement.constructors.indexOf(element); |
168 return _constructorKinds.putIfAbsent(constructorIndex, | 167 return _constructorKinds.putIfAbsent(constructorIndex, |
169 () => new IndexableElementKind._(ElementKind.CONSTRUCTOR)); | 168 () => new IndexableElementKind._(ElementKind.CONSTRUCTOR)); |
170 } | 169 } |
171 ElementKind elementKind = element.kind; | 170 ElementKind elementKind = element.kind; |
172 return _kindMap.putIfAbsent( | 171 return _kindMap.putIfAbsent( |
173 elementKind, () => new IndexableElementKind._(elementKind)); | 172 elementKind, () => new IndexableElementKind._(elementKind)); |
174 } | 173 } |
175 } | 174 } |
OLD | NEW |