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

Unified 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, 7 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/index/store/codec.dart
diff --git a/pkg/analysis_server/lib/src/services/index/store/codec.dart b/pkg/analysis_server/lib/src/services/index/store/codec.dart
index 0c2f81624192ea4aff8c95e0b3b8551d29cc0097..0330c965b87d6cb9ed7d27b7b81c8ae564467ba5 100644
--- a/pkg/analysis_server/lib/src/services/index/store/codec.dart
+++ b/pkg/analysis_server/lib/src/services/index/store/codec.dart
@@ -6,7 +6,9 @@ library services.src.index.store.codec;
import 'dart:collection';
+import 'package:analysis_server/analysis/index/index_core.dart';
import 'package:analysis_server/src/services/index/index.dart';
+import 'package:analysis_server/src/services/index/indexable_element.dart';
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
@@ -66,52 +68,23 @@ class ContextCodec {
* A helper that encodes/decodes [Element]s to/from integers.
*/
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
- static const int _CONSTRUCTOR_KIND_BASE = -100;
-
final StringCodec _stringCodec;
ElementCodec(this._stringCodec);
/**
- * Returns an [Element] that corresponds to the given identifiers.
+ * Returns an [IndexableObject] that corresponds to the given identifiers.
*/
- Element decode(AnalysisContext context, int fileId, int offset, int kindId) {
+ IndexableObject decode(
+ AnalysisContext context, int fileId, int offset, int kindId) {
String filePath = _stringCodec.decode(fileId);
- List<Source> unitSources = context.getSourcesWithFullName(filePath);
- for (Source unitSource in unitSources) {
- List<Source> libSources = context.getLibrariesContaining(unitSource);
- for (Source libSource in libSources) {
- CompilationUnitElement unitElement =
- context.getCompilationUnitElement(unitSource, libSource);
- if (unitElement == null) {
- return null;
- }
- if (kindId == ElementKind.LIBRARY.ordinal) {
- return unitElement.library;
- } else if (kindId == ElementKind.COMPILATION_UNIT.ordinal) {
- return unitElement;
- } else {
- Element element = unitElement.getElementAt(offset);
- if (element == null) {
- return null;
- }
- if (element is ClassElement && kindId <= _CONSTRUCTOR_KIND_BASE) {
- int constructorIndex = -1 * (kindId - _CONSTRUCTOR_KIND_BASE);
- return element.constructors[constructorIndex];
- }
- if (element is PropertyInducingElement) {
- if (kindId == ElementKind.GETTER.ordinal) {
- return element.getter;
- }
- if (kindId == ElementKind.SETTER.ordinal) {
- return element.setter;
- }
- }
- return element;
- }
- }
+ IndexableObjectKind kind = IndexableObjectKind.getKind(kindId);
+ if (kind == null) {
+ return null;
+ } else if (kind is IndexableNameKind) {
+ return new IndexableElement(new NameElement(_stringCodec.decode(offset)));
}
- return null;
+ return kind.decode(context, filePath, offset);
}
/**
@@ -119,8 +92,8 @@ class ElementCodec {
* In the most cases it is an encoding of the [element]'s file path.
* If the given [element] is not defined in a file, returns `-1`.
*/
- int encode1(Element element) {
- Source source = element.source;
+ int encode1(IndexableObject indexable) {
+ Source source = indexable.source;
if (source == null) {
return -1;
}
@@ -132,44 +105,41 @@ class ElementCodec {
* Returns the second component of the [element] id.
* In the most cases it is the [element]'s name offset.
*/
- int encode2(Element element) {
- if (element is NameElement) {
- String name = element.name;
+ int encode2(IndexableObject indexable) {
+ if (indexable is IndexableName) {
+ String name = indexable.name;
return _stringCodec.encode(name);
}
- if (element is ConstructorElement) {
- return element.enclosingElement.nameOffset;
+ int offset = indexable.offset;
+ if (offset < 0) {
+ return _stringCodec.encode(indexable.name);
}
- return element.nameOffset;
+ return offset;
}
/**
* Returns the third component of the [element] id.
* In the most cases it is the [element]'s kind.
*/
- int encode3(Element element) {
- if (element is ConstructorElement) {
- ClassElement classElement = element.enclosingElement;
- int constructorIndex = classElement.constructors.indexOf(element);
- return _CONSTRUCTOR_KIND_BASE - constructorIndex;
- }
- return element.kind.ordinal;
+ int encode3(IndexableObject indexable) {
+ return indexable.kind.index;
}
/**
* Returns an integer that corresponds to the name of [element].
*/
- int encodeHash(Element element) {
- String elementName = element.displayName;
+ 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.
+ String elementName = indexable.name; // was: indexable.displayName;
int elementNameId = _stringCodec.encode(elementName);
- LibraryElement libraryElement = element.library;
- if (libraryElement != null) {
- String libraryPath = libraryElement.source.fullName;
- int libraryPathId = _stringCodec.encode(libraryPath);
- return JenkinsSmiHash.combine(libraryPathId, elementNameId);
- } else {
- return elementNameId;
+ if (indexable is IndexableElement) {
+ LibraryElement libraryElement = indexable.element.library;
+ if (libraryElement != null) {
+ String libraryPath = libraryElement.source.fullName;
+ int libraryPathId = _stringCodec.encode(libraryPath);
+ return JenkinsSmiHash.combine(libraryPathId, elementNameId);
+ }
}
+ return elementNameId;
}
}

Powered by Google App Engine
This is Rietveld 408576698