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

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

Issue 367793002: Update Element/navigation/outline to use Location. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Forgotten file Created 6 years, 5 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library computer.element; 5 library computer.element;
6 6
7 import 'package:analysis_server/src/constants.dart'; 7 import 'package:analysis_server/src/constants.dart';
8 import 'package:analyzer/src/generated/element.dart' as engine; 8 import 'package:analyzer/src/generated/element.dart' as engine;
9 import 'package:analyzer/src/generated/source.dart';
9 import 'package:analyzer/src/generated/utilities_dart.dart' as engine; 10 import 'package:analyzer/src/generated/utilities_dart.dart' as engine;
10 11
11 12
12 /** 13 /**
13 * Information about an element. 14 * Information about an element.
14 */ 15 */
15 class Element { 16 class Element {
16 static const List<Element> EMPTY_ARRAY = const <Element>[]; 17 static const List<Element> EMPTY_ARRAY = const <Element>[];
17 18
18 static const int FLAG_ABSTRACT = 0x01; 19 static const int FLAG_ABSTRACT = 0x01;
19 static const int FLAG_CONST = 0x02; 20 static const int FLAG_CONST = 0x02;
21 static const int FLAG_FINAL = 0x04;
22 static const int FLAG_STATIC = 0x08;
23 static const int FLAG_PRIVATE = 0x10;
20 static const int FLAG_DEPRECATED = 0x20; 24 static const int FLAG_DEPRECATED = 0x20;
21 static const int FLAG_FINAL = 0x04;
22 static const int FLAG_PRIVATE = 0x10;
23 static const int FLAG_STATIC = 0x08;
24
25 final bool isAbstract;
26 final bool isConst;
27 final bool isDeprecated;
28 final bool isFinal;
29 final bool isPrivate;
30 final bool isStatic;
31 25
32 /** 26 /**
33 * The kind of the element. 27 * The kind of the element.
34 */ 28 */
35 final ElementKind kind; 29 final ElementKind kind;
36 30
37 /** 31 /**
38 * The length of the name of the element.
39 */
40 final int length;
41
42 /**
43 * The name of the element. This is typically used as the label in the outline . 32 * The name of the element. This is typically used as the label in the outline .
44 */ 33 */
45 final String name; 34 final String name;
46 35
47 /** 36 /**
48 * The offset of the name of the element. 37 * The location of the element.
49 */ 38 */
50 final int offset; 39 final Location location;
51 40
52 /** 41 /**
53 * The parameter list for the element. 42 * The parameter list for the element.
54 * If the element is not a method or function then `null`. 43 * If the element is not a method or function then `null`.
55 * If the element has zero parameters, then `()`. 44 * If the element has zero parameters, then `()`.
56 */ 45 */
57 final String parameters; 46 final String parameters;
58 47
59 /** 48 /**
60 * The return type of the element. 49 * The return type of the element.
61 * If the element is not a method or function then `null`. 50 * If the element is not a method or function then `null`.
62 * If the element does not have a declared return type, then an empty string. 51 * If the element does not have a declared return type, then an empty string.
63 */ 52 */
64 final String returnType; 53 final String returnType;
65 54
66 Element(this.kind, this.name, this.offset, this.length, this.isPrivate, 55 final bool isAbstract;
56 final bool isConst;
57 final bool isFinal;
58 final bool isStatic;
59 final bool isPrivate;
60 final bool isDeprecated;
61
62 Element(this.kind, this.name, this.location, this.isPrivate,
67 this.isDeprecated, {this.parameters, this.returnType, this.isAbstract: fal se, 63 this.isDeprecated, {this.parameters, this.returnType, this.isAbstract: fal se,
68 this.isConst: false, this.isFinal: false, this.isStatic: false}); 64 this.isConst: false, this.isFinal: false, this.isStatic: false});
69 65
70 factory Element.fromEngine(engine.Element element) { 66 factory Element.fromEngine(engine.Element element) {
71 String name = element.displayName; 67 String name = element.displayName;
72 int nameLength = name != null ? name.length : 0;
73 String elementParameters = _getParametersString(element); 68 String elementParameters = _getParametersString(element);
74 String elementReturnType = _getReturnTypeString(element); 69 String elementReturnType = _getReturnTypeString(element);
75 return new Element(ElementKind.valueOfEngine(element.kind), name, 70 return new Element(ElementKind.valueOfEngine(element.kind), name,
76 element.nameOffset, nameLength, element.isPrivate, element.isDeprecated, 71 new Location.fromElement(element), element.isPrivate, element.isDeprecat ed,
77 parameters: elementParameters, returnType: elementReturnType, isAbstract : 72 parameters: elementParameters, returnType: elementReturnType, isAbstract :
78 _isAbstract(element), isConst: _isConst(element), isFinal: _isFinal(elem ent), 73 _isAbstract(element), isConst: _isConst(element), isFinal: _isFinal(elem ent),
79 isStatic: _isStatic(element)); 74 isStatic: _isStatic(element));
80 } 75 }
81 76
82 factory Element.fromJson(Map<String, Object> map) { 77 factory Element.fromJson(Map<String, Object> map) {
83 ElementKind kind = ElementKind.valueOf(map[KIND]); 78 ElementKind kind = ElementKind.valueOf(map[KIND]);
84 int flags = map[FLAGS]; 79 int flags = map[FLAGS];
85 return new Element(kind, map[NAME], map[OFFSET], map[LENGTH], _hasFlag( 80 return new Element(kind, map[NAME], new Location.fromJson(map[LOCATION]),
86 flags, FLAG_PRIVATE), _hasFlag(flags, FLAG_DEPRECATED), parameters: 81 _hasFlag(flags, FLAG_PRIVATE), _hasFlag(flags, FLAG_DEPRECATED), paramet ers:
87 map[PARAMETERS], returnType: map[RETURN_TYPE], isAbstract: _hasFlag(flag s, 82 map[PARAMETERS], returnType: map[RETURN_TYPE], isAbstract: _hasFlag(flag s,
88 FLAG_ABSTRACT), isConst: _hasFlag(flags, FLAG_CONST), isFinal: _hasFlag( flags, 83 FLAG_ABSTRACT), isConst: _hasFlag(flags, FLAG_CONST), isFinal: _hasFlag( flags,
89 FLAG_FINAL), isStatic: _hasFlag(flags, FLAG_STATIC)); 84 FLAG_FINAL), isStatic: _hasFlag(flags, FLAG_STATIC));
90 } 85 }
91 86
92 int get flags { 87 int get flags {
93 int flags = 0; 88 int flags = 0;
94 if (isAbstract) flags |= FLAG_ABSTRACT; 89 if (isAbstract) flags |= FLAG_ABSTRACT;
95 if (isConst) flags |= FLAG_CONST; 90 if (isConst) flags |= FLAG_CONST;
96 if (isFinal) flags |= FLAG_FINAL; 91 if (isFinal) flags |= FLAG_FINAL;
97 if (isStatic) flags |= FLAG_STATIC; 92 if (isStatic) flags |= FLAG_STATIC;
98 if (isPrivate) flags |= FLAG_PRIVATE; 93 if (isPrivate) flags |= FLAG_PRIVATE;
99 if (isDeprecated) flags |= FLAG_DEPRECATED; 94 if (isDeprecated) flags |= FLAG_DEPRECATED;
100 return flags; 95 return flags;
101 } 96 }
102 97
103 Map<String, Object> toJson() { 98 Map<String, Object> toJson() {
104 Map<String, Object> json = { 99 Map<String, Object> json = {
105 KIND: kind.name, 100 KIND: kind.name,
106 NAME: name, 101 NAME: name,
107 OFFSET: offset, 102 LOCATION: location.toJson(),
108 LENGTH: length,
109 FLAGS: flags 103 FLAGS: flags
110 }; 104 };
111 if (parameters != null) { 105 if (parameters != null) {
112 json[PARAMETERS] = parameters; 106 json[PARAMETERS] = parameters;
113 } 107 }
114 if (returnType != null) { 108 if (returnType != null) {
115 json[RETURN_TYPE] = returnType; 109 json[RETURN_TYPE] = returnType;
116 } 110 }
117 return json; 111 return json;
118 } 112 }
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 } 271 }
278 if (kind == engine.ElementKind.SETTER) { 272 if (kind == engine.ElementKind.SETTER) {
279 return SETTER; 273 return SETTER;
280 } 274 }
281 if (kind == engine.ElementKind.TOP_LEVEL_VARIABLE) { 275 if (kind == engine.ElementKind.TOP_LEVEL_VARIABLE) {
282 return TOP_LEVEL_VARIABLE; 276 return TOP_LEVEL_VARIABLE;
283 } 277 }
284 return UNKNOWN; 278 return UNKNOWN;
285 } 279 }
286 } 280 }
281
282
283 /**
284 * Information about a location.
285 */
286 class Location {
287 final String file;
288 final int offset;
289 final int length;
290 final int startLine;
291 final int startColumn;
292
293 Location(this.file, this.offset, this.length, this.startLine,
294 this.startColumn);
295
296 factory Location.fromElement(engine.Element element) {
297 Source source = element.source;
298 LineInfo lineInfo = element.context.getLineInfo(source);
299 String name = element.displayName;
300 // prepare location
301 int offset = element.nameOffset;
302 int length = name != null ? name.length : 0;
303 LineInfo_Location lineLocation = lineInfo.getLocation(offset);
304 int startLine = lineLocation.lineNumber;
305 int startColumn = lineLocation.columnNumber;
306 if (element is engine.CompilationUnitElement) {
307 offset = 0;
308 length = 0;
309 startLine = 1;
310 startColumn = 1;
311 }
312 // done
313 return new Location(source.fullName, offset, length, startLine,
314 startColumn);
315 }
316
317 factory Location.fromJson(Map<String, Object> map) {
318 return new Location(map[FILE], map[OFFSET], map[LENGTH], map[START_LINE],
319 map[START_COLUMN]);
320 }
321
322 Map<String, Object> toJson() {
323 return {
324 FILE: file,
325 OFFSET: offset,
326 LENGTH: length,
327 START_LINE: startLine,
328 START_COLUMN: startColumn
329 };
330 }
331
332 @override
333 String toString() {
334 return 'Location(file=$file; offset=$offset; length=$length; '
335 'startLine=$startLine; startColumn=$startColumn)';
336 }
337 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698