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

Unified Diff: pkg/analysis_server/lib/src/services/index/index_contributor.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/index_contributor.dart
diff --git a/pkg/analysis_server/lib/src/services/index/index_contributor.dart b/pkg/analysis_server/lib/src/services/index/index_contributor.dart
index b5bef6bb75c792e51faad20d6e88e4880016de36..2c45fdfea372d075ae7fe6c17df3e74f2fd77c52 100644
--- a/pkg/analysis_server/lib/src/services/index/index_contributor.dart
+++ b/pkg/analysis_server/lib/src/services/index/index_contributor.dart
@@ -6,9 +6,12 @@ library services.src.index.index_contributor;
import 'dart:collection' show Queue;
+import 'package:analysis_server/analysis/index/index_core.dart';
+import 'package:analysis_server/analysis/index/index_dart.dart';
import 'package:analysis_server/src/services/correction/namespace.dart';
import 'package:analysis_server/src/services/index/index.dart';
import 'package:analysis_server/src/services/index/index_store.dart';
+import 'package:analysis_server/src/services/index/indexable_element.dart';
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart';
@@ -65,6 +68,19 @@ void indexHtmlUnit(
}
/**
+ * An [IndexContributor] that can be used to contribute relationships for Dart
+ * files.
+ */
+class DefaultDartIndexContributor extends DartIndexContributor {
+ @override
+ void internalContributeTo(IndexStore store, CompilationUnit unit) {
+ _IndexContributor contributor =
+ new _IndexContributor(store as InternalIndexStore);
+ unit.accept(contributor);
+ }
+}
+
+/**
* Visits a resolved AST and adds relationships into [InternalIndexStore].
*/
class _IndexContributor extends GeneralizingAstVisitor {
@@ -90,12 +106,13 @@ class _IndexContributor extends GeneralizingAstVisitor {
}
/**
- * @return the inner-most enclosing [Element], may be `null`.
+ * Return the inner-most enclosing [Element], wrapped as an indexable object,
+ * or `null` if there is no enclosing element.
*/
- Element peekElement() {
+ IndexableElement peekElement() {
for (Element element in _elementStack) {
if (element != null) {
- return element;
+ return new IndexableElement(element);
}
}
return null;
@@ -108,7 +125,8 @@ class _IndexContributor extends GeneralizingAstVisitor {
void recordRelationship(
Element element, RelationshipImpl relationship, LocationImpl location) {
if (element != null && location != null) {
- _store.recordRelationship(element, relationship, location);
+ _store.recordRelationship(
+ new IndexableElement(element), relationship, location);
}
}
@@ -231,8 +249,8 @@ class _IndexContributor extends GeneralizingAstVisitor {
if (fieldName != null) {
Element element = fieldName.staticElement;
LocationImpl location = _createLocationForNode(fieldName);
- _store.recordRelationship(
- element, IndexConstants.IS_WRITTEN_BY, location);
+ _store.recordRelationship(new IndexableElement(element),
scheglov 2015/05/25 18:29:33 Could we use [this.]recordRelationship(element, ki
Brian Wilkerson 2015/06/01 13:20:08 Done
+ IndexConstants.IS_WRITTEN_BY, location);
}
// index expression
if (expression != null) {
@@ -374,9 +392,10 @@ class _IndexContributor extends GeneralizingAstVisitor {
}
// name invocation
{
- Element nameElement = new NameElement(name.name);
+ IndexableObject indexable =
+ new IndexableElement(new NameElement(name.name));
_store.recordRelationship(
- nameElement, IndexConstants.IS_INVOKED_BY, location);
+ indexable, IndexConstants.IS_INVOKED_BY, location);
}
_recordImportElementReferenceWithoutPrefix(name);
super.visitMethodInvocation(node);
@@ -426,12 +445,16 @@ class _IndexContributor extends GeneralizingAstVisitor {
@override
visitSimpleIdentifier(SimpleIdentifier node) {
- Element nameElement = new NameElement(node.name);
+ IndexableObject indexable =
+ new IndexableElement(new NameElement(node.name));
LocationImpl location = _createLocationForNode(node);
+ if (location == null) {
+ return;
+ }
// name in declaration
if (node.inDeclarationContext()) {
- recordRelationship(
- nameElement, IndexConstants.NAME_IS_DEFINED_BY, location);
+ _store.recordRelationship(
+ indexable, IndexConstants.NAME_IS_DEFINED_BY, location);
return;
}
// prepare information
@@ -447,21 +470,22 @@ class _IndexContributor extends GeneralizingAstVisitor {
bool inSetterContext = node.inSetterContext();
if (inGetterContext && inSetterContext) {
_store.recordRelationship(
- nameElement, IndexConstants.IS_READ_WRITTEN_BY, location);
+ indexable, IndexConstants.IS_READ_WRITTEN_BY, location);
} else if (inGetterContext) {
_store.recordRelationship(
- nameElement, IndexConstants.IS_READ_BY, location);
+ indexable, IndexConstants.IS_READ_BY, location);
} else if (inSetterContext) {
_store.recordRelationship(
- nameElement, IndexConstants.IS_WRITTEN_BY, location);
+ indexable, IndexConstants.IS_WRITTEN_BY, location);
}
}
// this.field parameter
if (element is FieldFormalParameterElement) {
- RelationshipImpl relationship = peekElement() == element
+ RelationshipImpl relationship = peekElement().element == element
? IndexConstants.IS_WRITTEN_BY
: IndexConstants.IS_REFERENCED_BY;
- _store.recordRelationship(element.field, relationship, location);
+ _store.recordRelationship(
+ new IndexableElement(element.field), relationship, location);
return;
}
// record specific relations
@@ -585,8 +609,8 @@ class _IndexContributor extends GeneralizingAstVisitor {
if (node is SimpleIdentifier) {
isResolved = node.bestElement != null;
}
- Element element = peekElement();
- return new LocationImpl(element, node.offset, node.length,
+ IndexableObject indexable = peekElement();
+ return new LocationImpl(indexable, node.offset, node.length,
isQualified: isQualified, isResolved: isResolved);
}
@@ -598,7 +622,7 @@ class _IndexContributor extends GeneralizingAstVisitor {
* inner-most [Element].
*/
LocationImpl _createLocationForOffset(int offset, int length) {
- Element element = peekElement();
+ IndexableObject element = peekElement();
return new LocationImpl(element, offset, length);
}
@@ -606,7 +630,7 @@ class _IndexContributor extends GeneralizingAstVisitor {
* @return the [LocationImpl] representing location of the [Token].
*/
LocationImpl _createLocationForToken(Token token, bool isResolved) {
- Element element = peekElement();
+ IndexableObject element = peekElement();
return new LocationImpl(element, token.offset, token.length,
isQualified: true, isResolved: isResolved);
}
@@ -744,19 +768,20 @@ class _IndexContributor extends GeneralizingAstVisitor {
* Records the [Element] definition in the library and universe.
*/
void _recordTopLevelElementDefinition(Element element) {
- LocationImpl location = createLocation(element);
+ LocationImpl location = createLocation(new IndexableElement(element));
recordRelationship(_libraryElement, IndexConstants.DEFINES, location);
_store.recordTopLevelDeclaration(element);
}
/**
- * Creates a [LocationImpl] representing declaration of the [Element].
+ * Creates a location representing the declaration of the given [indexable]
+ * element.
*/
- static LocationImpl createLocation(Element element) {
- if (element != null) {
- int offset = element.nameOffset;
- int length = element.displayName.length;
- return new LocationImpl(element, offset, length);
+ static LocationImpl createLocation(IndexableElement indexable) {
scheglov 2015/05/25 18:29:33 The same here - we need IndexableElement only for
Brian Wilkerson 2015/06/01 13:20:08 Given that this method is only invoked in one plac
+ if (indexable != null) {
+ int offset = indexable.offset;
+ int length = indexable.length;
+ return new LocationImpl(indexable, offset, length);
}
return null;
}

Powered by Google App Engine
This is Rietveld 408576698