Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: pkg/analysis_server/lib/src/services/index/store/codec.dart

Issue 1156493004: Move index closer to the plugin API (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 services.src.index.store.codec; 5 library services.src.index.store.codec;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analysis_server/analysis/index/index_core.dart';
9 import 'package:analysis_server/src/services/index/index.dart'; 10 import 'package:analysis_server/src/services/index/index.dart';
11 import 'package:analysis_server/src/services/index/indexable_element.dart';
10 import 'package:analyzer/src/generated/element.dart'; 12 import 'package:analyzer/src/generated/element.dart';
11 import 'package:analyzer/src/generated/engine.dart'; 13 import 'package:analyzer/src/generated/engine.dart';
12 import 'package:analyzer/src/generated/source.dart'; 14 import 'package:analyzer/src/generated/source.dart';
13 import 'package:analyzer/src/generated/utilities_general.dart'; 15 import 'package:analyzer/src/generated/utilities_general.dart';
14 16
15 /** 17 /**
16 * A helper that encodes/decodes [AnalysisContext]s from/to integers. 18 * A helper that encodes/decodes [AnalysisContext]s from/to integers.
17 */ 19 */
18 class ContextCodec { 20 class ContextCodec {
19 /** 21 /**
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 int id = _contextToIndex.remove(context); 60 int id = _contextToIndex.remove(context);
59 if (id != null) { 61 if (id != null) {
60 _indexToContext.remove(id); 62 _indexToContext.remove(id);
61 } 63 }
62 } 64 }
63 } 65 }
64 66
65 /** 67 /**
66 * A helper that encodes/decodes [Element]s to/from integers. 68 * A helper that encodes/decodes [Element]s to/from integers.
67 */ 69 */
68 class ElementCodec { 70 class ElementCodec {
scheglov 2015/05/25 18:29:33 It seems that this class needs more work. We will
Brian Wilkerson 2015/06/01 13:20:08 Done
69 static const int _CONSTRUCTOR_KIND_BASE = -100;
70
71 final StringCodec _stringCodec; 71 final StringCodec _stringCodec;
72 72
73 ElementCodec(this._stringCodec); 73 ElementCodec(this._stringCodec);
74 74
75 /** 75 /**
76 * Returns an [Element] that corresponds to the given identifiers. 76 * Returns an [IndexableObject] that corresponds to the given identifiers.
77 */ 77 */
78 Element decode(AnalysisContext context, int fileId, int offset, int kindId) { 78 IndexableObject decode(
79 AnalysisContext context, int fileId, int offset, int kindId) {
79 String filePath = _stringCodec.decode(fileId); 80 String filePath = _stringCodec.decode(fileId);
80 List<Source> unitSources = context.getSourcesWithFullName(filePath); 81 IndexableObjectKind kind = IndexableObjectKind.getKind(kindId);
81 for (Source unitSource in unitSources) { 82 if (kind == null) {
82 List<Source> libSources = context.getLibrariesContaining(unitSource); 83 return null;
83 for (Source libSource in libSources) { 84 } else if (kind is IndexableNameKind) {
84 CompilationUnitElement unitElement = 85 return new IndexableElement(new NameElement(_stringCodec.decode(offset)));
85 context.getCompilationUnitElement(unitSource, libSource);
86 if (unitElement == null) {
87 return null;
88 }
89 if (kindId == ElementKind.LIBRARY.ordinal) {
90 return unitElement.library;
91 } else if (kindId == ElementKind.COMPILATION_UNIT.ordinal) {
92 return unitElement;
93 } else {
94 Element element = unitElement.getElementAt(offset);
95 if (element == null) {
96 return null;
97 }
98 if (element is ClassElement && kindId <= _CONSTRUCTOR_KIND_BASE) {
99 int constructorIndex = -1 * (kindId - _CONSTRUCTOR_KIND_BASE);
100 return element.constructors[constructorIndex];
101 }
102 if (element is PropertyInducingElement) {
103 if (kindId == ElementKind.GETTER.ordinal) {
104 return element.getter;
105 }
106 if (kindId == ElementKind.SETTER.ordinal) {
107 return element.setter;
108 }
109 }
110 return element;
111 }
112 }
113 } 86 }
114 return null; 87 return kind.decode(context, filePath, offset);
115 } 88 }
116 89
117 /** 90 /**
118 * Returns the first component of the [element] id. 91 * Returns the first component of the [element] id.
119 * In the most cases it is an encoding of the [element]'s file path. 92 * In the most cases it is an encoding of the [element]'s file path.
120 * If the given [element] is not defined in a file, returns `-1`. 93 * If the given [element] is not defined in a file, returns `-1`.
121 */ 94 */
122 int encode1(Element element) { 95 int encode1(IndexableObject indexable) {
123 Source source = element.source; 96 Source source = indexable.source;
124 if (source == null) { 97 if (source == null) {
125 return -1; 98 return -1;
126 } 99 }
127 String filePath = source.fullName; 100 String filePath = source.fullName;
128 return _stringCodec.encode(filePath); 101 return _stringCodec.encode(filePath);
129 } 102 }
130 103
131 /** 104 /**
132 * Returns the second component of the [element] id. 105 * Returns the second component of the [element] id.
133 * In the most cases it is the [element]'s name offset. 106 * In the most cases it is the [element]'s name offset.
134 */ 107 */
135 int encode2(Element element) { 108 int encode2(IndexableObject indexable) {
136 if (element is NameElement) { 109 if (indexable is IndexableName) {
137 String name = element.name; 110 String name = indexable.name;
138 return _stringCodec.encode(name); 111 return _stringCodec.encode(name);
139 } 112 }
140 if (element is ConstructorElement) { 113 int offset = indexable.offset;
141 return element.enclosingElement.nameOffset; 114 if (offset < 0) {
115 return _stringCodec.encode(indexable.name);
142 } 116 }
143 return element.nameOffset; 117 return offset;
144 } 118 }
145 119
146 /** 120 /**
147 * Returns the third component of the [element] id. 121 * Returns the third component of the [element] id.
148 * In the most cases it is the [element]'s kind. 122 * In the most cases it is the [element]'s kind.
149 */ 123 */
150 int encode3(Element element) { 124 int encode3(IndexableObject indexable) {
151 if (element is ConstructorElement) { 125 return indexable.kind.index;
152 ClassElement classElement = element.enclosingElement;
153 int constructorIndex = classElement.constructors.indexOf(element);
154 return _CONSTRUCTOR_KIND_BASE - constructorIndex;
155 }
156 return element.kind.ordinal;
157 } 126 }
158 127
159 /** 128 /**
160 * Returns an integer that corresponds to the name of [element]. 129 * Returns an integer that corresponds to the name of [element].
161 */ 130 */
162 int encodeHash(Element element) { 131 int encodeHash(IndexableObject indexable) {
scheglov 2015/05/25 18:29:33 Could we move implementation into IndexableObjectK
Brian Wilkerson 2015/06/01 13:20:08 TODO added so we don't forget in a future pass.
163 String elementName = element.displayName; 132 String elementName = indexable.name; // was: indexable.displayName;
164 int elementNameId = _stringCodec.encode(elementName); 133 int elementNameId = _stringCodec.encode(elementName);
165 LibraryElement libraryElement = element.library; 134 if (indexable is IndexableElement) {
166 if (libraryElement != null) { 135 LibraryElement libraryElement = indexable.element.library;
167 String libraryPath = libraryElement.source.fullName; 136 if (libraryElement != null) {
168 int libraryPathId = _stringCodec.encode(libraryPath); 137 String libraryPath = libraryElement.source.fullName;
169 return JenkinsSmiHash.combine(libraryPathId, elementNameId); 138 int libraryPathId = _stringCodec.encode(libraryPath);
170 } else { 139 return JenkinsSmiHash.combine(libraryPathId, elementNameId);
171 return elementNameId; 140 }
172 } 141 }
142 return elementNameId;
173 } 143 }
174 } 144 }
175 145
176 /** 146 /**
177 * A helper that encodes/decodes [Relationship]s to/from integers. 147 * A helper that encodes/decodes [Relationship]s to/from integers.
178 */ 148 */
179 class RelationshipCodec { 149 class RelationshipCodec {
180 final StringCodec _stringCodec; 150 final StringCodec _stringCodec;
181 151
182 RelationshipCodec(this._stringCodec); 152 RelationshipCodec(this._stringCodec);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 int encode(String name) { 187 int encode(String name) {
218 int index = nameToIndex[name]; 188 int index = nameToIndex[name];
219 if (index == null) { 189 if (index == null) {
220 index = _indexToName.length; 190 index = _indexToName.length;
221 nameToIndex[name] = index; 191 nameToIndex[name] = index;
222 _indexToName.add(name); 192 _indexToName.add(name);
223 } 193 }
224 return index; 194 return index;
225 } 195 }
226 } 196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698