5
5
import 'dart:collection' ;
6
6
7
7
import 'package:analyzer/dart/analysis/analysis_context.dart' ;
8
- import 'package:analyzer/dart/ast/ast.dart' ;
9
8
import 'package:analyzer/dart/element/element.dart' ;
10
9
import 'package:analyzer/file_system/file_system.dart' ;
11
10
import 'package:analyzer/source/source.dart' ;
12
11
// ignore: implementation_imports
12
+ import 'package:analyzer/src/dart/ast/ast.dart' ;
13
+ // ignore: implementation_imports
13
14
import 'package:analyzer/src/dart/element/inheritance_manager3.dart'
14
15
show InheritanceManager3;
15
16
// ignore: implementation_imports
@@ -234,6 +235,20 @@ class PackageGraph with CommentReferable, Nameable {
234
235
// me how, because the data is on AST nodes, not the element model.
235
236
void gatherModelNodes (DartDocResolvedLibrary resolvedLibrary) {
236
237
for (var unit in resolvedLibrary.units) {
238
+ for (var directive in unit.directives.whereType <LibraryDirective >()) {
239
+ // There should be only one library directive. If there are more, there
240
+ // is no harm in grabbing ModelNode for each.
241
+ var commentReferenceData = directive.documentationComment? .data;
242
+ _modelNodes.putIfAbsent (
243
+ resolvedLibrary.element,
244
+ () => ModelNode (
245
+ directive,
246
+ resolvedLibrary.element,
247
+ analysisContext,
248
+ commentReferenceData: commentReferenceData,
249
+ ));
250
+ }
251
+
237
252
for (var declaration in unit.declarations) {
238
253
_populateModelNodeFor (declaration);
239
254
switch (declaration) {
@@ -243,6 +258,9 @@ class PackageGraph with CommentReferable, Nameable {
243
258
}
244
259
case EnumDeclaration ():
245
260
if (declaration.declaredElement? .isPublic ?? false ) {
261
+ for (var constant in declaration.constants) {
262
+ _populateModelNodeFor (constant);
263
+ }
246
264
for (var member in declaration.members) {
247
265
_populateModelNodeFor (member);
248
266
}
@@ -269,12 +287,21 @@ class PackageGraph with CommentReferable, Nameable {
269
287
}
270
288
271
289
void _populateModelNodeFor (Declaration declaration) {
290
+ var commentReferenceData = declaration.documentationComment? .data;
291
+
272
292
if (declaration is FieldDeclaration ) {
273
293
var fields = declaration.fields.variables;
274
294
for (var field in fields) {
275
295
var element = field.declaredElement! ;
276
296
_modelNodes.putIfAbsent (
277
- element, () => ModelNode (field, element, analysisContext));
297
+ element,
298
+ () => ModelNode (
299
+ field,
300
+ element,
301
+ analysisContext,
302
+ commentReferenceData: commentReferenceData,
303
+ ),
304
+ );
278
305
}
279
306
return ;
280
307
}
@@ -283,13 +310,27 @@ class PackageGraph with CommentReferable, Nameable {
283
310
for (var field in fields) {
284
311
var element = field.declaredElement! ;
285
312
_modelNodes.putIfAbsent (
286
- element, () => ModelNode (field, element, analysisContext));
313
+ element,
314
+ () => ModelNode (
315
+ field,
316
+ element,
317
+ analysisContext,
318
+ commentReferenceData: commentReferenceData,
319
+ ),
320
+ );
287
321
}
288
322
return ;
289
323
}
290
324
var element = declaration.declaredElement! ;
291
325
_modelNodes.putIfAbsent (
292
- element, () => ModelNode (declaration, element, analysisContext));
326
+ element,
327
+ () => ModelNode (
328
+ declaration,
329
+ element,
330
+ analysisContext,
331
+ commentReferenceData: commentReferenceData,
332
+ ),
333
+ );
293
334
}
294
335
295
336
ModelNode ? getModelNodeFor (Element element) => _modelNodes[element];
@@ -1029,3 +1070,41 @@ class InheritableElementsKey {
1029
1070
1030
1071
InheritableElementsKey (this .element, this .library);
1031
1072
}
1073
+
1074
+ extension on Comment {
1075
+ /// A mapping of all comment references to their various data.
1076
+ Map <String , CommentReferenceData > get data {
1077
+ if (references.isEmpty) return const {};
1078
+
1079
+ var data = < String , CommentReferenceData > {};
1080
+ for (var reference in references) {
1081
+ var commentReferable = reference.expression;
1082
+ String name;
1083
+ Element ? staticElement;
1084
+ if (commentReferable case PropertyAccessImpl (: var propertyName)) {
1085
+ var target = commentReferable.target;
1086
+ if (target is ! PrefixedIdentifierImpl ) continue ;
1087
+ name = '${target .name }.${propertyName .name }' ;
1088
+ staticElement = propertyName.staticElement;
1089
+ } else if (commentReferable case PrefixedIdentifier (: var identifier)) {
1090
+ name = commentReferable.name;
1091
+ staticElement = identifier.staticElement;
1092
+ } else if (commentReferable case SimpleIdentifier ()) {
1093
+ name = commentReferable.name;
1094
+ staticElement = commentReferable.staticElement;
1095
+ } else {
1096
+ continue ;
1097
+ }
1098
+
1099
+ if (staticElement != null && ! data.containsKey (name)) {
1100
+ data[name] = CommentReferenceData (
1101
+ staticElement,
1102
+ name,
1103
+ commentReferable.offset,
1104
+ commentReferable.length,
1105
+ );
1106
+ }
1107
+ }
1108
+ return data;
1109
+ }
1110
+ }
0 commit comments