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

Unified Diff: pkg/analyzer/lib/src/generated/element.dart

Issue 738673002: Use shorter DartElementLocation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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/analyzer/lib/src/generated/element.dart
diff --git a/pkg/analyzer/lib/src/generated/element.dart b/pkg/analyzer/lib/src/generated/element.dart
index a1fc144dbf098860bb539023d658610914e67a6a..cc4512886badab74b84c9233b426aa13d86c12a7 100644
--- a/pkg/analyzer/lib/src/generated/element.dart
+++ b/pkg/analyzer/lib/src/generated/element.dart
@@ -21,6 +21,7 @@ import 'sdk.dart' show DartSdk;
import 'source.dart';
import 'utilities_collection.dart';
import 'utilities_dart.dart';
+import 'element.dart';
/**
* Information about Angular application.
@@ -2399,6 +2400,7 @@ class CompilationUnitElementImpl extends UriReferencedElementImpl implements
void visitChildren(ElementVisitor visitor) {
super.visitChildren(visitor);
safelyVisitChildren(_accessors, visitor);
+ safelyVisitChildren(_enums, visitor);
safelyVisitChildren(_functions, visitor);
safelyVisitChildren(_typeAliases, visitor);
safelyVisitChildren(_types, visitor);
@@ -2972,6 +2974,11 @@ class DynamicElementImpl extends ElementImpl {
ElementKind get kind => ElementKind.DYNAMIC;
@override
+ ElementLocation get location {
+ return new DartElementLocation(null, null, nameOffset, kind);
+ }
+
+ @override
accept(ElementVisitor visitor) => null;
}
@@ -3559,7 +3566,20 @@ abstract class ElementImpl implements Element {
getAncestor((element) => element is LibraryElement);
@override
- ElementLocation get location => new ElementLocationImpl.con1(this);
+ ElementLocation get location {
+ // TODO(scheglov) add separate DartElementImpl
Brian Wilkerson 2014/11/18 15:11:22 I don't know what that means.
+ LibraryElement library = this.library;
+ if (library == null) {
+ return new DartElementLocation(null, null, nameOffset, kind);
+ }
+ String librarySourceEncoding = library.source.encoding;
+ String unitSourceEncoding = source.encoding;
+ return new DartElementLocation(
+ librarySourceEncoding,
+ unitSourceEncoding,
+ nameOffset,
+ kind);
+ }
@override
String get name => _name;
@@ -3913,172 +3933,51 @@ class ElementKind extends Enum<ElementKind> {
}
/**
- * The interface `ElementLocation` defines the behavior of objects that represent the location
- * of an element within the element model.
+ * The interface `ElementLocation` defines the behavior of objects that
+ * represent the location of an element within the element model.
*/
abstract class ElementLocation {
- /**
- * Return the path to the element whose location is represented by this object. Clients must not
- * modify the returned array.
- *
- * @return the path to the element whose location is represented by this object
- */
- List<String> get components;
-
- /**
- * Return an encoded representation of this location that can be used to create a location that is
- * equal to this location.
- *
- * @return an encoded representation of this location
- */
- String get encoding;
}
/**
- * Instances of the class `ElementLocationImpl` implement an [ElementLocation].
+ * The interface `DartElementLocation` defines the behavior of objects that
+ * represent the location of a Dart element within the element model.
*/
-class ElementLocationImpl implements ElementLocation {
- /**
- * The character used to separate components in the encoded form.
- */
- static int _SEPARATOR_CHAR = 0x3B;
-
+class DartElementLocation implements ElementLocation {
/**
- * The path to the element whose location is represented by this object.
+ * An encoded representation of the library source that contains this element.
*/
- List<String> _components;
+ final String librarySourceEncoding;
/**
- * Initialize a newly created location to represent the given element.
- *
- * @param element the element whose location is being represented
+ * An encoded representation of the unit source that contains this element.
*/
- ElementLocationImpl.con1(Element element) {
- List<String> components = new List<String>();
- Element ancestor = element;
- while (ancestor != null) {
- components.insert(0, (ancestor as ElementImpl).identifier);
- ancestor = ancestor.enclosingElement;
- }
- this._components = components;
- }
+ final String unitSourceEncoding;
/**
- * Initialize a newly created location from the given encoded form.
- *
- * @param encoding the encoded form of a location
+ * The offset of the name of this element in the file that contains
+ * the declaration of this element, or `-1` if this element is synthetic,
+ * does not have a name, or otherwise does not have an offset.
*/
- ElementLocationImpl.con2(String encoding) {
- this._components = _decode(encoding);
- }
+ final int nameOffset;
/**
- * Initialize a newly created location from the given components.
- *
- * @param components the components of a location
+ * The kind of this element.
*/
- ElementLocationImpl.con3(List<String> components) {
- this._components = components;
- }
-
- @override
- List<String> get components => _components;
+ final ElementKind kind;
- @override
- String get encoding {
- StringBuffer buffer = new StringBuffer();
- int length = _components.length;
- for (int i = 0; i < length; i++) {
- if (i > 0) {
- buffer.writeCharCode(_SEPARATOR_CHAR);
- }
- _encode(buffer, _components[i]);
- }
- return buffer.toString();
- }
-
- @override
- int get hashCode {
- int result = 1;
- for (int i = 0; i < _components.length; i++) {
- String component = _components[i];
- result = 31 * result + component.hashCode;
- }
- return result;
- }
-
- @override
- bool operator ==(Object object) {
- if (identical(this, object)) {
- return true;
- }
- if (object is! ElementLocationImpl) {
- return false;
- }
- ElementLocationImpl location = object as ElementLocationImpl;
- List<String> otherComponents = location._components;
- int length = _components.length;
- if (otherComponents.length != length) {
- return false;
- }
- for (int i = 0; i < length; i++) {
- if (_components[i] != otherComponents[i]) {
- return false;
- }
- }
- return true;
- }
+ DartElementLocation(this.librarySourceEncoding, this.unitSourceEncoding,
+ this.nameOffset, this.kind);
@override
- String toString() => encoding;
-
- /**
- * Decode the encoded form of a location into an array of components.
- *
- * @param encoding the encoded form of a location
- * @return the components that were encoded
- */
- List<String> _decode(String encoding) {
- List<String> components = new List<String>();
- StringBuffer buffer = new StringBuffer();
- int index = 0;
- int length = encoding.length;
- while (index < length) {
- int currentChar = encoding.codeUnitAt(index);
- if (currentChar == _SEPARATOR_CHAR) {
- if (index + 1 < length &&
- encoding.codeUnitAt(index + 1) == _SEPARATOR_CHAR) {
- buffer.writeCharCode(_SEPARATOR_CHAR);
- index += 2;
- } else {
- components.add(buffer.toString());
- buffer = new StringBuffer();
- index++;
- }
- } else {
- buffer.writeCharCode(currentChar);
- index++;
- }
- }
- components.add(buffer.toString());
- return components;
- }
-
- /**
- * Append an encoded form of the given component to the given builder.
- *
- * @param builder the builder to which the encoded component is to be appended
- * @param component the component to be appended to the builder
- */
- void _encode(StringBuffer buffer, String component) {
- int length = component.length;
- for (int i = 0; i < length; i++) {
- int currentChar = component.codeUnitAt(i);
- if (currentChar == _SEPARATOR_CHAR) {
- buffer.writeCharCode(_SEPARATOR_CHAR);
- }
- buffer.writeCharCode(currentChar);
+ bool operator ==(Object other) {
+ if (other is DartElementLocation) {
+ return other.librarySourceEncoding == librarySourceEncoding &&
+ other.unitSourceEncoding == unitSourceEncoding &&
+ other.nameOffset == nameOffset &&
+ other.kind == kind;
}
+ return false;
}
}
@@ -8589,6 +8488,51 @@ class LibraryElementImpl extends ElementImpl implements LibraryElement {
return null;
}
+ /**
+ * Returns the child [Element] of this library.
+ *
+ * [unitSourceEncoding] - the encoding of the unit.
+ * [offset] - the name offset of the element.
+ * [kind] - the kind of the element.
+ */
+ ElementImpl getChildElement(String unitSourceEncoding, int offset,
+ ElementKind kind) {
+ for (CompilationUnitElement unit in units) {
+ if (unit.source.encoding == unitSourceEncoding) {
+ ElementImpl element = _findElementAt(unit, offset, kind);
+ if (element != null) {
+ return element;
+ }
+ break;
+ }
+ }
+ for (ImportElement importElement in _imports) {
+ if (importElement.nameOffset == offset) {
+ return importElement as ImportElementImpl;
+ }
+ }
+ for (ExportElement exportElement in _exports) {
+ if (exportElement.nameOffset == offset) {
+ return exportElement as ExportElementImpl;
+ }
+ }
+ return null;
+ }
+
+ ElementImpl _findElementAt(CompilationUnitElement unit, int offset,
Brian Wilkerson 2014/11/18 15:11:22 Seems like this should either be moved to Compilat
+ ElementKind kind) {
+ if (offset == -1) {
+ return unit as ElementImpl;
+ }
+ _FindElementAt visitor = new _FindElementAt(offset, kind);
+ try {
+ unit.accept(visitor);
+ } on _FindElementAtDone catch (e) {
+ return visitor.result;
+ }
+ return null;
+ }
+
@override
List<ImportElement> getImportsWithPrefix(PrefixElement prefixElement) {
int count = _imports.length;
@@ -12470,3 +12414,25 @@ class VoidTypeImpl extends TypeImpl implements VoidType {
List<DartType> parameterTypes) =>
this;
}
+
+
+class _FindElementAt extends GeneralizingElementVisitor {
Brian Wilkerson 2014/11/18 15:11:22 I'm not sure why this is a private class. Seems li
+ final int offset;
+ final ElementKind kind;
+ Element result;
+
+ _FindElementAt(this.offset, this.kind);
+
+ @override
+ visitElement(Element element) {
+ if (element.nameOffset == offset && element.kind == kind) {
+ result = element;
+ throw new _FindElementAtDone();
+ }
+ super.visitElement(element);
+ }
+}
+
+
+class _FindElementAtDone {
+}

Powered by Google App Engine
This is Rietveld 408576698