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

Side by Side Diff: pkg/analysis_server/lib/src/computer/element.dart

Issue 340183003: 'Element' and 'Outline' implementations for the new server API. (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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library computer.element;
6
7 import 'package:analysis_server/src/constants.dart';
8
9
10 /**
11 * Information about an element.
12 */
13 class Element {
14 static const List<Element> EMPTY_ARRAY = const <Element>[];
15
16 static const int _FLAG_ABSTRACT = 0x01;
17 static const int _FLAG_CONST = 0x02;
18 static const int _FLAG_DEPRECATED = 0x20;
19 static const int _FLAG_FINAL = 0x04;
20 static const int _FLAG_PRIVATE = 0x10;
21 static const int _FLAG_STATIC = 0x08;
22
23 final bool isAbstract;
24 final bool isConst;
25 final bool isDeprecated;
26 final bool isFinal;
27 final bool isPrivate;
28 final bool isStatic;
29
30 /**
31 * The kind of the element.
32 */
33 final ElementKind kind;
34
35 /**
36 * The length of the name of the element.
37 */
38 final int length;
39
40 /**
41 * The name of the element. This is typically used as the label in the outline .
42 */
43 final String name;
44
45 /**
46 * The offset of the name of the element.
47 */
48 final int offset;
49
50 /**
51 * The parameter list for the element.
52 * If the element is not a method or function then `null`.
53 * If the element has zero parameters, then `()`.
54 */
55 final String parameters;
56
57 /**
58 * The return type of the element.
59 * If the element is not a method or function then `null`.
60 * If the element does not have a declared return type, then an empty string.
61 */
62 final String returnType;
63
64 Element(this.kind, this.name, this.offset, this.length, this.isPrivate,
65 this.isDeprecated, {this.parameters, this.returnType, this.isAbstract: fal se,
66 this.isConst: false, this.isFinal: false, this.isStatic: false});
67
68 factory Element.fromJson(Map<String, Object> map) {
69 ElementKind kind = ElementKind.valueOf(map[KIND]);
70 int flags = map[FLAGS];
71 return new Element(kind, map[NAME], map[OFFSET], map[LENGTH], _hasFlag(
72 flags, _FLAG_PRIVATE), _hasFlag(flags, _FLAG_DEPRECATED), parameters:
73 map[PARAMETERS], returnType: map[RETURN_TYPE], isAbstract: _hasFlag(flag s,
74 _FLAG_ABSTRACT), isConst: _hasFlag(flags, _FLAG_CONST), isFinal: _hasFla g(flags,
75 _FLAG_FINAL), isStatic: _hasFlag(flags, _FLAG_STATIC));
76 }
77
78 int get flags {
79 int flags = 0;
80 if (isAbstract) flags |= _FLAG_ABSTRACT;
81 if (isConst) flags |= _FLAG_CONST;
82 if (isFinal) flags |= _FLAG_FINAL;
83 if (isStatic) flags |= _FLAG_STATIC;
84 if (isPrivate) flags |= _FLAG_PRIVATE;
85 if (isDeprecated) flags |= _FLAG_DEPRECATED;
86 return flags;
87 }
88
89 Map<String, Object> toJson() {
90 Map<String, Object> json = {
91 KIND: kind.name,
92 NAME: name,
93 OFFSET: offset,
94 LENGTH: length,
95 FLAGS: flags
96 };
97 if (parameters != null) {
98 json[PARAMETERS] = parameters;
99 }
100 if (returnType != null) {
101 json[RETURN_TYPE] = returnType;
102 }
103 return json;
104 }
105
106 static bool _hasFlag(int flags, int flag) => (flags & flag) != 0;
107 }
108
109
110 /**
111 * An enumeration of the kinds of elements.
112 */
113 class ElementKind {
114 static const CLASS = const ElementKind('CLASS');
115 static const CLASS_TYPE_ALIAS = const ElementKind('CLASS_TYPE_ALIAS');
116 static const COMPILATION_UNIT = const ElementKind('COMPILATION_UNIT');
117 static const CONSTRUCTOR = const ElementKind('CONSTRUCTOR');
118 static const FIELD = const ElementKind('FIELD');
119 static const FUNCTION = const ElementKind('FUNCTION');
120 static const FUNCTION_TYPE_ALIAS = const ElementKind('FUNCTION_TYPE_ALIAS');
121 static const GETTER = const ElementKind('GETTER');
122 static const LIBRARY = const ElementKind('LIBRARY');
123 static const METHOD = const ElementKind('METHOD');
124 static const SETTER = const ElementKind('SETTER');
125 static const TOP_LEVEL_VARIABLE = const ElementKind('TOP_LEVEL_VARIABLE');
126 static const UNIT_TEST_CASE = const ElementKind('UNIT_TEST_CASE');
127 static const UNIT_TEST_GROUP = const ElementKind('UNIT_TEST_GROUP');
128 static const UNKNOWN = const ElementKind('UNKNOWN');
129
130 final String name;
131
132 const ElementKind(this.name);
133
134 static ElementKind valueOf(String name) {
135 if (CLASS.name == name) return CLASS;
136 if (CLASS_TYPE_ALIAS.name == name) return CLASS_TYPE_ALIAS;
137 if (COMPILATION_UNIT.name == name) return COMPILATION_UNIT;
138 if (CONSTRUCTOR.name == name) return CONSTRUCTOR;
139 if (FIELD.name == name) return FIELD;
140 if (FUNCTION.name == name) return FUNCTION;
141 if (FUNCTION_TYPE_ALIAS.name == name) return FUNCTION_TYPE_ALIAS;
142 if (GETTER.name == name) return GETTER;
143 if (LIBRARY.name == name) return LIBRARY;
144 if (METHOD.name == name) return METHOD;
145 if (SETTER.name == name) return SETTER;
146 if (TOP_LEVEL_VARIABLE.name == name) return TOP_LEVEL_VARIABLE;
147 if (UNIT_TEST_CASE.name == name) return UNIT_TEST_CASE;
148 if (UNIT_TEST_GROUP.name == name) return UNIT_TEST_GROUP;
149 if (UNKNOWN.name == name) return UNKNOWN;
150 throw new ArgumentError('Unknown ElementKind: $name');
151 }
152 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698