OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analysis_server.plugin.index.index_core; | 5 library analysis_server.plugin.index.index_core; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:analysis_server/src/services/index/index.dart'; | 10 import 'package:analysis_server/src/services/index/index.dart'; |
11 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
12 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
13 | 13 |
14 /** | 14 /** |
15 * Return the integer value that corresponds to the given [string]. The value of | 15 * Return the integer value that corresponds to the given [string]. The value of |
16 * [string] may be `null`. | 16 * [string] may be `null`. |
17 * | 17 * |
18 * Clients are not expected to implement this signature. A function of this type | 18 * Clients are not expected to implement this signature. A function of this type |
19 * is provided by the framework to clients in order to implement the method | 19 * is provided by the framework to clients in order to implement the method |
20 * [IndexableObjectKind.encodeHash]. | 20 * [IndexableObjectKind.encodeHash]. |
21 */ | 21 */ |
22 typedef int StringToInt(String string); | 22 typedef int StringToInt(String string); |
23 | 23 |
24 /** | 24 /** |
25 * An object that can have a [Relationship] with various [Location]s in a code | 25 * An object that can have a [Relationship] with various [Location]s in a code |
26 * base. The object is abstractly represented by a [kind] and an [offset] within | 26 * base. The object is abstractly represented by a [kind] and an [offset] within |
27 * a [source]. | 27 * a file with the [filePath]. |
28 * | 28 * |
29 * Clients must ensure that two distinct objects in the same source cannot have | 29 * Clients must ensure that two distinct objects in the same source cannot have |
30 * the same kind and offset. Failure to do so will make it impossible for | 30 * the same kind and offset. Failure to do so will make it impossible for |
31 * clients to identify the model element corresponding to the indexable object. | 31 * clients to identify the model element corresponding to the indexable object. |
32 * | 32 * |
33 * Clients are expected to subtype this class when implementing plugins. | 33 * Clients are expected to subtype this class when implementing plugins. |
34 */ | 34 */ |
35 abstract class IndexableObject { | 35 abstract class IndexableObject { |
36 /** | 36 /** |
37 * Return the absolute path of the file containing the indexable object. | 37 * Return the absolute path of the file containing the indexable object. |
38 */ | 38 */ |
39 String get filePath; | 39 String get filePath; |
40 | 40 |
41 /** | 41 /** |
42 * Return the kind of this object. | 42 * Return the kind of this object. |
43 */ | 43 */ |
44 IndexableObjectKind get kind; | 44 IndexableObjectKind get kind; |
45 | 45 |
46 /** | 46 /** |
47 * Return the offset of the indexable object within its source. | 47 * Return the offset of the indexable object within its file. |
48 */ | 48 */ |
49 int get offset; | 49 int get offset; |
50 } | 50 } |
51 | 51 |
52 /** | 52 /** |
53 * The kind associated with an [IndexableObject]. | 53 * The kind associated with an [IndexableObject]. |
54 * | 54 * |
55 * Clients are expected to implement this class when implementing plugins. | 55 * Clients are expected to implement this class when implementing plugins. |
56 */ | 56 */ |
57 abstract class IndexableObjectKind { | 57 abstract class IndexableObjectKind<T extends IndexableObject> { |
58 /** | 58 /** |
59 * The next available index for a newly created kind of indexable object. | 59 * The next available index for a newly created kind of indexable object. |
60 */ | 60 */ |
61 static int _nextIndex = 0; | 61 static int _nextIndex = 0; |
62 | 62 |
63 /** | 63 /** |
64 * A table mapping indexes to object kinds. | 64 * A table mapping indexes to object kinds. |
65 */ | 65 */ |
66 static Map<int, IndexableObjectKind> _registry = | 66 static Map<int, IndexableObjectKind> _registry = |
67 new HashMap<int, IndexableObjectKind>(); | 67 new HashMap<int, IndexableObjectKind>(); |
68 | 68 |
69 /** | 69 /** |
70 * Return the next available index for a newly created kind of indexable | 70 * Return the next available index for a newly created kind of indexable |
71 * object. | 71 * object. |
72 */ | 72 */ |
73 static int get nextIndex => _nextIndex++; | 73 static int get nextIndex => _nextIndex++; |
74 | 74 |
75 /** | 75 /** |
76 * Return the unique index for this kind of indexable object. Implementations | 76 * Return the unique index for this kind of indexable object. Implementations |
77 * should invoke [nextIndex] to allocate an index that cannot be used by any | 77 * should invoke [nextIndex] to allocate an index that cannot be used by any |
78 * other object kind. | 78 * other object kind. |
79 */ | 79 */ |
80 int get index; | 80 int get index; |
81 | 81 |
82 /** | 82 /** |
83 * Return the indexable object of this kind that exists in the given | 83 * Return the indexable object of this kind that exists in the given |
84 * [context], in the source with the given [filePath], and at the given | 84 * [context], in the source with the given [filePath], and at the given |
85 * [offset]. | 85 * [offset]. |
86 */ | 86 */ |
87 IndexableObject decode(AnalysisContext context, String filePath, int offset); | 87 T decode(AnalysisContext context, String filePath, int offset); |
88 | 88 |
89 /** | 89 /** |
90 * Returns the hash value that corresponds to the given [indexable]. | 90 * Returns the hash value that corresponds to the given [indexable]. |
91 * | 91 * |
92 * This hash is used to remember buckets with relations of the given | 92 * This hash is used to remember buckets with relations of the given |
93 * [indexable]. Usually the name of the indexable object is encoded | 93 * [indexable]. Usually the name of the indexable object is encoded |
94 * using [stringToInt] and mixed with other information to produce the final | 94 * using [stringToInt] and mixed with other information to produce the final |
95 * result. | 95 * result. |
96 * | 96 * |
97 * Clients must ensure that the same value is returned for the same object. | 97 * Clients must ensure that the same value is returned for the same object. |
98 * | 98 * |
99 * Returned values must have good selectivity, e.g. if it is possible that | 99 * Returned values must have good selectivity, e.g. if it is possible that |
100 * there are many different objects with the same name, then additional | 100 * there are many different objects with the same name, then additional |
101 * information should be mixed in, for example the hash of the source that | 101 * information should be mixed in, for example the hash of the source that |
102 * declares the given [indexable]. | 102 * declares the given [indexable]. |
103 * | 103 * |
104 * Clients don't have to use name to compute this result, so if an indexable | 104 * Clients don't have to use name to compute this result, so if an indexable |
105 * object does not have a name, some other value may be returned, but it still | 105 * object does not have a name, some other value may be returned, but it still |
106 * must be always the same for the same object and have good selectivity. | 106 * must be always the same for the same object and have good selectivity. |
107 */ | 107 */ |
108 int encodeHash(StringToInt stringToInt, IndexableObject indexable); | 108 int encodeHash(StringToInt stringToInt, T indexable); |
109 | 109 |
110 /** | 110 /** |
111 * Return the object kind with the given [index]. | 111 * Return the object kind with the given [index]. |
112 */ | 112 */ |
113 static IndexableObjectKind getKind(int index) { | 113 static IndexableObjectKind getKind(int index) { |
114 return _registry[index]; | 114 return _registry[index]; |
115 } | 115 } |
116 | 116 |
117 /** | 117 /** |
118 * Register the given object [kind] so that it can be found by it's unique | 118 * Register the given object [kind] so that it can be found by it's unique |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 */ | 271 */ |
272 abstract class Relationship { | 272 abstract class Relationship { |
273 /** | 273 /** |
274 * Return a relationship that has the given [identifier]. If the relationship | 274 * Return a relationship that has the given [identifier]. If the relationship |
275 * has already been created, then it will be returned, otherwise a new | 275 * has already been created, then it will be returned, otherwise a new |
276 * relationship will be created | 276 * relationship will be created |
277 */ | 277 */ |
278 factory Relationship(String identifier) => | 278 factory Relationship(String identifier) => |
279 RelationshipImpl.getRelationship(identifier); | 279 RelationshipImpl.getRelationship(identifier); |
280 } | 280 } |
OLD | NEW |