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

Unified Diff: pkg/analysis_server/lib/src/computer/element.dart

Issue 356563006: Element.fromEngine() constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/computer/element.dart
diff --git a/pkg/analysis_server/lib/src/computer/element.dart b/pkg/analysis_server/lib/src/computer/element.dart
index ff6e98d200d1564838cfd8d85d212f8aa269503e..3c63d2ef667eff520386392c12ea2e8be4f924c7 100644
--- a/pkg/analysis_server/lib/src/computer/element.dart
+++ b/pkg/analysis_server/lib/src/computer/element.dart
@@ -5,6 +5,8 @@
library computer.element;
import 'package:analysis_server/src/constants.dart';
+import 'package:analyzer/src/generated/element.dart' as engine;
+import 'package:analyzer/src/generated/utilities_dart.dart' as engine;
/**
@@ -13,12 +15,12 @@ import 'package:analysis_server/src/constants.dart';
class Element {
static const List<Element> EMPTY_ARRAY = const <Element>[];
- static const int _FLAG_ABSTRACT = 0x01;
- static const int _FLAG_CONST = 0x02;
- static const int _FLAG_DEPRECATED = 0x20;
- static const int _FLAG_FINAL = 0x04;
- static const int _FLAG_PRIVATE = 0x10;
- static const int _FLAG_STATIC = 0x08;
+ static const int FLAG_ABSTRACT = 0x01;
+ static const int FLAG_CONST = 0x02;
+ static const int FLAG_DEPRECATED = 0x20;
+ static const int FLAG_FINAL = 0x04;
+ static const int FLAG_PRIVATE = 0x10;
+ static const int FLAG_STATIC = 0x08;
final bool isAbstract;
final bool isConst;
@@ -65,24 +67,36 @@ class Element {
this.isDeprecated, {this.parameters, this.returnType, this.isAbstract: false,
this.isConst: false, this.isFinal: false, this.isStatic: false});
+ factory Element.fromEngine(engine.Element element) {
+ String name = element.displayName;
+ int nameLength = name != null ? name.length : 0;
+ String elementParameters = _getParametersString(element);
+ String elementReturnType = _getReturnTypeString(element);
+ return new Element(ElementKind.valueOfEngine(element.kind), name,
+ element.nameOffset, nameLength, element.isPrivate, element.isDeprecated,
+ parameters: elementParameters, returnType: elementReturnType, isAbstract:
+ _isAbstract(element), isConst: _isConst(element), isFinal: _isFinal(element),
+ isStatic: _isStatic(element));
+ }
+
factory Element.fromJson(Map<String, Object> map) {
ElementKind kind = ElementKind.valueOf(map[KIND]);
int flags = map[FLAGS];
return new Element(kind, map[NAME], map[OFFSET], map[LENGTH], _hasFlag(
- flags, _FLAG_PRIVATE), _hasFlag(flags, _FLAG_DEPRECATED), parameters:
+ flags, FLAG_PRIVATE), _hasFlag(flags, FLAG_DEPRECATED), parameters:
map[PARAMETERS], returnType: map[RETURN_TYPE], isAbstract: _hasFlag(flags,
- _FLAG_ABSTRACT), isConst: _hasFlag(flags, _FLAG_CONST), isFinal: _hasFlag(flags,
- _FLAG_FINAL), isStatic: _hasFlag(flags, _FLAG_STATIC));
+ FLAG_ABSTRACT), isConst: _hasFlag(flags, FLAG_CONST), isFinal: _hasFlag(flags,
+ FLAG_FINAL), isStatic: _hasFlag(flags, FLAG_STATIC));
}
int get flags {
int flags = 0;
- if (isAbstract) flags |= _FLAG_ABSTRACT;
- if (isConst) flags |= _FLAG_CONST;
- if (isFinal) flags |= _FLAG_FINAL;
- if (isStatic) flags |= _FLAG_STATIC;
- if (isPrivate) flags |= _FLAG_PRIVATE;
- if (isDeprecated) flags |= _FLAG_DEPRECATED;
+ if (isAbstract) flags |= FLAG_ABSTRACT;
+ if (isConst) flags |= FLAG_CONST;
+ if (isFinal) flags |= FLAG_FINAL;
+ if (isStatic) flags |= FLAG_STATIC;
+ if (isPrivate) flags |= FLAG_PRIVATE;
+ if (isDeprecated) flags |= FLAG_DEPRECATED;
return flags;
}
@@ -103,7 +117,82 @@ class Element {
return json;
}
+ static String _getParametersString(engine.Element element) {
+ if (element is engine.ExecutableElement) {
+ var sb = new StringBuffer();
Brian Wilkerson 2014/06/25 13:50:08 This duplicates code in engine.ExecutableElement.
scheglov 2014/06/25 17:20:46 I've added TODO.
+ String closeOptionalString = '';
+ for (var parameter in element.parameters) {
+ if (sb.isNotEmpty) {
+ sb.write(', ');
+ }
+ if (closeOptionalString.isEmpty) {
+ if (parameter.kind == engine.ParameterKind.NAMED) {
+ sb.write('{');
+ closeOptionalString = '}';
+ }
+ if (parameter.kind == engine.ParameterKind.POSITIONAL) {
+ sb.write('[');
+ closeOptionalString = ']';
+ }
+ }
+ sb.write(parameter.toString());
+ }
+ sb.write(closeOptionalString);
+ return '(' + sb.toString() + ')';
+ } else {
+ return null;
+ }
+ }
+
+ static String _getReturnTypeString(engine.Element element) {
+ if ((element is engine.ExecutableElement)) {
+ return element.returnType.toString();
+ } else {
+ return null;
+ }
+ }
+
static bool _hasFlag(int flags, int flag) => (flags & flag) != 0;
+
+ static bool _isAbstract(engine.Element element) {
+ if (element is engine.ClassElement) {
Brian Wilkerson 2014/06/25 13:50:08 Code with lots of 'is' expressions is always suspe
scheglov 2014/06/25 17:20:46 I've added TODO.
+ return element.isAbstract;
+ }
+ if (element is engine.MethodElement) {
+ return element.isAbstract;
+ }
+ if (element is engine.PropertyAccessorElement) {
+ return element.isAbstract;
+ }
+ return false;
+ }
+
+ static bool _isConst(engine.Element element) {
+ if (element is engine.ConstructorElement) {
+ return element.isConst;
+ }
+ if (element is engine.VariableElement) {
+ return element.isConst;
+ }
+ return false;
+ }
+
+ static bool _isFinal(engine.Element element) {
+ if (element is engine.VariableElement) {
+ return element.isFinal;
+ }
+ return false;
+ }
+
+ static bool _isStatic(engine.Element element) {
+ if (element is engine.ExecutableElement) {
+ return element.isStatic;
+ }
+ if (element is engine.PropertyInducingElement) {
+ return element.isStatic;
+ }
+ return false;
+ }
}
@@ -131,6 +220,9 @@ class ElementKind {
const ElementKind(this.name);
+ @override
+ String toString() => name;
+
static ElementKind valueOf(String name) {
if (CLASS.name == name) return CLASS;
if (CLASS_TYPE_ALIAS.name == name) return CLASS_TYPE_ALIAS;
@@ -149,4 +241,41 @@ class ElementKind {
if (UNKNOWN.name == name) return UNKNOWN;
throw new ArgumentError('Unknown ElementKind: $name');
}
+
+ static ElementKind valueOfEngine(engine.ElementKind kind) {
Brian Wilkerson 2014/06/25 13:50:08 What's the value of having two implementations of
scheglov 2014/06/25 17:20:46 Well, there isn't. But ElementKind here is the one
+ if (kind == engine.ElementKind.CLASS) {
+ return CLASS;
+ }
+ if (kind == engine.ElementKind.COMPILATION_UNIT) {
+ return COMPILATION_UNIT;
+ }
+ if (kind == engine.ElementKind.CONSTRUCTOR) {
+ return CONSTRUCTOR;
+ }
+ if (kind == engine.ElementKind.FIELD) {
+ return FIELD;
+ }
+ if (kind == engine.ElementKind.FUNCTION) {
+ return FUNCTION;
+ }
+ if (kind == engine.ElementKind.FUNCTION_TYPE_ALIAS) {
+ return FUNCTION_TYPE_ALIAS;
+ }
+ if (kind == engine.ElementKind.GETTER) {
+ return GETTER;
+ }
+ if (kind == engine.ElementKind.LIBRARY) {
+ return LIBRARY;
+ }
+ if (kind == engine.ElementKind.METHOD) {
+ return METHOD;
+ }
+ if (kind == engine.ElementKind.SETTER) {
+ return SETTER;
+ }
+ if (kind == engine.ElementKind.TOP_LEVEL_VARIABLE) {
+ return TOP_LEVEL_VARIABLE;
+ }
+ return UNKNOWN;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698