Make IndexableObjectKind parameterized.

LMK if this is a bad change.

R=brianwilkerson@google.com
BUG=

Review URL: https://codereview.chromium.org/1413563004 .
This commit is contained in:
Konstantin Shcheglov 2015-10-19 10:13:43 -07:00
parent 365ea71f78
commit e0636b183c
4 changed files with 30 additions and 31 deletions

View file

@ -24,7 +24,7 @@ typedef int StringToInt(String string);
/**
* An object that can have a [Relationship] with various [Location]s in a code
* base. The object is abstractly represented by a [kind] and an [offset] within
* a [source].
* a file with the [filePath].
*
* Clients must ensure that two distinct objects in the same source cannot have
* the same kind and offset. Failure to do so will make it impossible for
@ -44,7 +44,7 @@ abstract class IndexableObject {
IndexableObjectKind get kind;
/**
* Return the offset of the indexable object within its source.
* Return the offset of the indexable object within its file.
*/
int get offset;
}
@ -54,7 +54,7 @@ abstract class IndexableObject {
*
* Clients may implement this class when implementing plugins.
*/
abstract class IndexableObjectKind {
abstract class IndexableObjectKind<T extends IndexableObject> {
/**
* The next available index for a newly created kind of indexable object.
*/
@ -84,7 +84,7 @@ abstract class IndexableObjectKind {
* [context], in the source with the given [filePath], and at the given
* [offset].
*/
IndexableObject decode(AnalysisContext context, String filePath, int offset);
T decode(AnalysisContext context, String filePath, int offset);
/**
* Returns the hash value that corresponds to the given [indexable].
@ -105,7 +105,7 @@ abstract class IndexableObjectKind {
* object does not have a name, some other value may be returned, but it still
* must be always the same for the same object and have good selectivity.
*/
int encodeHash(StringToInt stringToInt, IndexableObject indexable);
int encodeHash(StringToInt stringToInt, T indexable);
/**
* Return the object kind with the given [index].

View file

@ -81,7 +81,7 @@ class IndexableName implements IndexableObject {
String get filePath => null;
@override
IndexableObjectKind get kind => IndexableNameKind.INSTANCE;
IndexableNameKind get kind => IndexableNameKind.INSTANCE;
@override
int get offset {
@ -99,7 +99,7 @@ class IndexableName implements IndexableObject {
/**
* The kind of an indexable name.
*/
class IndexableNameKind implements IndexableObjectKind {
class IndexableNameKind implements IndexableObjectKind<IndexableName> {
/**
* The unique instance of this class.
*/
@ -119,14 +119,14 @@ class IndexableNameKind implements IndexableObjectKind {
}
@override
IndexableObject decode(AnalysisContext context, String filePath, int offset) {
IndexableName decode(AnalysisContext context, String filePath, int offset) {
throw new UnsupportedError(
'Indexable names cannot be decoded through their kind');
}
@override
int encodeHash(StringToInt stringToInt, IndexableObject indexable) {
String name = (indexable as IndexableName).name;
int encodeHash(StringToInt stringToInt, IndexableName indexable) {
String name = indexable.name;
return stringToInt(name);
}
}

View file

@ -30,11 +30,16 @@ class IndexableElement implements IndexableObject {
}
}
@override
String get filePath {
return element.source?.fullName;
}
@override
int get hashCode => element.hashCode;
@override
IndexableObjectKind get kind => IndexableElementKind.forElement(element);
IndexableElementKind get kind => IndexableElementKind.forElement(element);
@override
int get offset {
@ -44,11 +49,6 @@ class IndexableElement implements IndexableObject {
return element.nameOffset;
}
@override
String get filePath {
return element.source?.fullName;
}
@override
bool operator ==(Object object) =>
object is IndexableElement && element == object.element;
@ -60,7 +60,7 @@ class IndexableElement implements IndexableObject {
/**
* The kind associated with an [IndexableElement].
*/
class IndexableElementKind implements IndexableObjectKind {
class IndexableElementKind implements IndexableObjectKind<IndexableElement> {
/**
* A table mapping element kinds to the corresponding indexable element kind.
*/
@ -104,7 +104,8 @@ class IndexableElementKind implements IndexableObjectKind {
}
@override
IndexableObject decode(AnalysisContext context, String filePath, int offset) {
IndexableElement decode(
AnalysisContext context, String filePath, int offset) {
List<Source> unitSources = context.getSourcesWithFullName(filePath);
for (Source unitSource in unitSources) {
List<Source> libSources = context.getLibrariesContaining(unitSource);
@ -143,17 +144,15 @@ class IndexableElementKind implements IndexableObjectKind {
}
@override
int encodeHash(StringToInt stringToInt, IndexableObject indexable) {
Element element = (indexable as IndexableElement).element;
int encodeHash(StringToInt stringToInt, IndexableElement indexable) {
Element element = indexable.element;
String elementName = element.displayName;
int elementNameId = stringToInt(elementName);
if (indexable is IndexableElement) {
LibraryElement libraryElement = indexable.element.library;
if (libraryElement != null) {
String libraryPath = libraryElement.source.fullName;
int libraryPathId = stringToInt(libraryPath);
return JenkinsSmiHash.combine(libraryPathId, elementNameId);
}
LibraryElement libraryElement = element.library;
if (libraryElement != null) {
String libraryPath = libraryElement.source.fullName;
int libraryPathId = stringToInt(libraryPath);
return JenkinsSmiHash.combine(libraryPathId, elementNameId);
}
return elementNameId;
}

View file

@ -42,7 +42,7 @@ class IndexableFile implements IndexableObject {
/**
* The kind of an indexable file.
*/
class IndexableFileKind implements IndexableObjectKind {
class IndexableFileKind implements IndexableObjectKind<IndexableFile> {
/**
* The unique instance of this class.
*/
@ -62,13 +62,13 @@ class IndexableFileKind implements IndexableObjectKind {
}
@override
IndexableObject decode(AnalysisContext context, String filePath, int offset) {
IndexableFile decode(AnalysisContext context, String filePath, int offset) {
return new IndexableFile(filePath);
}
@override
int encodeHash(StringToInt stringToInt, IndexableObject indexable) {
String path = (indexable as IndexableFile).path;
int encodeHash(StringToInt stringToInt, IndexableFile indexable) {
String path = indexable.path;
return stringToInt(path);
}
}